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.

Does not work with generic host

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
edvinklaebocommented, Nov 24, 2020

@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>(); });
            }
1reaction
sibeliuzcommented, Sep 9, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NET Generic Host
Learn about the .NET Generic Host, which is responsible for app startup and lifetime management.
Read more >
NET Generic Host in ASP.NET Core
Generic Host is responsible for app startup and lifetime management. ... The colon ( : ) separator doesn't work with environment variable ......
Read more >
UseEnvironment does not work when using generic host ...
UseEnvironment does not work when using generic host based webhost #18499 ; area-hosting Includes Hosting ; area-runtime Includes: Azure, Caching, ...
Read more >
C# - .NET 6 - Console app with Generic Host vs without
1 Answer 1 ... The benefits of using the generic host is that by default a lot of services are already setup for...
Read more >
Building a Console App with .NET Generic Host | David's Blog
NET Generic Host is a feature which sets up some convenient patterns for an application including those for dependency injection (DI), logging, ...
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