Antaris / RazorEngine

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

RazorEngine.Core.NetCore CompileRun fail under ASP.NET Core 2.1 #524

Open StuffOfInterest opened 6 years ago

StuffOfInterest commented 6 years ago

Trying to update some .NET Framework code to .NET Core with .NET Standard support libraries. Installed the RazorEngine.Core.NetCore 4.5.2-rc1 package fork of the RazorEngine library which will install under .NET Standard and is consumable by a .NET Core 2.1 test project. Unfortunately, the tests are failing badly.

First, here is how I'm invoking:

var template = "Welcome, @Model.Name!";
var templateName = "somethingRandom";
var model = new { Name = "Jack" };
Engine.Razor.RunCompile(template, templateName, null, model);

Here is the result spit out from the engine:

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:
 - error: (12, 66) 'RazorEngine_c8c16161b0b54440b29148193ab5695b.ExecuteAsync()': no suitable method found to override
Temporary files of the compilation can be found in (please delete the folder): C:\Users\dkmatlock\AppData\Local\Temp\RazorEngine_x5zwu0yv.fvu
The template we tried to compile is: 
------------- START -----------
Welcome, @Model.Name!
------------- END -----------

The generated source code is: 
------------- START -----------
// <auto-generated/>
#pragma warning disable 1591
namespace CompiledRazorTemplates.Dynamic
{
    #line hidden
    using System;
    using System.Threading.Tasks;
    using System.Collections.Generic;
    using System.Linq;
    internal class RazorEngine_c8c16161b0b54440b29148193ab5695b : RazorEngine.Templating.TemplateBase<dynamic>
    {
        #pragma warning disable 1998
        public async override global::System.Threading.Tasks.Task ExecuteAsync()
        {
            WriteLiteral("Welcome, ");
                Write(Model.Name);
            WriteLiteral("!");
        }
        #pragma warning restore 1998
    }
}
#pragma warning restore 1591

Not sure if this is an issue with the port to .NET Core/.NET Standard, the forked package, or the update to ASP.NET Core 2.1. I've got quite a bit of code that is utilizing RazorEngine to translate cshtml (mostly hard coded or database stored) into HTML so I'd hate to give up on this project but it looks like development is stalled until a new maintainer steps up to take over. Getting this issue out there in case someone knows that is going on.

birdlll commented 6 years ago

me to

birdlll commented 6 years ago

Trying to update for 4.5.1-rc1 is ok!

alex-training commented 6 years ago

@birdlll Hi guys, for me still doesn't work

StuffOfInterest commented 6 years ago

I gave up and switched to the RazorLight library. It took a little extra coding to work with their template provider model but after that was done it passed all my unit tests.

csimone86 commented 6 years ago

You must load the assembly with a custom ReferenceResolver

devedse commented 6 years ago

I'm using RazorLight in .NET Core: https://github.com/toddams/RazorLight

pateljan commented 6 years ago

HI,

i have same problem. can someone give working example how to load the assembly with custom RefereceResolver ?

nfabian13 commented 6 years ago

same issue here. please someone share a solution here! :) Thanks

hermanho commented 6 years ago

@nfabian13 , Can you please provide a reproduce repo? Or you could try version 4.6.0-rc1.

MaheshGooner commented 5 years ago

I am trying to run some specflow tests inside a docker container for DotNet Core project. I have the same error even with RazorLight. Does anyone have a solution ? works fine on my windows/ just the problem inside docker container(microsoft/dotnet:2.1.302-sdk-stretch).