NullReferenceException when @inject directive is used in built-in FilesystemProject (RazorLightEngineBuilder.UseFilesystemProject)
See original GitHub issueIf I use
void ConfigureRazorLight(IServiceCollection services)
{
services.AddSingleton<IMyService, MyService>();
RazorLightEngine engine = new RazorLightEngineBuilder()
.UseFilesystemProject(_hostingEnvironment.ContentRootPath)
.UseMemoryCachingProvider()
.Build();
services.AddRazorLight(() => engine);
}
And if I have a
@inject IMyService _myService
directive in Razor view then _myService is always compiled as null. In case of built-in embeded resources (.UseEmbeddedResourcesProject(typeof(Program))) it works fine.
I don’t know whether it is a bug or something is wrong in my code.
Issue Analytics
- State:
- Created 5 years ago
- Comments:10
Top Results From Across the Web
NullReferenceException when @inject directive is used in built- ...
NullReferenceException when @inject directive is used in built-in FilesystemProject (RazorLightEngineBuilder.UseFilesystemProject)
Read more >RazorLight
RazorLight is throwing the following exception when running engine. ... NullReferenceException when @inject directive is used in built-in FilesystemProject ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For the record, I experienced the same issue. The engine was build as follow (i.e. not with some dependency injection).
Adding the following two lines below the snippet fixed the issue:
Sorry about that, I’m using FluentEmail (https://github.com/lukencode/FluentEmail,) which uses RazorLight underneath. For my DI configuration I’m using
Not sure if that helps or not.
I think the issue is related to this ticket: https://github.com/toddams/RazorLight/issues/166 so I think I can work around it Thanks for creating a great library much appreciated