feO2x
feO2x

Reputation: 5728

Update configuration and restart self-hosted ASP.NET Web API 2 on HTTP request

I have a self-hosted OWIN web app running ASP.NET Web API 2.2, which is executed as a windows service. I want to achieve a remote configuration scenario: from a client, I want to make an HTTP request and in the called Web API action, I want to update a configuration file (e.g. to change a DB connection string) and then restart the service (so that the new configuration is loaded). However, here is the tricky part:

If I simply call Environment.Exit, then the respone won't finish and the client won't get the message that it worked. Is there any way to write to the response stream and finish it before restarting the service from within a Web-API action? Or should I use something else for this, maybe a filter? Or is there any other way that you would rather suggest?

I am not interested in any security discussion - the service is only available in an intranet and the corresponding Web API action is secured with authentication and authorization.

Thanks for your help in advance.

Upvotes: 1

Views: 744

Answers (1)

Anton Gorbunov
Anton Gorbunov

Reputation: 1444

Look like sucker punch, but it works:

[HttpGet]
public async Task<IHttpActionResult> ActionMethod()
{
    var httpActionResult = Ok(); //some work instead it
    new Thread(() =>
    {
        Thread.Sleep(500);
        Environment.Exit(1);
    }).Start();
    return httpActionResult;
}

I not shure, but if you exit with exitCode != 0, then services subsystem will think, that your service crashed and try restat it (If you setup it in service settings)

Upvotes: 3

Related Questions