Hosted services start later with minimal API
See original GitHub issueI’ve found another problem with the new minimal API introduced in .NET 6 which I haven’t seen announced. Hosting services appear to start later than they did before. Here’s a sample application which demonstrates the problem:
Program.cs
using Microsoft.AspNetCore.Mvc;
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(builder => {
builder.ConfigureServices((context, services) => services.AddServices())
.Configure(app => app.UseMiddleware());
})
.Build()
.Run();
//var builder = WebApplication.CreateBuilder(args);
//// Add services to the container.
//builder.Services.AddServices();
//var app = builder.Build();
//app.UseMiddleware();
//app.Run();
public static class ServiceCollectionExtensions {
public static IServiceCollection AddServices(this IServiceCollection services) {
services.AddOptions<MvcOptions>().Configure(options => {
});
services.AddHostedService<TestHostedService>();
services.AddControllers();
return services;
}
}
public static class ApplicationBuilderExtensions {
public static IApplicationBuilder UseMiddleware(this IApplicationBuilder app) {
return app.UseRouting()
.UseEndpoints(endpoints => endpoints.MapControllers());
}
}
public class TestHostedService : IHostedService {
public Task StartAsync(CancellationToken cancellationToken) {
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
If you put a breakpoint between the Configure
callback (line 25) and the hosted service StartAsync
method (line 43) and run the application you will see that the StartAsync
method runs first. However if you comment out the old startup code (lines 3 to 9) and un-comment the new code (lines 11 to 20) you will notice the breakpoints are hit the other way around. This causes a problem to me as I need the hosted service to run first. Is there a way to fix this using the minimal API?
Issue Analytics
- State:
- Created 2 years ago
- Reactions:7
- Comments:12 (8 by maintainers)
Top Results From Across the Web
Waiting for your ASP.NET Core app to be ready from an ...
In this post I described how to wait in a BackgroundService / IHostedService for your ASP.NET Core application to finish starting, so you...
Read more >Background tasks with hosted services in ASP.NET Core
In ASP.NET Core, background tasks can be implemented as hosted services. A hosted service is a class with background task logic that ...
Read more >c# - Start IHostedService after Configure()
I want the /status endpoint to start running before the HostedService kicks off. How do i start the endpoint before the Hosted Service?...
Read more >Concurrent Hosted Service Start and Stop in .NET 8
In this post, I demonstrate starting and stopping hosted services concurrently in .NET 8 using newly available HostOptions.
Read more >A Complete Guide to Hosted Service(s) in .NET 6 using C# 10
A Complete Guide to Background Worker Service(s) in .NET 6 using C# 10. It explains the Hosted Service LifeTime, Start and Stop Behavior, ......
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 Free
Top 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
Thanks for contacting us. We’re moving this issue to the
.NET 8 Planning
milestone for future evaluation / consideration. Because it’s not immediately obvious that this is a bug in our framework, we would like to keep this around to collect more feedback, which can later help us determine the impact of it. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it’s very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.OK great. I’m going to go ahead and close this. Whilst this is a change from the previous hosting model I think that this represents the best approach for folks that need to perf actions synchronously before the application actually starts.
I always thought that folks using hosted services to race to do things before the app starts vs. just invoking the specific operation you want to do was unnecessary complexity.