.NET Generic Host Writing "Application Started" when its done with its work
So I have been playing around with the Generic Host (Microsoft.Hosting.Extensions and HostedServices.
So I have this output, where "Application started" comes after my service have stopped working.
I am not sure about how, I should structure the code to get the "Application Started" before my service runs.
I am currently using StartAsync, but gets the same behaviour with RunAsync
StartAsync can be seen here: https://source.dot.net/#Microsoft.Extensions.Hosting/Internal/Host.cs,57
My Output
My Code
15 Replies
I should structure the code to get the "Application Started" before my service runswhy would you want that?
Because currently my services prints that it starts, just as it completes
well it does nothing 😐 apart from waiting 1 sec
so again why do you care that your service is started before or after "Application Started"?
The Thread.Sleep could be swapped out with some real work, like fetching data from an API or something
ps it shouldn't be Thread.Sleep but await Task.Delay
why do you care when this happens
are there dependencies?
I am mainly interested how it works, as I could get it working without using a HostedService or even the GenericHost.
So I mainly care, because I think the startup messages (printing how to quick, what enviroment its running in and rootpath is good information)
While I could make the startup-message myself, it would feel stupid, to have:
Startup-message
"Work-being-done"-messages
Startup-message
Closing-message
i'm sorry i really don't get what's the issue (also because you didn't finish you phrase up there)
StartAsync is called when the application starts
you can log stuff in there if you want to
what more do you want
like are you just saying that you want your logs after the startup messages
hosted services traditionally dont do imperative work once then finish, which is what the code you have shown above does
the whole idea of a service is that its a long running thing that has a distinct start and stop phase, normally starting a server or process that does something, perhaps on a timer.
so the reason you are getting "unsual" behaviour is that you are essentially missusing the IHostedService structure
Which structure would/should I use instead?
well if you just want a program that runs its code once then terminates, you dont need generic host
just a normal console app. You could use the hostbuilder to get config/DI setup for you, but then dont start the host, just resolve the actual application class you make instead
remember, generic host is for hosting long living processes
not running a one-and-done process
Yeah, is using it for config/DI
I personally roll my own configbuilder + DI setup for simple console apps, but I can see the appeal in having it "done for you"
something like this
Thanks for the help
or after having configured services get an ILogger and log what you need
but application startup class would be better
yeah you can inject whatever you want into
MyActualApplicationClass
here
a logger, a config object, whatevs