.NET Core 3.0 introduced a new WorkerService template that can be used as a starting point for long running service apps. As the worker service template didn’t use a Startup.cs file like in a traditional ASP.NET Core application, it wasn’t immediately obvious to me where I had to configure my IoC container.
So a quick tip for my future self, you can do this using the ConfigureContainer() method on the HostBuilder:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Host.CreateDefaultBuilder(args) | |
.UseServiceProviderFactory(new AutofacServiceProviderFactory()) | |
.ConfigureServices((hostContext, services) => | |
{ | |
services.AddHostedService<Worker>(); | |
}) | |
.ConfigureContainer<ContainerBuilder>((hostContext,builder)=> { | |
//Configure your container here | |
}); |
Remark: The example above is using Autofac but the approach is similar for other containers.