New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update hosted service topic #3352
Comments
@danroth27 which dev should we work with? |
@muratg Who can help us out for this doc? |
@muratg can we get a sample doing this? |
@Rick-Anderson Chris can help with it after Preview2 work winds down. |
cc @Tratcher |
@Tratcher can you come up with a sample app? |
|
@Tratcher Thanks. I plugged your code into the Razor Pages project. I'd like to use the My sample is here - can you show me how to cancel? Should I set up a meeting with you or @muratg or both on the outline of the article? |
Fowler abandoned that approach, it has too many threading and cancellation issues. I don't know that cancellation from the About page makes sense. These services have the same lifetime as your application, you don't spin them up and down dynamically. You could communicate with the background service from the About page though to do things like change the output message. Inject a shared singleton dependency like MyBackgroundServiceData that holds the message to write into both the background service and the about page. |
We should discuss what we want to document. Right now in the box is an extremely low level building block API. 2.1 will have more goodies that make it easier to implement common scenarios but showing something like a timer would be good. We should also discuss some issues that will be extremely common like doing DI in one of these services. One big issues people will end up having to deal with with is scoped objects (like a db context). When you using this API we'll activate your service once at startup and never again. You'll need to manually create scopes during execution to do anything with say your db context in the timer callback. |
@Rick-Anderson let's get some time on the calendar then with the right folks |
@danroth27 who are the right folks for the meeting? |
Probably @davidfowl and @Tratcher and whoever else they think should be there. |
Here's a sample for consuming scoped services in a hosted service:
I owe you one more sample for queuing background tasks. |
@davidfowl how's this?
|
@Tratcher I hacked up a version of the Hosted Services topic sample (HostedServicesTopicSample) that runs on the Generic Host (the usual 🔪 Dino Hacks™️ 🔪 ... u know the drill! 😀) . A few questions ...
This last bit pertains to the overall planning for this topic and the docs work on Generic Host in general:
|
I agree that we should get the basic docs up for Generic Host and then come back and sort out the samples. |
Nevermind ... "Until 3.0 we'll need separate samples." ... interpreted as 'yes' ... we'll add the sample now. |
Yes, do add the sample, but get the basic docs done first. |
The new (draft) sample has been updated for use when we get back to this issue: HostedServicesTopicSample. Console bits added to deal with enqueuing background work items can be seen in the Program.cs file ... https://github.com/guardrex/HostedServicesTopicSample/blob/master/Program.cs We'll need to address the terminal choice for running the sample because the keystroke capture fails in a redirected console (e.g., such as with VSC's |
@guardrex did we add the call to the new |
@davidfowl It's in the Generic Host topic: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-2.1#configureservices ... and it appears in the Generic Host sample of this topic (the background services topic). We planned to have the background services topic text call it out when the Generic Host takes over. |
See Document IHostStartup (host lightup) #4438
To do background tasks in ASP.NET Core you use IHostedService. We should document that.
meeting with chris/fowler
Notes;
Low level component for running long running tasks.
Runs a jobs once at startup and runs for the life of the app. If you want to get scoped dependencies - you need to create a scope per invocation.
Not something you create/destroy while app is running. It's not QBWI - although it is designed for long running processes.
Sample goes with controller, not RP. RP are for UI.
14:30 IHostedService jobs don't run in parallel with application.
StartAsync
should return quickly so it doesn't block the web app from starting.return Task.CompletedTask;
needs to return when the task is started, not when the task is completed. So you can't return a task of a long running operation inStartAsync
.The text was updated successfully, but these errors were encountered: