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

Unable to instantiate in asp.net core 5.0 project #383

Closed jessicah closed 3 years ago

jessicah commented 3 years ago

Describe the bug services.AddRazorLight() doesn't appear sufficient to register types needed for DI to work.

To Reproduce Steps to reproduce the behavior:

services.AddRazorLight(() =>
{
    return new RazorLightEngineBuilder()
        .UseFileSystemProject("~/repo")
        .UseMemoryCachingProvider()
        .Build();
});

Expected behavior RazorLight dependencies registered and able to run project.

Information (please complete the following information):

Additional context System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: RazorLight.IEngineHandler Lifetime: Singleton ImplementationType: RazorLight.EngineHandler': Unable to resolve service for type 'RazorLight.Compilation.IRazorTemplateCompiler' while attempting to activate 'RazorLight.EngineHandler'.)'

jessicah commented 3 years ago

I managed to workaround the issue by hoisting the RazorEngine instance out of AddRazorLight(), then adding the following:

services.AddSingleton(new RazorLightOptions());
services.AddSingleton(razorEngine.Handler.Compiler);
services.AddSingleton(razorEngine.Handler.FactoryProvider);
services.AddSingleton(razorEngine.Handler.Cache);

But it would be nice to not have to do this... although now I'm blocked on https://github.com/toddams/RazorLight/issues/365, so I don't know if it's fully resolved yet.

jzabroski commented 3 years ago

@jessicah I merged #365 fix this morning and will release this week. Thank you for your patience.

jzabroski commented 3 years ago

@jessicah Please update to 2.0.0-rc.1 package. Let me know if you run into any issues. Closing for now.