I was migrating an ASP.NET Core App to the Worker Service template and was intending to keep the Startup code.
However after
within Program I kept the CreateHostBuilder as described on the MS Docs:
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureWebHostDefaults(
webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.ConfigureServices(
services =>
{
services.AddHostedService<Worker>();
});
while debugging, the ConfigureServices is being called
public void ConfigureServices(IServiceCollection services)
but the Configure within the Startup, is not reached / called
public void Configure(IApplicationBuilder app)
before it crashes calling Run().
I also tried this with the same result:
public static IHostBuilder CreateHostBuilder(string[] args)
=> Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureWebHostDefaults(webBuilder => webBuilder.Configure(Startup.Configure))
.ConfigureServices(
services =>
{
Startup.ConfigureServices(services);
services.AddHostedService<Worker>();
});
The interesseting part is that the following code actually calls the Startup.Configure(IApplicationBuilder app):
public static IHostBuilder CreateHostBuilder(string[] args)
=> Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureWebHostDefaults(webBuilder => webBuilder.Configure(Startup.Configure));
As soon as I am adding ConfigureServices it skips the IApplicationBuilder configuration call.
Am I missing something or what is the suggested way to achieve this?
Edit:
the exact error is:
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Builder.IApplicationBuilder' while attempting to activate 'Kledex.Extensions.KledexAppBuilder'.
stack trace:
at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable'1 serviceDescriptors, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder) at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter'1.CreateServiceProvider(Object containerBuilder) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build()
the error happens as soon as it reaches CreateHostBuilder(args).Build().Run(); and tries to resolve the registered services, while the above one has a dependency to some config app.UseSomething(); within the Startup.Configure() method.
A breakpoint in Startup.Configure() doesn't get hit.

Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env)is never called, even when requested.public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args).UseStartup<Startup>().UseIISIntegration(). As such there must be a difference in the lifecycle of the Builder or the way it needs to be configured.