RazorLight icon indicating copy to clipboard operation
RazorLight copied to clipboard

Multiple instances of IRazorLightEngine

Open reponemec opened this issue 7 years ago • 1 comments

Is it possible to use multiple instances of IRazorLightEngine at the same time in ASP.Net Core application? For example, first instance for embeded resource templates, second for database and third for files. Then, I need access to these engines separately using dependency injection. Currently, I use following construct for creating engine for one of them - embeded resources:

Startup.cs:
...
void ConfigureRazorLight(IServiceCollection services)
        {
            RazorLightEngine engine = new RazorLightEngineBuilder()
                .UseEmbeddedResourcesProject(typeof(Program))
                .UseMemoryCachingProvider()
                .Build();
                //The next line must be used (I use RazorLight 2.0.0-beta1), otherwise an application fails at IRazorLightEngine.CompileRenderAsync if I declare a custom service @inject IMyService in a razor view and try to use its instance!
                services.AddRazorLight(() => engine); 
        }

The problem is that this way I obtain access only for one type of templates (embeded resources).

For info, a source code for AddRazorLight extension method is here.

reponemec avatar Jul 31 '18 09:07 reponemec

I think this can be solved using a custom RazorLight project. Sample here: https://github.com/toddams/RazorLight/tree/dev-2.0/samples/RazorLight.Samples

Maybe this will help also, this one loads templates from multiple assemblies, which is not supported by default https://github.com/toddams/RazorLight/issues/197

szilardd avatar Jul 31 '18 09:07 szilardd