Asp. NET Development specification: OWIN

Source: Internet
Author: User

Asp. NET Development specification: OWIN

Today I have a resume for the interview ...

This program is recorded:

    • Owin Introduction
    • Owin specification
    • Katana
    • Hello World (3 kinds of host)
    • Custom Middleware

Owin Introduction

OWIN (Open Web Interface for. Net)

Owin is a. NET Web development architecture developed by the. NET open source community for reference to Ruby, with a very simple specification definition that is intended to decouple Web server and Web application.

Asp. NET's limitations

Asp. NET core is system.web, and system.web tightly coupled iis.iis is tied to Windows OS, so ASP.

System.Web is an important component of the. NET Framework, so the system.web update needs to wait for the. NET release

Owin specification

The Owin defines 4 layers:

2

Host: Primarily responsible for application configuration and startup processes, including initializing Owin Pipeline, running server.

Server: The HTTP request that binds the socket and listens and then encapsulates the body and header of the request and response into a dictionary that conforms to the Owin specification and is sent to Owin middleware pipeline

Middleware: Called middleware, component, between server and application, to handle requests sent to pipeline

Application: This is a specific application code, except that we register them in the Owin pipeline to process the HTTP request as part of the Owin pipeline

Owin interface

Func<idictionary<string, Object>, task>;

For server and middleware interactions. He is not a strict interface, but a delegate and each Owin middleware component must provide.

Where the variable with the parameter type idictionary<string,object> is an environment variable

Katana

Microsoft introduced and promoted Owin, and implemented the Katana in accordance with the Owin specification.

Host

The host is just a process and is the carrier of the entire Owin program. This host can be IIS, IIS Express, Console, Windows service, and so on.

Katana offers us a choice of 3:

    • Iis/asp.net: Using IIS is the simplest and backwards compatible way in which Owin pipeline is started by standard HttpModule and HttpHandler. Using this host you must use System.Web as the Owin Server
    • Self-host: If you want to use a different server to replace system.web, and you can have more control, you can choose to create a custom host, such as using Windows Service, console application, WinForm to host the server
    • Owinhost: You can also use the OwinHost.exe provided by Katana: He is a command-line application that runs at the root of the project, starts HttpListener server, and finds the constraint-based startup startup item

Server

Responsible for binding to the TCP port, listen to the port sent over the request, while the requested information according to the Owin specification, packaged into a dictionary format, passed to the lower middleware

Katana implementation of Owin server is divided into the following categories:

    • System.Web: When using IIS as Host, system.web registers itself as HttpModule and HttpHandler and processes requests sent to IIS (MICROSOFT.OWIN.HOST.SYSTEMWEB)
    • HttpListener: This is the OwinHost.exe and the custom host default server. (Microsoft.Owin.Host.HttpListener)
    • Weblistener: This is the default lightweight server for ASP. Vnext, which he is currently unable to use in Katana

Middleware

This is the component in the OWIN pipeline that is composed.

Middleware can be understood as providing func<idictionary<string, Object>, and task> interface components.

Katana provides a owinmiddleware base class that is more convenient for us to inherit to implement Owin middleware.

Application

Specific code implementations, such as the ASP. NET Web API, SIGNALR specific code implementations.

In fact, for these types of applications, the Katana component is visible only with a small configuration class.

Hello World

Iis-host

Create a new empty Web project, NuGet references Microsoft.Owin.Host.SystemWeb

Create Startup Class

1234567891011 publicclassStartup{    publicvoidConfiguration(IAppBuilder app)    {        app.Run(context =>        {            context.Response.ContentType = "text/plain";            returncontext.Response.WriteAsync("Hello World!");        });    }}

Access project Address: http://localhost:xx

Self-host

New Console project, NuGet reference Microsoft.Owin.SelfHost

Main method

1234 using(WebApp.Start<Startup>("http://localhost:12345")){    Console.ReadLine();}

Create Startup Class

12345678910111213     classStartup    {        publicvoidConfiguration(IAppBuilder app)        {#if DEBUG            //诊断            app.UseErrorPage();#endif            //欢迎页            //app.UseWelcomePage("/");            app.Run(x=>x.Response.WriteAsync("hello world"));        }    }

Access Address: http://localhost:12345

Owin-host

New Class Library project, NuGet reference Owinhost

Create Startup class (requires assembly Owin and Microsoft.owin)

1234567891011 publicclassStartup{    publicvoidConfiguration(IAppBuilder app)    {        app.Run(context =>        {            context.Response.ContentType = "text/plain";            returncontext.Response.WriteAsync("Hello World!");        });    }}

OwinHost.exe

We need to be aware

    1. After NuGet downloads, OwinHost.exe will be in the package folder
    2. OwinHost.exe will load the assembly under the./bin folder as the server assembly

So we're going to move this owinhost.exe3 file to

=

Also move the files under the Debug or Release folder to the Bin folder

CMD run OwinHost.exe

Visit: Http://localhost:5000/

Custom Middleware

Middleware is very similar to HttpModule, and is registered to execute code in Owin's pipeline.

Inherit Owinmiddleware

12345678910111213 publicclassHelloMiddlerware : OwinMiddleware{    publicHelloMiddlerware(OwinMiddleware next)        base(next)    {    }    publicoverrideTask Invoke(IOwinContext context)    {        context.Response.Write("hello"+DateTime.Now);<br>        //管道继续往下走        returnNext.Invoke(context);    }}

Startup

12345678910111213 publicclassStartup{    publicvoidConfiguration(IAppBuilder app)    {        // 有关如何配置应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=316888        //管线自由组合        app.Use<HelloMiddlerware>();        //Run是插入一个中间件,并终止继续往下流        app.Run(x => x.Response.WriteAsync("good"));        //此中间件将不执行        app.Use<HelloMiddlerware>();    }}

Take a look at the execution order of the components

123456789101112131415161718192021222324 publicclass Startup{    publicvoidConfiguration(IAppBuilder app)    {        app.Use((x, next) =>        {            x.Response.ContentType = "text/html";            returnnext.Invoke();        });        app.Use((x, next) =>        {            x.Response.WriteAsync("1 Before");            next.Invoke();            returnx.Response.WriteAsync("1 After");        });        app.Use((x, next) =>        {            x.Response.WriteAsync("2 Before");            next.Invoke();            return x.Response.WriteAsync("2 After");        });        app.Run(x => x.Response.WriteAsync("<br/>hello world<br/>"));    }}

Browsing results:

Extended

How the startup class is associated

1. Default name Matching
You can define the Startup.cs class, as long as the namespace and assembly names of this class are the same. The configuration method in this Startup.cs, then, is executed when the Owin pipeline is initialized.

2. Using Owinstartup Attribute
This is the way we use it in our example, specifying directly which specific class is the startup class.

3. appsetting node settings in the configuration file

123 <appSettings>   <add key="owin:appStartup"value="StartupDemo.ProductionStartup"/></appSettings>

Pipeline

The pipeline of Owin is essentially registered on the pipeline of HttpApplication on IIS host.

such as: App. Usestagemarker (pipelinestage.authenticate);

This article refers to:

Http://www.cnblogs.com/JustRun1983/p/3955238.html

Katana:http://www.asp.net/aspnet/overview/owin-and-katana/an-overview-of-project-katana

Asp. NET Development specification: OWIN

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.