RazorEngine icon indicating copy to clipboard operation
RazorEngine copied to clipboard

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

Open Vitala opened this issue 6 years ago • 21 comments

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.

Vitala avatar Nov 19 '18 13:11 Vitala

I got same problem on macos 10.13.2

yindongfei avatar Dec 11 '18 12:12 yindongfei

me too...

kyamada1221 avatar Feb 20 '19 15:02 kyamada1221

+1

kmute90 avatar Feb 22 '19 23:02 kmute90

I got the same problem on macos 10.14.3

Wingjam avatar Apr 02 '19 18:04 Wingjam

+1 macos 10.14.3

Blocking my tool development. :(

jvukovich avatar Apr 18 '19 04:04 jvukovich

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.

Mudshovel avatar Apr 18 '19 16:04 Mudshovel

Still not working :( Need a fix for this.

janniksam avatar Apr 19 '19 08:04 janniksam

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?

Mudshovel avatar Apr 19 '19 09:04 Mudshovel

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?

mckabue avatar Apr 19 '19 12:04 mckabue

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.

janniksam avatar Apr 21 '19 10:04 janniksam

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

milons avatar May 27 '19 21:05 milons

any fix on this?

jfvaleroso avatar Aug 06 '19 03:08 jfvaleroso

still getting the same error in docker

vosaba avatar Aug 06 '19 10:08 vosaba

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

jfvaleroso avatar Aug 06 '19 15:08 jfvaleroso

See my previous comment. :)

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

janniksam avatar Aug 06 '19 16:08 janniksam

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

jvukovich avatar Aug 06 '19 16:08 jvukovich

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.'

SAssassin avatar Nov 26 '19 14:11 SAssassin

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

  • error: (0, 0) Unexpected error writing debug information -- 'COM Interop is not supported on this platform.'

namilkimfree avatar Dec 16 '19 07:12 namilkimfree

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

What did you change to?

namilkimfree avatar Dec 16 '19 08:12 namilkimfree

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

Wingjam avatar Dec 16 '19 14:12 Wingjam

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.

jasonsultana avatar Jan 28 '21 09:01 jasonsultana