RazorEngine
RazorEngine copied to clipboard
RazorEngine.Core.NetCore in a Linux docker container: 'COM Interop is not supported on this platform.'
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.
I got same problem on macos 10.13.2
me too...
+1
I got the same problem on macos 10.14.3
+1 macos 10.14.3
Blocking my tool development. :(
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.
Still not working :( Need a fix for this.
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?
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
?
Did you implemented the changes above?
Basically you only need to change (line#242) :
compilation.Emit(assemblyStream, pdbStreamHelper, options: opts)
tocompilation.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.
What is the current status of this error? Any updates? Is there any way to run this package on Linux?
any fix on this?
still getting the same error in docker
did you guys use any alternative plugin aside from razor engine that works in container
See my previous comment. :)
I am on RazorLight now. It has other downsides, but it atleast fixed my problem for the moment.
I switched to a different template engine (not Razor).
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.'
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.'
I switched to a different template engine (not Razor).
What did you change to?
I think as of today, you got 2 options (without changing your existing templates):
- Using the fix of @mgoodfellow available from Nuget. Ref https://github.com/fouadmess/RazorEngine/pull/3
- Switching to RazorLight. Works like RazorEngine, but really more maintained
Personally, I switched to RazorLight
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.