RazorLight
RazorLight copied to clipboard
No effect with 'inject' when use FileSystem Project
Same with #211 , When I use FileSystem Project (UseFilesystemProject(projectPath)
), like this:
var engine = new RazorLightEngineBuilder()
.UseFilesystemProject(projectPath)
.UseMemoryCachingProvider()
.Build();
services.AddRazorLight(() => engine);
and inject in tamplate like this:
@inject IConfiguration configuration
Finally, I got a NullReferenceException, what should I do ? Thanks~
After my debug, I found the engine.Options.PreRenderCallbacks.Count always equal 0, although I use '.Add(t => System.Console.WriteLine("1"))' manually.
Having the same problem with
new RazorLightEngineBuilder()
.UseFilesystemProject(...)
.UseMemoryCachingProvider()
.Build()
.CompileRenderAsync(template, model);
An workaround is to pass the services I want to inject as part of the input model. But ofcourse thats not optimal.
Hi, @toddams
I find this problem, in source code of method AddRazorLight
:
services.AddSingleton<IRazorLightEngine>(p =>
{
var engine = engineFactoryProvider();
AddEngineRenderCallbacks(engine, p);
return engine;
});
Method AddEngineRenderCallBacks
be called when dotnet core injects IRazorLightEngine
, but I don't inject it and make engine
as a prop to other class, and inject other class, like this:
var render = new RazorFileSystemRenderer(templatePath);
builder.Services.AddRazorLight(() => render._engine);
builder.Services.AddSingleton<ITemplateRenderer>(render);
return builder;
@Andrioden Maybe your problem is the same reason.
Has there been any movement on this? I have experienced the same issues where anything I try to inject into a razor view winds up resolving as null. This is a pretty big issue.
@alec-lieberman-ef Workaround would be to inject into the ViewBag instead of your templates directly. In mean time, happy to accept PRs to fix the problem...
After thinking about this, in relation to #317, I think the more basic feedback is people don't want to call Build directly. Why, I'm not sure. I need user stories as to why RazorLightEngineBuilder is not good enough. My take on this is people are using the library in ways it wasn't originally designed for, without explaining why.
@smartlei24 Thanks for all your help explaining this problem.
var render = new RazorFileSystemRenderer(templatePath); builder.Services.AddRazorLight(() => render._engine); builder.Services.AddSingleton<ITemplateRenderer>(render); return builder;
So, I think there may be several red flags here.
One, that I didn't notice before, is "builder.
". I think what builder is matters a lot here. Are you by any chance using Blazor - I found this via searching code snippets for "builder.Services"
Two, why aren't you simply doing the following:
serviceCollection.TryAddSingleton<IRazorLightTemplateRenderer, RazorLightTemplateRenderer>();
serviceCollection.AddRazorLight(() => extractedOutCallToRazorLightEngineFrom_new_RazorLightTemplateRenderer);
and
public class RazorLightTemplateRenderer : IRazorLightTemplateRenderer
{
RazorLightTemplateRenderer(IRazorLightEngine razorLightEngine)
{
// todo
}
}
}