question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NullReferenceException when @inject directive is used in built-in FilesystemProject (RazorLightEngineBuilder.UseFilesystemProject)

See original GitHub issue

If 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:open
  • Created 5 years ago
  • Comments:10

github_iconTop GitHub Comments

2reactions
ancailliaucommented, Jun 26, 2023

For the record, I experienced the same issue. The engine was build as follow (i.e. not with some dependency injection).

var engine = new CustomRazorLightEngineBuilder()
  .UseFileSystemProject(Path.Combine(Directory.GetCurrentDirectory(), "Views"))
    .UseMemoryCachingProvider()
    .Build();

Adding the following two lines below the snippet fixed the issue:

var injector = new PropertyInjector(_serviceProvider);
engine.Options.PreRenderCallbacks.Add(template => injector.Inject(template));
0reactions
dotnetshadowcommented, Jan 31, 2021

Sorry about that, I’m using FluentEmail (https://github.com/lukencode/FluentEmail,) which uses RazorLight underneath. For my DI configuration I’m using

var emailSettings = config.GetSection("EmailSettings").Get<AuthMessageSenderOptions>();

            var smtpClientOptions = new SmtpClientOptions
            {
                UseSsl = emailSettings.UseSsl,
                Password = emailSettings.Password,
                Port = emailSettings.Port,
                Server = emailSettings.Server,
                User = emailSettings.Username,
                UsePickupDirectory = false,
                RequiresAuthentication = !string.IsNullOrEmpty(emailSettings.Username),
                SocketOptions = SecureSocketOptions.StartTlsWhenAvailable
            };
            
            services.AddFluentEmail(emailSettings.SystemEmailFrom, emailSettings.SystemEmailFromName)
                .AddRazorRenderer()
                .AddMailKitSender(smtpClientOptions);

services..AddTransient<IEmailSender, EmailService>();

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found