1

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.

2
  • 1
    Restarting the service is a very heavy-handed way of solving this problem. Have you considered making your components capable of responding to configuration changes? Commented Nov 22, 2017 at 15:24
  • @PaulTurner Thanks for your response - I'm aware that your solution would be way better (reinitialize the application in the composition root) but unfortunately, I cannot do this in the amount of time I have left. Commented Nov 22, 2017 at 15:32

1 Answer 1

3

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.