Skip to content
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 Background tasks article for 3.0 (Generic Host) #12841

Closed
tdykstra opened this issue Jun 12, 2019 — with docs.microsoft.com · 9 comments · Fixed by #14417
Closed

Update Background tasks article for 3.0 (Generic Host) #12841

tdykstra opened this issue Jun 12, 2019 — with docs.microsoft.com · 9 comments · Fixed by #14417
Assignees
Labels
Milestone

Comments

Copy link
Contributor

tdykstra commented Jun 12, 2019

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.

@dotnet-bot dotnet-bot added the Source - Docs.ms Docs Customer feedback via GitHub Issue label Jun 12, 2019
@guardrex guardrex removed the Source - Docs.ms Docs Customer feedback via GitHub Issue label Jun 12, 2019
@tdykstra tdykstra added this to the Backlog milestone Jun 17, 2019
@tdykstra tdykstra changed the title Update Background tasks article for 3.0 Update Background tasks article for 3.0 (Generic Host) Jun 26, 2019
@guardrex guardrex self-assigned this Jul 16, 2019
@guardrex guardrex modified the milestones: Backlog, 3.0 Aug 6, 2019
@guardrex
Copy link
Collaborator

guardrex commented Sep 15, 2019

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**.

@guardrex
Copy link
Collaborator

guardrex commented Sep 18, 2019

@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.

@analogrelay
Copy link
Contributor

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.

@guardrex
Copy link
Collaborator

guardrex commented Sep 18, 2019

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 🦆 says to slay 🔪 that beast 👹!

@guardrex
Copy link
Collaborator

guardrex commented Sep 18, 2019

@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 👉 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]

@manigandham
Copy link
Contributor

manigandham commented Sep 19, 2019

@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.

@guardrex
Copy link
Collaborator

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

@analogrelay
Copy link
Contributor

  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.

@guardrex
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants