Aspnetcore.docs: Update Background tasks article for 3.0 (Generic Host)

Created on 12 Jun 2019  ยท  9Comments  ยท  Source: dotnet/AspNetCore.Docs

Migrate sample app to 3.0 (Generic Host) and put snippets in 2.2 vs. 3.0 moniker blocks


Document Details

โš  Do not edit this section. It is required for docs.microsoft.com โžŸ GitHub issue linking.

P1

Most helpful comment

What about the (somewhat crappy) Generic Host version for 2.x. That's probably not such a good thing to have. It isn't tied into the topic. It was just a show 'n tell from the early Generic Host days. Shall I kill that thing off?

Nevermind ... rubber :duck: says to slay ๐Ÿ”ช that beast ๐Ÿ‘น!

All 9 comments

Confirm and enable worker service content for VS4Mac ...

# [Visual Studio for Mac](#tab/visual-studio-mac)

1. Create a new project with **File** > **New Solution**.
1. Select **App** under **.NET Core** in the sidebar.
1. Select **Worker** under **ASP.NET Core**. Select **Next**.
1. Select **.NET Core 3.0** for the **Target Framework**. Select **Next**.
1. Provide a name in the **Project Name** field. Select **Create**.

@davidfowl @anurse The hosted service topic has separate 2.x samples: Web Host and Generic Host.

โ“ Would you like the 3.x updates to include a 3.x Web Host sample, or should I drop that sample and only have a 3.x Generic Host sample going forward?

... and a follow-up is that since the Generic Host is a bit different from 2.x days, should we even keep that example in the 2.x version of the topic? The 2.x version really only relies on the Web Host sample app for its code and explanations.

Would you like the 3.x updates to include a 3.x Web Host sample, or should I drop that sample and only have a 3.x Generic Host sample going forward?

I think we should drop the Web Host sample from 3.x. It's deprecated.

What about the (somewhat crappy) Generic Host version for 2.x. That's probably not such a good thing to have. It isn't tied into the topic. It was just a show 'n tell from the early Generic Host days. Shall I kill that thing off?

Nevermind ... rubber :duck: says to slay ๐Ÿ”ช that beast ๐Ÿ‘น!

@anurse I have a version of the sample app running. Would you plz take a look and help me with a few questions/concerns?

I put it up here :point_right: https://github.com/guardrex/BackgroundTasks30

I based that on the Worker Service template.

  1. It used to be that only the Queued Hosted Service example relied on the BackgroundService class (in the 2.x sample). Now, I'm trying a version where all three hosted services use it. Is what I did appropriate, or should the Timed and Scoped hosted service examples go back to being based directly on IHostedService?
  2. If it is fine to use BackgroundService for all of the examples, did I _mishandle those tokens_ in ExecuteAsync calls or elsewhere? How about the way I shot a token into the scoped service? Is that valid?
  3. The app seems to run fine ... it has the correct runtime behaviors. Do u see anything else that I botched? ๐Ÿ™ˆ
  4. Almost seems to run fine ... there is one tiny nit: When running the app in the VS Code external terminal, the Ctrl+C won't stop it. When running it from a command shell, Ctrl+C works as expected. Any idea why? What I plan to do right now is just tell the reader in the README.md file to run the sample with dotnet run. (I still don't like that it doesn't work properly from VSC external terminal tho.)
  5. [REMOVED]

@guardrex

For # 5, I believe the issue is that the VideoWatcher class is trying to get the path to the content root directory which is set in Startup.cs as key/value data on the AppDomain. It would be simpler to just inject the IHostEnvironment in the constructor and read the ContentRootPath directly instead.

@manigandham Ah, yes ... I see what you mean. Thanks.

  1. It used to be that only the Queued Hosted Service example relied on the BackgroundService class (in the 2.x sample). Now, I'm trying a version where all three hosted services use it. Is what I did appropriate, or should the Timed and Scoped hosted service examples go back to being based directly on IHostedService?

I think it's probably better for TimedHostedService to directly implement IHostedService. It's not really a match for the BackgroundService model. BackgroundService assumes that ExecuteAsync will be the lifetime of the service, and the CancellationToken is the signal that StopAsync was called. In this case, the ExecuteAsync method isn't really doing anything, the timer is doing all the work. Also, you should ensure the Timer is Disposed.

2. If it is fine to use BackgroundService for all of the examples, did I _mishandle those tokens_ in ExecuteAsync calls or elsewhere? How about the way I shot a token into the scoped service? Is that valid?

For ConsumeScopedServiceHostedService you should have the scoped processing service be async and have the tasks awaited all the way up through ExecuteAsync instead of awaiting Task.CompletedTask at the end. Also, instead of overriding StopAsync, you can just try-finally or try-catch(OperationCancelledException) and write a "Service is stopping" message there. The whole idea of ExecuteAsync is that it lets you write your service "linearly". The cancellation token will be signalled when you stop and you can use existing primitives like try-catch-finally to handle the exit path (since generally, code will throw OperationCancelledException if the CT is cancelled). I'd use try-finally when you're logging though because whatever happens, the end of the method is the end of the service :).

3. The app seems to run fine ... it has the correct runtime behaviors. Do u see anything else that I botched? ๐Ÿ™ˆ

My comments above aren't really functional, so it makes sense that it would work. It looks correct from a runtime perspective, just a touch odd in the way the BackgroundService lifetimes are done.

4. When running the app in the VS Code external terminal, the Ctrl+C won't stop it. When running it from a command shell, Ctrl+C works as expected. Any idea why?

Sounds like a VS Code issue to be honest, if the real shell works. I don't have any specific thoughts.

Thanks @anurse ... I'll react to your feedback.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Rick-Anderson picture Rick-Anderson  ยท  3Comments

Rick-Anderson picture Rick-Anderson  ยท  3Comments

AnthonyMastrean picture AnthonyMastrean  ยท  3Comments

danroth27 picture danroth27  ยท  3Comments

royshouvik picture royshouvik  ยท  3Comments