aspnet / Hosting

[Archived] Code for hosting and starting up an ASP.NET Core application. Project moved to https://github.com/aspnet/Extensions and https://github.com/aspnet/AspNetCore
Apache License 2.0
552 stars 312 forks source link

Is there a IApplicationLifetime.ServicesStarted?? #1538

Closed AceHack closed 6 years ago

AceHack commented 6 years ago

I need to fire some code after all the IHostedServices have started. Is there a hook for this or can I somehow be sure my IHostedService will get called last?

Tratcher commented 6 years ago

No, not at the moment. https://github.com/aspnet/Hosting/blob/c9a4ee8b719ffb01eb44705d68efd158eb80c221/src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs#L150-L153

You'd have to track some state in a shared service.

davidfowl commented 6 years ago

We don’t have that event. Today application started fires before hosted services are initialized. When we move to 3.0 we’ll look at restructuring things with the generic host.

For now, if you control the application then just add the hosted service last. There no easy way to guarantee you’re last but it may not matter.

AceHack commented 6 years ago

So I have two types of apps, one uses the generic host IHost because it does not need REST and communicates over Kafka the other has REST so it uses IWebHost, this makes things really difficult because the order is different between the two.

I see in WebHost the IApplicationLifetime.ApplicationStarted gets called before any IHostedServices are called. https://github.com/aspnet/Hosting/blob/release/2.1/src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs#L150

In Host the IApplicationLifetime.ApplicationStarted gets called after all IHostedServices are called. https://github.com/aspnet/Hosting/blob/release/2.1/src/Microsoft.Extensions.Hosting/Internal/Host.cs#L51

This seems very inconsistent, can I expect this behavior to change in the future to be more consistent?

What I'm trying to accomplish is to publish metrics to Prometheus of all loaded assemblies and their version number but I want to make sure the application has fully loaded since it does dynamic loading of dlls inside some of the IHostedServices. Do you know of any better place to hook into? I don't have full control of the application, I'm just writing the metrics framework that multiple other teams are consuming in their microservices, they make a call to hook metrics into their DI container.

davidfowl commented 6 years ago

We can look at changing the web host to fire the event after hosted services but that’s a breaking change. We’ll see what it looks like and decide if it’s ok for 2.2 or 3.0.

I’m not sure if you can do much else. All of the workarounds I have are borderline awful and hacky 😬

AceHack commented 6 years ago

Thank you for your help!! If you change asp.net core logic itself to be an IHostedService then that would inherently change the order correct?

I only ask because I saw you mention doing that here https://github.com/aspnet/Hosting/issues/1085