Skip to content

Commit

Permalink
Exclude all IHostedService implementations except GenericWebHostServi…
Browse files Browse the repository at this point in the history
…ce because it Configures the pipeline. (#4523)

By removing the IHostedService implementations it will stop running these when starting the app which happens with the WebApplication builder.
  • Loading branch information
desjoerd committed Sep 28, 2023
1 parent 81db67d commit 5d4167d
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/NSwag.Commands/HostApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ void ConfigureHostBuilder(object hostBuilder)
{
services.AddSingleton<IServer, NoopServer>();
services.AddSingleton<IHostLifetime, NoopHostLifetime>();
for (var i = services.Count - 1; i >= 0; i--)
{
// exclude all implementations of IHostedService
// except Microsoft.AspNetCore.Hosting.GenericWebHostService because that one will build/configure
// the WebApplication/Middleware pipeline in the case of the GenericWebHostBuilder.
if (typeof(IHostedService).IsAssignableFrom(services[i].ServiceType)
&& services[i].ImplementationType is not { FullName: "Microsoft.AspNetCore.Hosting.GenericWebHostService" })
{
services.RemoveAt(i);
}
}
});
}

Expand Down

0 comments on commit 5d4167d

Please sign in to comment.