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.

No effect with 'inject' when use FileSystem Project

See original GitHub issue

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~

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
smartlei24commented, Mar 21, 2019

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.

0reactions
jzabroskicommented, Jun 30, 2020

@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
  }
}
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

When is it not appropriate to use the dependency injection ...
Basically, dependency injection makes some (usually but not always valid) assumptions about the nature of your objects. If those are wrong, ...
Read more >
Criticism and disadvantages of dependency injection
So you might decide to inject the external service and the file system as interface dependencies, which will allow you to inject mocks...
Read more >
Laravel dependency injection - bindings
I've noticed that when calling the exists method on the injected $filesystem object with a directory that does not exist, the condition ...
Read more >
Make it easier to inject the FileSystem service [#3048126]
We have quite a few places where it is problematic to inject the file system service, due to either direct or indirect circular...
Read more >
5 Reasons to Use Dependency Injection in Your Code
Because of the externally injected dependencies, developers can scale up the application without worrying about managing dependencies manually ...
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