toddams / RazorLight

Template engine based on Microsoft's Razor parsing engine for .NET Core
Apache License 2.0
1.51k stars 261 forks source link

Method not found: 'Void Microsoft.Extensions.Caching.Memory.MemoryCacheOptions.set_CompactOnMemoryPressure(Boolean)'. #376

Closed kiran198910 closed 3 years ago

kiran198910 commented 3 years ago

Getting error while parsing template as string. There is a bug happened for CreatePhysical. And Error message is Method not found: 'Void Microsoft.Extensions.Caching.Memory.MemoryCacheOptions.set_CompactOnMemoryPressure(Boolean)'.

IRazorLightEngine mail_engine = EngineFactory.CreatePhysical(HostingEnvironment.MapPath("~/Templates"));

I have no idea the reason and how to resolve it.

Information

jzabroski commented 3 years ago

@kiran198910 Are you really using RazorLight 1.1?

kiran198910 commented 3 years ago

Yes, I am using Razorlight 1.1.0 Screenshot 2020-09-25 040542

jzabroski commented 3 years ago

Use the 2.0.0-beta10 release please. We're getting very close to an official non-beta release.

On Thu, Sep 24, 2020 at 6:37 PM kiran198910 notifications@github.com wrote:

Yes, I am using Razorlight 1.1.0 [image: Screenshot 2020-09-25 040542] https://user-images.githubusercontent.com/51714857/94207114-8bd1c300-fee4-11ea-813b-bb2b8c9d442a.png

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/toddams/RazorLight/issues/376#issuecomment-698623602, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADNH7K4KMH4A26QDKL4GNLSHPCZXANCNFSM4RYZELOA .

kiran198910 commented 3 years ago

Now I have upgraded RazorLight version to 2.0.0-beta10. I am trying to parse HTML template to string. Here is my code

var engine = new RazorLightEngineBuilder().UseFileSystemProject(HostingEnvironment.MapPath("~/Templates")) .UseMemoryCachingProvider() .Build(); var model = new { Name = "John Doe" }; string result = await engine.CompileRenderAsync("View.cshtml", model); But Now I am getting error "Value cannot be null.Parameter name: operatingAssembly ". Please advice

jzabroski commented 3 years ago

On the builder, there is an extension method SetOperatingAssembly. You can set to to Assembly.GetEntryAssembly() if that is where you locate your RazorLight assemblies.

devssharma commented 3 years ago

I am getting bellow error, do we have any fix of this issue

Method not found: 'System.Collections.Generic.IList`1 Microsoft.Extensions.Caching.Memory.MemoryCacheEntryOptions.get_ExpirationTokens()'.

jzabroski commented 3 years ago

@devssharma What version of RazorLight are you using?

devssharma commented 3 years ago

@jzabroski i am using RazorLight 2.0.0-beta9 in .net core 3.1 project. image

jzabroski commented 3 years ago

@devssharma In looking at this more carefully, the issue seems to be that in ASP.NET Core 2.x, they removed this interface, and then put it back but marked it as obsolete before the final release.

Compare:

  1. 2.2 docs: https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.caching.memory.memorycacheoptions?view=dotnet-plat-ext-2.2
  2. 3.0 docs: no mention of this setting.
jzabroski commented 3 years ago

@devssharma Can you provide your full stack trace? We don't directly call this method directly, so it seems like you're referencing Microsoft.Extensions.Caching 2.2 in a .netcoreapp3.1 TFM somehow.

jzabroski commented 3 years ago

See also: https://github.com/toddams/RazorLight/issues/69#issuecomment-604134253 -- previously another user ran into this library and reported their Nuget Package Manager went haywire and it was not RazorLight that caused the issue. I think that's LIKELY the case here. But you need to get much, much better at providing repro details. You're giving me partial stack traces and no PackageReference or TargetFramework xml.

devssharma commented 3 years ago

@jzabroski I tried razor parsing in temporary .net core 3.1 console app, it successfully parsing the razor template. but in actual lambda function (.net core 3.1) not working.

Error: Method not found: 'System.Collections.Generic.IList`1 Microsoft.Extensions.Caching.Memory.MemoryCacheEntryOptions.get_ExpirationTokens()'.

Information OS: Windows 10 Platform .NET Core 3.1 RazorLight version : 2.0.0 beta-9 Visual Studio version : VS2019 [16.7.2]

this is what i have written in my project for razor template parsing.

Create a function for parsing the razor template.

async Task RazorParsingString(string tmpl) { Assembly assembly = Assembly.GetExecutingAssembly(); var razorEngine = new RazorLightEngineBuilder() .UseEmbeddedResourcesProject(assembly, "OrderComponent") // OrderComponent is a project name .UseMemoryCachingProvider() .Build();

        string sqlTmplt = tmpl;

        string sql = await razorEngine.CompileRenderStringAsync("templateKey", sqlTmplt, new { OrderID = "123" });

        return sql;
    }

and i am calling above function from actual lambda function. looks like.

public JObject Process(JObject input, ILambdaContext context) { context.Logger.LogLine("Get Request\n"); string sql = string.Empty;

        string tmpl = "select* from OrderTable Where OrderID = @Model.OrderID";
        context.Logger.LogLine(tmpl);
        Task.Run(async () => { sql = await RazorParsingString(tmpl); }).Wait();

        context.Logger.LogLine(sql);

.......

my requirement is just parse the razor template in .NET core 3.1 version. please help me on this

jzabroski commented 3 years ago

Hi - I don't think I have the time to debug why this won't work on AWS Lambda. Maybe you can open a support request with AWS as to why when you convert a console app to a lambda app, all of a sudden it stops working? This doesn't sounds like a RazorLight problem, and AWS support is pretty good. If they do give you good feedback, I would appreciate it if you could share what they tell you so others benefit.

That said, you probably shouldn't use the ReadyToRun flag with RazorLight, as it dynamically loads dependencies.

jzabroski commented 3 years ago

@devssharma Did you ever get any help from AWS Support on this?

devssharma commented 3 years ago

@jzabroski My problem was resolved with existing library so I didn't contacted them.

Thank you so much for help on this.

eralston commented 3 years ago

Apologies poking the sleeping dog here, but this is literally the only Google result for this issue:

I am now receiving this same error as a regression bug in my project...

image

Except in my case it's in an Azure Function (.Net Core 3.1 using RazorLight 2.0.0-rc.2) that was previously working properly. I have the following code (which again, was working for months up to a short-time ago):

public static void Init(Assembly entryAssembly, Assembly viewAssembly, string rootNamespace)
        {
            Engine = new RazorLightEngineBuilder()
                                .SetOperatingAssembly(entryAssembly)
                                .UseEmbeddedResourcesProject(viewAssembly, rootNamespace)
                                .UseMemoryCachingProvider()
                                .Build();
        }

public async Task<string> RenderAsync<TModel>(string viewName, TModel model)
        {
            string result = await Engine.CompileRenderAsync(viewName, model);
            return result;
        }

I would welcome any insight. I will likely go into jumping through past versions now and if I find a solution, I will post it here in the hopes that future generations with not suffer the same fate.