使用OWIN自托管ASP.NET Web API

tech2022-09-20  73

                   (说明:本例为官方教程Use OWIN to Self-Host ASP.NET Web API)

本教程展示了如何在控制台应用程序中托管ASP.NET Web API,使用OWIN自托管Web API框架。

Open Web Interface for .net (OWIN)定义了.Net Web服务器和Web应用程序之间的抽象。OWIN将web应用程序与服务器解耦,这使得OWIN非常适合在IIS之外的自己的进程中自托管web应用程序。

 

Software versions used in the tutorial

Visual Studio 2017Web API 5.2.7

Note

本教程的完整源代码 github.com/aspnet/samples.

 

创建控制台应用程序

在File菜单上New,然后选择 Project。从 Installed中,在Visual C#下,选择Windows Desktop,然后选择Console App (.Net Framework)。将项目命名为“OwinSelfhostSample”并选择 OK。

添加Web API和OWIN包

从“Tools”菜单中选择“NuGet Package Manager”,然后选择“Package Manager Console”。在包管理器控制台窗口中,输入以下命令:

Install-Package Microsoft.AspNet.WebApi.OwinSelfHost

将安装WebAPI OWIN selfhost包和所有必需的OWIN包。

为自托管(self-host)配置Web API

在“解决方案资源管理器”中,右键单击项目并选择Add / Class以添加一个新类。将类命名为Startup。

将此文件中的所有样板(boilerplate )代码替换为以下代码:

using Owin; using System.Web.Http; namespace OwinSelfhostSample { 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); } } }

添加一个Web API controller

接下来,添加一个Web API控制器类。在“解决方案资源管理器”中,右键单击项目并选择Add / Class以添加一个新类。将类命名为“ValuesController”。

将此文件中的所有样板(boilerplate )代码替换为以下代码:

using System.Collections.Generic; using System.Web.Http; namespace OwinSelfhostSample { public class ValuesController : ApiController { // GET api/values public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } // GET api/values/5 public string Get(int id) { return "value"; } // POST api/values public void Post([FromBody]string value) { } // PUT api/values/5 public void Put(int id, [FromBody]string value) { } // DELETE api/values/5 public void Delete(int id) { } } }

启动OWIN Host并使用HttpClient发出请求

用以下代码替换Program.cs文件中的所有样板(boilerplate)代码:

using Microsoft.Owin.Hosting; using System; using System.Net.Http; namespace OwinSelfhostSample { public class Program { static void Main() { string baseAddress = "http://localhost:9000/"; // Start OWIN host using (WebApp.Start<Startup>(url: baseAddress)) { // Create HttpClient and make a request to api/values HttpClient client = new HttpClient(); var response = client.GetAsync(baseAddress + "api/values").Result; Console.WriteLine(response); Console.WriteLine(response.Content.ReadAsStringAsync().Result); Console.ReadLine(); } } } }

运行应用程序

在Visual Studio中按F5运行应用程序,输出应该如下所示:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Date: Tue, 09 Jul 2013 18:10:15 GMT Server: Microsoft-HTTPAPI/2.0 Content-Length: 19 Content-Type: application/json; charset=utf-8 } ["value1","value2"]

额外的资源

An Overview of Project Katana

Host ASP.NET Web API in an Azure Worker Role

最新回复(0)