简体   繁体   English

转换 Web API 以使用自托管

[英]Convert Web API to use Self Hosting

I am trying to convert an existing ASP.NET Web API project (currently hosted in IIS) into one that can use the SelfHost framework.我正在尝试将现有的 ASP.NET Web API 项目(当前托管在 IIS 中)转换为可以使用 SelfHost 框架的项目。 I'm a bit fuzzy on the actual details but understand I can run a self-host server in a console window and then run the service on top of it.我对实际细节有点模糊,但我明白我可以在控制台窗口中运行一个自托管服务器,然后在它上面运行服务。 The problem I'm having is that my project is an MVC project and not a console one.我遇到的问题是我的项目是一个 MVC 项目,而不是一个控制台项目。 My familiarity with console/Windows apps is somewhat limited as I generally work with projects to be hosted in IIS.我对控制台/Windows 应用程序的熟悉程度有限,因为我通常处理要在 IIS 中托管的项目。

What I'm a bit confused on is whether I need to convert my existing Web API project in Visual Studio into a new console application, or if there's a way to create another console application Project in the solution which can act as the web server for the Web API services, or rather if there's a way to add a console element with a Main() entry point to the existing MVC project (overriding the Global.asax entry point.)我有点困惑的是,我是否需要将 Visual Studio 中现有的 Web API 项目转换为新的控制台应用程序,或者是否有办法在解决方案中创建另一个控制台应用程序项目,该项目可以充当 Web 服务器Web API 服务,或者更确切地说,是否有办法将带有 Main() 入口点的控制台元素添加到现有 MVC 项目(覆盖 Global.asax 入口点)。

Search didn't yield much information that helps me fill this knowledge gap.搜索并没有产生多少信息来帮助我填补这个知识空白。 Hoping someone can point me in the right direction.希望有人能指出我正确的方向。 Even at a high level.即使在高水平。

I recently had to convert a Web API project into a self-hosted service using OWIN (on Visual Studio 2013).我最近不得不使用 OWIN(在 Visual Studio 2013 上)将 Web API 项目转换为自托管服务。 I did that as follows:我是这样做的:

  1. Manually added Program.cs and Startup.cs files at the root of the project.在项目的根目录下手动添加 Program.cs 和 Startup.cs 文件。 Both files containing code as described here: http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api .这两个文件都包含此处描述的代码: http : //www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api

  2. Went to the properties of the Web API project.转到 Web API 项目的属性。 On the "Applications" section, I stated "Output Type" as "Console Application", and set the "Program" class as the "Startup object".在“应用程序”部分,我将“输出类型”声明为“控制台应用程序”,并将“程序”类设置为“启动对象”。

Although not required, I slightly modified the using block within Program.Main() to look as follows:虽然不是必需的,但我稍微修改了Program.Main()using块,如下所示:

// Start OWIN host 
using (WebApp.Start<Startup>(url: baseAddress)) 
{ 
  // Create HttpCient and make a request to api/values 
  HttpClient client = new HttpClient(); 
  var response = client.GetAsync(baseAddress + "api/values").Result; 

  if (response != null)
  {
    Console.WriteLine("Information from service: {0}", response.Content.ReadAsStringAsync().Result);
  }
  else
  {
    Console.WriteLine("ERROR: Impossible to connect to service");
  }

  Console.WriteLine();
  Console.WriteLine("Press ENTER to stop the server and close app...");
  Console.ReadLine();
} 

Finally, instead of calling config.Routes.MapHttpRoute() multiple times within Startup.Configuration() , you can refer to the routes you already wrote for the Web API:最后,您可以参考您已经为 Web API 编写的路由,而不是在Startup.Configuration()多次调用config.Routes.MapHttpRoute()

// Configure Web API for self-host. 
var config = new HttpConfiguration();
WebApiConfig.Register(config);        
app.UseWebApi(config); 

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

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