Just use ManualResetEvent . In your program class, add a static public field:
public static System.Threading.ManualResetEvent shutDown = new ManualResetEvent(false);
And then in the main method:
config.Routes.MapHttpRoute( "API Default", "api/{controller}/{id}", new { id = RouteParameter.Optional }); using (HttpSelfHostServer server = new HttpSelfHostServer(config)) { server.OpenAsync().Wait();
And then in the corresponding API method:
Program.shutDown.Set();
When your main method exits, the program will stop.
source share