RazorLight icon indicating copy to clipboard operation
RazorLight copied to clipboard

No effect with 'inject' when use FileSystem Project

Open smartlei24 opened this issue 6 years ago • 7 comments

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~

smartlei24 avatar Jan 22 '19 07:01 smartlei24

After my debug, I found the engine.Options.PreRenderCallbacks.Count always equal 0, although I use '.Add(t => System.Console.WriteLine("1"))' manually.

smartlei24 avatar Jan 22 '19 10:01 smartlei24

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.

Andrioden avatar Mar 13 '19 04:03 Andrioden

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.

smartlei24 avatar Mar 21 '19 00:03 smartlei24

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 avatar Nov 11 '19 10:11 alec-lieberman-ef

@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...

jzabroski avatar Dec 19 '19 20:12 jzabroski

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.

jzabroski avatar Mar 26 '20 01:03 jzabroski

@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
  }
}
}

jzabroski avatar Jun 30 '20 03:06 jzabroski