aspnet / Hosting

[Archived] Code for hosting and starting up an ASP.NET Core application. Project moved to https://github.com/aspnet/Extensions and https://github.com/aspnet/AspNetCore

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RunAsService does not block after a change of host

DragosFlorea opened this issue · comments

I'm working on .net core 2.1 environment.
I have selfhosted RestApi(kestrel) which is installed as a service. I want to change the port of the restapi on run-time. The service starts correctly on port "xxxx", but the problem is that when i recreate the host with the new port it doesn't blocks on RunAsService, it doesn't throw an error, it simply run over.
The steps behind:
0-start the restapi service
1-call an api function with the new port as parameter
2-stop the current host (I've seen that when i'm attached to process, after this step, if i'm stopped in breakepoint, after a while the service stops itself)
3-create the new host with the new port
4-trying to runasservice the new host,but it does not blocks here

This scenario works fine in console, but as a service it does not work. I'm not sure if there is some configuration in windows to the service that i miss or i'm doing something wrong in code

RunAsService uses ServiceBase, which associates the lifetime of the host with the lifetime of the process. This is likely the part that breaks you:

.GetRequiredService<IApplicationLifetime>()
.ApplicationStopped
.Register(() =>
{
if (!_stopRequestedByWindows)
{
Stop();
}
});

You would need to redesign that code to suit your needs for restart. That or persit the new settings to disk and restart the process.

One alternative you could use is HttpSysServer which supports changing the server addresses without restarting: https://github.com/aspnet/HttpSysServer/blob/7e2debe46b65f37f2ad9dc5e4d91bf1f9de846d2/samples/HotAddSample/Startup.cs

We periodically close 'discussion' issues that have not been updated in a long period of time.

We apologize if this causes any inconvenience. We ask that if you are still encountering an issue, please log a new issue with updated information and we will investigate.