Does not work with generic host
See original GitHub issueDescribe 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:
- Created 4 years ago
- Reactions:6
- Comments:15
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jzabroski You need to setup the service provider to validate on build like this:
This workaround fixes it for me too.