2017-01-25 74 views
0

我有一个简单的WebApi2应用程序来处理各种REST请求。它本质上是SQL Server数据库上各种CRUD操作的前端。直到现在,我从来没有从Visual Studio以外的地方运行过它,但我通常不会执行Windows特定的东西,但是我在这里。从Windows应用程序启动WebApi应用程序

我的目标是将此webapp的功能构建到Windows桌面应用程序中(或者至少能够从Windows程序控制web应用程序),大多数情况下用户可以启动Web应用程序,停止它,查看谁连接到它等,但我不知道如何去连接这组特殊的点。谷歌其实是一件非常艰难的事情。如果答案涉及到执行各种系统命令行来告诉WebApp启动/停止/等,我可以通过我所做的任何事情需要在命令行上以某种方式,这很好)。

最终目的是为用户提供一个安装程序,他不必知道有一个web服务器,除非他真的想要。

那么我该如何去完成这部分? (如果这个问题太含糊,告诉我为什么,我会根据需要修改它)。

回答

0

有关Web API的好处之一就是可以在Web服务器(如IIS)之外托管。例如,您可以将其托管在Windows窗体应用程序中。以下是关于如何实现此目的的article with detailed instructions

你将有一个Startup类,将用于引导:

public class Startup 
{ 
    // This code configures Web API. The Startup class is specified as a type 
    // parameter in the WebApp.Start method. 
    public void Configuration(IAppBuilder appBuilder) 
    { 
     // Configure Web API for self-host. 
     HttpConfiguration config = new HttpConfiguration(); 
     config.Routes.MapHttpRoute( 
      name: "DefaultApi", 
      routeTemplate: "api/{controller}/{id}", 
      defaults: new { id = RouteParameter.Optional } 
     ); 

     appBuilder.UseWebApi(config); 
    } 
} 

,然后它只是一个开始听者的事情:

using (WebApp.Start<Startup>("http://localhost:8080")) 
{ 
    Console.WriteLine("Web Server is running."); 
    Console.WriteLine("Press any key to quit."); 
    Console.ReadLine(); 
} 

这将提供您的Web API上本地端口8080,您的应用程序可以向其发送HTTP请求。

所以基本上你要找的关键字是:self hosting asp.net web api

相关问题