OWIN

Posted by Kerwen Blog on February 10, 2022

OWIN是Open Web Server Interface for .NET的首字母缩写.
OWIN在.NET Web Servers与Web Application之间定义了一套标准接口,OWIN的目标是用于解耦Web Server和Web Application。基于此标准,鼓励开发者开发简单、灵活的模块,从而推进.NET Web Development开源生态系统的发展。

为什么需要这样一个接口呢?因为.NET Web应用程序是运行于Web服务器之中的,.NET Web应用程序需要通过Web服务器接收用户的请求,并且通过Web服务器将响应内容发送用户。在OWIN之前,当我们在写ASP.NET应用的时候,我们的思想里是默认将我们的应用绑定到IIS上的。如果没有OWIN这样一个接口,.NET Web应用程序就要依赖于所运行的具体Web服务器,比如ASP.NET应用程序要依赖于IIS。有了这个接口,ASP.NET应用程序只需依赖这个抽象接口,不用关心所运行的Web服务器。
所以,OWIN的作用就是通过引入一组抽象接口,解耦了.NET Web应用程序与Web服务器,再次体现了接口的重要性。

Self-Host OWIN

  1. 创建一个空的控制台项目
    img
  2. 通过Nuget来安装 Microsoft.AspNet.WebApi.OwinSelfHost
  3. 添加一个OWIN启动类名为Startup

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
     using Owin;
     using System.Web.Http;
    
     namespace Owin_demo
     {
         public class Startup
         {
             public void Configuration(IAppBuilder appBuilder)
             {
                 HttpConfiguration config = new HttpConfiguration();
                 config.Routes.MapHttpRoute(
                     name: "DefaultApi",
                     routeTemplate: "api/{controller}/{id}",
                     defaults: new { id = RouteParameter.Optional }
                 );
    
                 appBuilder.UseWebApi(config);
             }
         }
     }
    
  4. 编写一个WebAPI的控制器

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
     using System.Collections.Generic;
     using System.Web.Http;
    
     namespace Owin_demo
     {
         public class BlogController : ApiController
         {
             public IEnumerable<string> Get()
             {
                 return new string[] { "kerwen", "owin kerwen blog" };
             }
    
             public string Get(int id)
             {
                 return string.Format("owin {0} by:kerwen", id);
             }
         }
     }
    
  5. 在Program.cs中启动OWIN

    1
    2
    3
    4
    5
    6
    7
    
     static void Main(string[] args)
     {
         string baseAddress = "http://localhost:9000/";
         WebApp.Start<Startup>(url: baseAddress);
         Console.WriteLine("Program has started, press any key to exit");
         Console.ReadLine();
     }
    
  6. 启动控制台程序,然后访问我们刚才设置的地址:

    1
    2
    
     http://localhost:9000/api/blog 
     http://localhost:9000/api/blog/88
    

Katana

Katana is a flexible set of components for building and hosting Open Web Interface for .NET (OWIN)-based web apps. New development should use ASP.NET Core. The Microsoft.Owin.* libraries are not available for .NET Core because ASP.NET Core has equivalent replacements for them.

  1. 空的 ASP.NET Web Application 应用程序
    img
  2. 通过Nuget安装 Microsoft.Owin.Host.SystemWeb
  3. 新建Startup.cs 编写简单的helloworld

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
     public class Startup
     {
         public void Configuration(IAppBuilder app)
         {
             app.Run(context =>
             {
                 context.Response.ContentType = "text/plain";
                 return context.Response.WriteAsync("Hello World!");
             });
         }
     }
    
  4. Nuget安装OwinHost
  5. 运行OwinHost.exe img
  6. 在 Web 浏览器中导航以http://localhost:5000/显示现在通过控制台运行的应用程序。

ASP.NET Core

ASP.NET Core:

Supports the Open Web Interface for .NET (OWIN).
Has .NET Core compatible replacements for the Microsoft.Owin.* (Katana) libraries.

ASP.NET Core’s OWIN support is deployed as part of the Microsoft.AspNetCore.Owin package. You can import OWIN support into your project by installing this package.

Reference

owin
Open Web Interface for .NET (OWIN) with ASP.NET Core
OWIN and Katana
OWIN使用
Katana Samples
Implement Owin Pipeline using ASP.NET Core