Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Self terminating Self-hosted WebAPI

I have a console app which is self-hosting the ASP.Net WebAPI. I would like the console app to self terminate based on a specific call to the WebAPI.

The console app is heavily based on the example found here -> http://www.asp.net/web-api/overview/hosting-aspnet-web-api/self-host-a-web-api

To provide some context;

The console app will be used in conjunction with Jenkins CI to automate BDD testing of an Android application. Jenkins will be responsible for building, installing and initiating an Android app - it will then invoke this console app. The Android app will run through a series of Jasmine BDD tests automatically returning updates to Jenkins via the console app/ WebAPI. On completion of the tests, the Android App will call a specific WebAPI action - at this point I want it to terminate the console app so that Jenkins will move onto the next build step.

Thanks in advance.

like image 698
Mark Taylor Avatar asked Feb 15 '23 23:02

Mark Taylor


1 Answers

Just use a ManualResetEvent. In your Program class, add a static public field:

public static System.Threading.ManualResetEvent shutDown =
       new ManualResetEvent(false);

And then in your main method:

config.Routes.MapHttpRoute(
    "API Default", "api/{controller}/{id}", 
    new { id = RouteParameter.Optional });

using (HttpSelfHostServer server = new HttpSelfHostServer(config))
{
    server.OpenAsync().Wait();
    //Console.WriteLine("Press Enter to quit.");
    //Console.ReadLine();
    shutDown.WaitOne();
}

And then in the appropriate API method:

Program.shutDown.Set();

When your main method exits, the program will stop.

like image 112
Damien_The_Unbeliever Avatar answered Feb 22 '23 22:02

Damien_The_Unbeliever