简体   繁体   English

自终止自托管WebAPI

[英]Self terminating Self-hosted WebAPI

I have a console app which is self-hosting the ASP.Net WebAPI. 我有一个控制台应用程序,它自动托管ASP.Net WebAPI。 I would like the console app to self terminate based on a specific call to the WebAPI. 我希望控制台应用程序能够根据对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 控制台应用程序很大程度上基于此处的示例 - > 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 CI结合使用,以自动执行Android应用程序的BDD测试。 Jenkins will be responsible for building, installing and initiating an Android app - it will then invoke this console app. Jenkins将负责构建,安装和启动Android应用程序 - 然后它将调用此控制台应用程序。 The Android app will run through a series of Jasmine BDD tests automatically returning updates to Jenkins via the console app/ WebAPI. Android应用程序将运行一系列Jasmine BDD测试,通过控制台应用程序/ WebAPI自动返回Jenkins的更新。 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. 完成测试后,Android App将调用特定的WebAPI操作 - 此时我希望它终止控制台应用程序,以便Jenkins进入下一个构建步骤。

Thanks in advance. 提前致谢。

Just use a ManualResetEvent . 只需使用ManualResetEvent In your Program class, add a static public field: 在您的Program类中,添加一个静态公共字段:

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: 然后在适当的API方法中:

Program.shutDown.Set();

When your main method exits, the program will stop. 当主方法退出时,程序将停止。

To shutdown a normal .Net application you can use: 要关闭正常的.Net应用程序,您可以使用:

System.Environment.Exit(0)

See MSDN 请参阅MSDN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM