RazorLight: Does not work with generic host

Describe the bug InvalidOperationException: Unable to resolve service for type ‘RazorLight.RazorLightOptions’ while attempting to activate ‘RazorLight.EngineHandler’.

To Reproduce Steps to reproduce the behavior: Create a new ASP.NET Core 3.1 MVC application with the new generic host template

 public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }

Add RazorLight to the services:

  public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            services.AddRazorLight(() =>
  new RazorLightEngineBuilder()
                   .UseEmbeddedResourcesProject(typeof(Startup)) // exception without this (or another project type)
                   .UseMemoryCachingProvider()
                   .Build()
            );
        }

Exception: InvalidOperationException: Unable to resolve service for type 'RazorLight.RazorLightOptions' while attempting to activate 'RazorLight.EngineHandler'.

Expected behavior Runs smoothly.

The problem is that the RazorLightOptions are not registered which are required by the EngineHandler.

Workaround Register missing dependencies separately

public void ConfigureServices(IServiceCollection services)
        {
 var engine = new RazorLightEngineBuilder()
                   .UseEmbeddedResourcesProject(typeof(Startup)) // exception without this (or another project type)
                   .UseMemoryCachingProvider()
                   .Build();

            services.AddRazorLight(() => engine);
            services.AddSingleton(engine.Options);
            services.AddSingleton(engine.Handler.Compiler);
            services.AddSingleton(engine.Handler.FactoryProvider);
            services.AddSingleton(engine.Handler.Cache);
}

Information (please complete the following information):

  • Version 2.0 Beta4

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 6
  • Comments: 15

Commits related to this issue

Most upvoted comments

@jzabroski You need to setup the service provider to validate on build like this:

            static IHostBuilder CreateHostBuilder(string[] args)
            {
                return Host.CreateDefaultBuilder(args).UseDefaultServiceProvider((context, options) =>
                {
                    options.ValidateOnBuild = true;
                }).ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<EmbeddedEngineStartup>(); });
            }

Workaround Register missing dependencies separately

public void ConfigureServices(IServiceCollection services)
        {
 var engine = new RazorLightEngineBuilder()
                   .UseEmbeddedResourcesProject(typeof(Startup)) // exception without this (or another project type)
                   .UseMemoryCachingProvider()
                   .Build();

            services.AddRazorLight(() => engine);
            services.AddSingleton(engine.Options);
            services.AddSingleton(engine.Handler.Compiler);
            services.AddSingleton(engine.Handler.FactoryProvider);
            services.AddSingleton(engine.Handler.Cache);
}

This workaround fixes it for me too.