-
Notifications
You must be signed in to change notification settings - Fork 311
Third way of making configurable how hosting is kept alive #255
Conversation
@@ -90,6 +90,7 @@ private void EnsureApplicationServices() | |||
{ | |||
EnsureStartup(); | |||
|
|||
_applicationServiceCollection.AddSingleton<IHostingKeepAlive, ConsoleHostingKeepAlive>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implementation should be registered in Program.cs. Helios (the IIS layer) also uses this library.
3c33d74
to
4316b4f
Compare
I have updated my sample code |
I don't really like the name of the interface If the implementation doesn't call one of the termination methods, then the code will keep running forever. It would be a keep alive contract if it would block or return a cancellation token. |
appShutdownService.RequestShutdown(); | ||
}); | ||
foreach (var hostingKeepAlive in hostingKeepAlives) | ||
hostingKeepAlive.Setup(appShutdownService); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Coding style: always use
{ }
- This approach doesn't allow me to overwrite the default behavior and that's a critical scenario for the Nano server
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well we actually do have this pattern already today with multiservices, its not great but you can override the default behavior by removing the IHostingKeepAliveService and adding your own.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what I meant by IServiceCollection.Replace
. There is an extension method for that in DependencyInjection.
|
|
Isn't it weird that the "shutdown" handler writes "started"? 😄 |
Hehe. See my samples for some better texts. It should be something along the lines of |
IShutdownManager might not be bad either, since its his job to request shutdown... |
Well, then all the way: |
Ok, then my vote is back to |
Huh, cla-required? |
@Tragetaschen We just changed the repos from MS Open Tech to .NET Foundation so there's a different CLA. The new CLA is simpler than the old one and so should only take a few moments to re-sign. Apologies for the extra bit of work! |
deafb77
to
bc87bbf
Compare
I have submitted the .NET Foundation's CLA, but @dnfclas doesn't recognize it yet. Maybe I did something wrong? @danroth27 |
bc87bbf
to
d6d2c0f
Compare
Back to topic: Yes, indeed, However, I'm having a harder time with the method name. I tried to write the API documentation when the name was |
Perhaps something like: "This method is responsible for handling shutdown and eventually calling RequestShutdown." |
After thinking through this a bit I'd (and after a discussion with @victorhurdugaci), I'm wondering why we need any of this at all? We can just always wait on the reset event and nothing else. If you want to setup something then just do it in your Configure method. Hook whatever it is you need and then call RequestShutdown at the appropriate time. |
And an |
We want a default behavior that allows the server to shutdown gracefully, but that behavior has to be override-able. |
@Tragetaschen, Thanks for signing the contribution license agreement so quickly! Actual humans will now validate the agreement and then evaluate the PR. |
I actually think is the best approach. What happens when you do ctrl + c right now? |
This ends up calling the equivalent of |
@Tragetaschen so as it is right now, things will hang for kestrel without the graceful unwind? We could just add the control c handling then. |
The main problem with On the other hand, I don't think it's necessary to overriding/replace a default CTRL+C handler when Hosting registers it. You would only need this, if you don't want the process to shutdown and that's quite counter-intuitive when actually running on a terminal. (dang, you beat me with your comment) |
@Tragetaschen add that to your change |
64f16dc
to
2e8c301
Compare
The |
2e8c301
to
19fc9fb
Compare
Looks like @ellismg didn't update the reference assembly. |
I made a fix to the reference assembly, you should see it in the next build. |
cee0a48
to
fcf4573
Compare
4.0.0-beta-22910 has landed and includes 👏 😄 |
Don't require a TTY on Unix.
fcf4573
to
95b1997
Compare
Third way of making configurable how hosting is kept alive
How do you overwrite the Ctrl+C behavior? |
Why would you want to do that? As discussed above, when not running in a console environment, the registered handler is never called and you can still implement any method you want to call If you are running in a console environment, that's an interactive environment and properly reacting to CTRL+C like here is a genuinely good idea. Still, I might be missing something, but right now I think a good answer to your question is: You don't need to. |
I have tried to incorporate the comments on the other two approaches (#129 and #253) to tackle the problem from yet another angle.
This now entirely relies on
IApplicationShutdown
as the method of communication and instead of going back and forth between Program and the keep-alive implementation, the latter is now responsible for doing whatever is required to callRequestShutdown()
. For things likeConsole.CancelKeyPressed
, this is the simplest event handler imaginable.Additionally, it's now possible to register any number of
IHostingKeepAlive
implementations and any one can callRequestShutdown()
independently.The default implementation can be
IServiceCollection.Replace
d, but keep in mind the open issue and discussion from aspnet/DependencyInjection#155.As a bonus, the implementation in Program.cs is now as simple as it can get.
@lodejard @davidfowl @victorhurdugaci @HaoK