Description
Type of issue
Missing information
Description
Hi!
I'm currently building an Azure Container App Job. I've built these as Console Apps before, but I'm now looking into using Worker Service instead as they set up the generic host and other goodness for me out of the box.
I need the app to start up, run some code, and then exit, so the process isn't long-lived. This is easy with the Console App; when it runs out of code to run, it just exits ;). It might be that I shouldn't be using worker services in my scenario, but I think it's a good question to ask anyway!
Like the documentation says, you need to stop the host by Signalling completion in a Worker Service. However, this will end the application with a succes code.
My question: How do you stop a host from inside a hosted service and signal that we're exiting because of an error?
For example, we're running some code in the Hosted Service and something goes wrong. Naturally, I want to exit with an exit code. However, HostApplicationLifetime.StopApplication();
doesn't support this.
There are other ways (Environment.Exit(1);
comes to mind) but I am not sure if this is recommended.
Therefore, some more information about exiting a worker service with an error code from inside a hosted service would be great!
Page URL
https://learn.microsoft.com/en-us/dotnet/core/extensions/workers
Content source URL
https://github.com/dotnet/docs/blob/main/docs/core/extensions/workers.md
Document Version Independent Id
ff379d26-0d85-6d74-67da-4a3cd260b04d
Platform Id
4a2c295f-6871-5e01-01ea-ec1779c4558c
Article author
Metadata
- ID: ae847f16-ff74-ab68-7163-f713b447430c
- PlatformId: 4a2c295f-6871-5e01-01ea-ec1779c4558c
- Service: dotnet-fundamentals