Antaris / RazorEngine

Open source templating engine based on Microsoft's Razor parsing engine
http://antaris.github.io/RazorEngine
Other
2.14k stars 578 forks source link

RazorEngine.Core.NetCore in a Linux docker container: 'COM Interop is not supported on this platform.' #539

Open Vitala opened 6 years ago

Vitala commented 6 years ago

Can you give me a slight insight what's happening here and why it tries to use Interop?

        var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

        var templateManager = new ResolvePathTemplateManager(new[]
            {Path.Combine(path, EmailTemplatesFolder)});

        var config = new TemplateServiceConfiguration
        {
            TemplateManager = templateManager,
        };

        var templateService = RazorEngineService.Create(config);

       var body = templateService .RunCompile($"MyTemplate.cshtml", null, _myModel);

The template itself is quite simple:

@model server.Models.MyModel

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Welcome</title>
    @{ Model.Subject = "bla bla bla"; }
</head>
<body>
    <p>
        Hello @Model.Username,
    </p>
</body>
</html>

However when I run it on a remote Linux machine in a docker container I got the following error:

RazorEngine.Templating.TemplateCompilationException: Errors while compiling a Template.
Please try the following to solve the situation:
  * If the problem is about missing/invalid references or multiple defines either try to load 
    the missing references manually (in the compiling appdomain!) or
    Specify your references manually by providing your own IReferenceResolver implementation.
    See https://antaris.github.io/RazorEngine/ReferenceResolver.html for details.
    Currently all references have to be available as files!
  * If you get 'class' does not contain a definition for 'member': 
        try another modelType (for example 'null' to make the model dynamic).
        NOTE: You CANNOT use typeof(dynamic) to make the model dynamic!
    Or try to use static instead of anonymous/dynamic types.
More details about the error:
 - warning: (7, 4) Unnecessary using directive.
     - warning: (6, 4) Unnecessary using directive.
     - warning: (8, 4) Unnecessary using directive.
     - warning: (5, 4) Unnecessary using directive.
     - error: (0, 0) Unexpected error writing debug information -- 'COM Interop is not supported on this platform.'

I'm using 4.6.0-rc1 version btw.

yindongfei commented 5 years ago

I got same problem on macos 10.13.2

kyamada1221 commented 5 years ago

me too...

kmute90 commented 5 years ago

+1

Wingjam commented 5 years ago

I got the same problem on macos 10.14.3

jvukovich commented 5 years ago

+1 macos 10.14.3

Blocking my tool development. :(

Mudshovel commented 5 years ago

Workaround :

I had to fork and changed :

RazorEngine.Roslyn.CSharp.RoslynCompilerServiceBase.cs:

using (var assemblyStream = File.Open(assemblyFile, FileMode.Create, FileAccess.ReadWrite))
using (var pdbStream = File.Open(assemblyPdbFile, FileMode.Create, FileAccess.ReadWrite))
{
    var opts = new EmitOptions()
        .WithPdbFilePath(assemblyPdbFile);
    var pdbStreamHelper = pdbStream;

    if (IsMono()) {
        opts = opts.WithDebugInformationFormat(DebugInformationFormat.PortablePdb);
    }

    EmitResult result = null;
    if (Debugger.IsAttached) {
        result = compilation.Emit(assemblyStream, pdbStreamHelper, options: opts);
    } else {
        result = compilation.Emit(assemblyStream);
    }
    //var result = compilation.Emit(assemblyStream, pdbStreamHelper, options: opts);
    //var result = compilation.Emit(assemblyStream);
    if (!result.Success)
    {
        var errors =
            result.Diagnostics.Select(diag => {
                var lineSpan = diag.Location.GetLineSpan();
                return new Templating.RazorEngineCompilerError(
                    string.Format("{0}", diag.GetMessage()),
                    lineSpan.Path,
                    lineSpan.StartLinePosition.Line,
                    lineSpan.StartLinePosition.Character,
                    diag.Id,
                    diag.Severity != DiagnosticSeverity.Error);
            });

        throw new Templating.TemplateCompilationException(errors, compilationData, context.TemplateContent);
    }
}

compilation.Emit(assemblyStream, pdbStreamHelper, options: opts)

is the part that fails.

Be aware that I think you wont get error details in case of a template error.

And of course requires more testing...

Works in a Docker container hosted in kubernetes.

janniksam commented 5 years ago

Still not working :( Need a fix for this.

Mudshovel commented 5 years ago

Did you implemented the changes above?

Basically you only need to change (line#242) : compilation.Emit(assemblyStream, pdbStreamHelper, options: opts) to compilation.Emit(assemblyStream)

I forked this version : https://github.com/fouadmess/RazorEngine

You get the same error?

mckabue commented 5 years ago

Just got the same error on ubuntu when using:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="RazorEngine.NetCore" Version="2.2.2" />
  </ItemGroup>
</Project>

When using dotnet add package RazorEngine --version 4.5.1-alpha001, i get "Could not load file or assembly 'System.Configuration.ConfigurationManager ...." https://github.com/Antaris/RazorEngine/issues/515

@Antaris is it that RazorEngine has never worked on netcoreapp?

janniksam commented 5 years ago

Did you implemented the changes above?

Basically you only need to change (line#242) : compilation.Emit(assemblyStream, pdbStreamHelper, options: opts) to compilation.Emit(assemblyStream)

I forked this version : https://github.com/fouadmess/RazorEngine

You get the same error?

Unfortunately I didnt hesitate and switched the library to get rid of the problem, because I didn't want to fork it and package it myself. I am on RazorLight now. It has other downsides, but it atleast fixed my problem for the moment.

milons commented 5 years ago

What is the current status of this error? Any updates? Is there any way to run this package on Linux?

jfvaleroso commented 5 years ago

any fix on this?

vosaba commented 5 years ago

still getting the same error in docker

jfvaleroso commented 5 years ago

did you guys use any alternative plugin aside from razor engine that works in container

janniksam commented 5 years ago

See my previous comment. :)

I am on RazorLight now. It has other downsides, but it atleast fixed my problem for the moment.

jvukovich commented 5 years ago

I switched to a different template engine (not Razor).

SAssassin commented 5 years ago

the same problem. .net core 2.2 on linux - error: (0, 0) Unexpected error writing debug information -- 'COM Interop is not supported on this platform.'

namilkimfree commented 4 years ago

the same problem. .net core 2.1 ~ 3.1 on mac

namilkimfree commented 4 years ago

I switched to a different template engine (not Razor).

What did you change to?

Wingjam commented 4 years ago

I think as of today, you got 2 options (without changing your existing templates):

  1. Using the fix of @mgoodfellow available from Nuget. Ref https://github.com/fouadmess/RazorEngine/pull/3
  2. Switching to RazorLight. Works like RazorEngine, but really more maintained

Personally, I switched to RazorLight

jasonsultana commented 3 years ago

One interesting observation on MacOS using Rider. If running the project with a debugger attached, I get this error. If the debugger isn't attached, no error.