Comments (5)
Thanks for the kind words. And yes, here's hoping we can deprecate this for 6.0!
If you want to try to disable to application in another way there's actually a reasonably simple hook for that. Instead of calling serviceCollection.AddBetterHostedServices()
, you can simply call serviceCollection.AddTransient<IApplicationEnder, YourCoolStrategyForEndingApplications>();
- and then experiment with whatever strategy you think is a good fit.
There's a bit of wonkyness with how .NET awaits the IHostedService (it doesn't) so I actually haven't been able to make it exit less gracefully than with StopApplication
- at least not in test scenarios, but that could be the test server.
If you figure out a good solution, I would love to know it so we can share it among others as well!
from betterhostedservices.
Alright - I have an update!
It turns out that using Environment.Exit()
rather than IHostApplicationLifetime.StopApplication()
does exactly what I was looking for, with the added bonus that a Windows error event is included in the event viewer (not a requirement in my case, but definitely nice!). My only issue is that it exits so quickly that the log buffer isn't getting flushed by the time the process ends, so whatever exception causes the shutdown doesn't get logged (but that's another issue).
using System;
using BetterHostedServices;
public class UncleanApplicationEnder : IApplicationEnder
{
public void ShutDownApplication()
{
// exit uncleanly with code 1
// this should trigger Windows service recovery (if configured in SCM)
Environment.Exit(1);
}
}
Out of curiosity, are you at all interested in having this alternative ApplicationEnder merged into your repo? If so I can add some docs and submit a PR, otherwise I'm fine with just keeping this custom implementation local and I think you can close the issue.
from betterhostedservices.
I would absolutely love it if you'd submit a PR to merge it in and document it (particularly the log-buffer issue)
from betterhostedservices.
Cool! I might have to get to it this weekend, had a lot of other issues crop up today.
One quick question though: in order to deal with my logging issues, maybe we should include an optional "BeforeShutdown" hook in IApplicationEnder
so a function can be fed to it at runtime, which will be called by ShutdownApplication()
before exiting. This will allow people like me to have a custom function for dealing with their log buffer (and do any other tidying up as necessary) before forcing the shutdown.
I can include this hook in the PR along with adding it to your sample program, does that sound good to you or do you have any other thoughts?
from betterhostedservices.
No rush.
I'm not sure whether it's a cleaner API to have a "shutdown" hook or just tell people to subclass the ApplicationEnder, do their own cleanup and then call base.ShutdownApplication()
- I'm okay with whatever API you come up with I think as both are good suggestions.
from betterhostedservices.
Related Issues (18)
- Badges don't work HOT 1
- Fixed failed symbol publishing HOT 2
- Nuget package has build number HOT 1
- Looks like I can use it in my .net generic host HOT 1
- Question about default logging on service crash HOT 2
- Make CriticalBackgroundService.OnError() abstract HOT 5
- Make ensuring TPeriodicTask registered more graceful HOT 1
- Use factory for instantiating TPeriodicTask HOT 1
- Fix CA2254 when logging HOT 1
- Return IServiceCollection from IServiceCollectionExtensions HOT 2
- Re-enable code style analyzers
- Packages on release instead push
- Target the library to .NET 5? HOT 1
- Rewrite tests to remove WebHost context
- Re-enable tests for Windows and MacOS HOT 1
- Readme and AddHostedServiceAsSingleton
- Change .pfx file HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from betterhostedservices.