[ASP. NET] Next-generation ASP. NET development specifications: OWIN, asp. netowin

Source: Internet
Author: User

[ASP. NET] Next-generation ASP. NET development specifications: OWIN, asp. netowin

I submitted my resume for the interview today...

 

This section contains the following contents:

  • OWIN Introduction
  • OWIN specifications
  • Katana
  • Hello World (3 hosts)
  • Custom Middleware

 

OWIN Introduction

OWIN (Open Web Interface For. Net)

OWIN is A. Net Web development architecture developed by the. Net open-source community based on Ruby. It has a very simple standard definition and is designed to decouple Web Server and Web Application.

 

Limitations of ASP. NET

The core of ASP. NET is System. Web, and System. Web is tightly coupled with IIS. IIS bound to Windows OS, so ASP. NET cannot be used across platforms.

System. Web is an important component of the. NET Framework. Therefore, System. Web updates need to wait for the release of. NET.

 

OWIN specifications

OWIN defines four layers:

2

Host: mainly responsible for application configuration and startup processes, including initializing OWIN Pipeline and running Server.

Server: binds the socket and listens for the HTTP Request. Then, encapsulates the Body and Header of the Request and Response into a dictionary that complies with the OWIN specifications and sends them to the OWIN Middleware Pipeline.

Middleware: a Middleware and component located between the Server and Application to process requests sent to the Pipeline.

Application: This is the specific Application code, but we register them into OWIN Pipeline to process HTTP requests and become part of the OWIN Pipeline.

 

OWIN Interface

Func <IDictionary <string, object>, Task>;

Used for interaction between Server and Middleware.It is not a strict interface, but a delegate and eachThe OWIN middleware component must be provided.

The variable of the parameter type IDictionary <string, object> isEnvironment Variable

 

Katana

Microsoft introduced and promoted OWIN and implemented Katana in accordance with the owin specification.

 

Host

The host is only a process and is the carrier of the entire OWIN program. This host can be IIS, IIS Express, Console, and Windows Service.

Katana provides us with three options:

  • IIS/ASP. NET: IIS is the simplest and backward compatible method. In this scenario, OWIN Pipeline is started through the standard HttpModule and HttpHandler. To use this Host, you must use System. Web as the OWIN Server.
  • Self-Host: If you want to replace System with other servers. web, and can have more control, you can choose to create a custom host, such as using Windows Service, console applications, Winform to host the Server
  • OwinHost: You can also use katanaw.owinhost.exe: it is a command line application that runs at the root of the project, starts the HttpListener Server, and finds the constraint-based Startup Item.

 

Server

Binds the request to the TCP port, listens to the requests sent from the port, and packs the request information in dictionary format according to the OWIN specification, and passes the request information to the lower-layer Middleware.

Katana implements OWIN Server in the following categories:

  • System. Web: When IIS is used as the Host, System. Web registers itself as HttpModule and HttpHandler and processes the requests sent to IIS (Microsoft. Owin. Host. SystemWeb)
  • HttpListener: the default Server of owinhost.exe and custom Host. (Microsoft. Owin. Host. HttpListener)
  • WebListener: This is the default lightweight Server of ASP. NET vNext. It cannot be used in Katana currently.

 

Middleware

This is a component in the OWIN pipeline.

Middleware can be understood as a component that provides the Func <IDictionary <string, object>, Task> interface.

Katana providesOwinMiddlewareThe base class makes it easier for us to inherit to implement OWIN Middleware.

 

Application

Specific Code implementation, such as ASP. NET Web API and SignalR code implementation.

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

 

 

Hello World

IIS-Host

Create an empty Web project. Nuget references Microsoft. Owin. Host. SystemWeb

 

Create a Startup class

    public class Startup    {        public void Configuration(IAppBuilder app)        {            app.Run(context =>            {                context.Response.ContentType = "text/plain";                return context.Response.WriteAsync("Hello World!");            });        }    }

Access Project address: http: // localhost: xx

 

Self-Host

Create a console project and Nuget references Microsoft. Owin. SelfHost

Main Method

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

Create a Startup class

Class Startup {public void Configuration (IAppBuilder app) {# if DEBUG // diagnose the app. useErrorPage (); # endif // welcome page // app. useWelcomePage ("/"); app. run (x => x. response. writeAsync ("hello world "));}}

Access address: http: // localhost: 12345

 

OWIN-Host

Create a class library project and Nuget references OwinHost

 

Create a Startup class (Assembly Owin and Microsoft. Owin are required)

    public class Startup    {        public void Configuration(IAppBuilder app)        {            app.Run(context =>            {                context.Response.ContentType = "text/plain";                return context.Response.WriteAsync("Hello World!");            });        }    }

 

OwinHost.exe

Note that

So we can move the three owinhost.exe files

=>

In addition, move the files in the Debug or Release folder to the bin folder.

 

Cmdrun owinhost.exe

Access: http: // localhost: 5000/

 

 

Custom Middleware

Middleware is very similar to HttpModule and will be registered to the pipeline of Owin to execute the code.

 

Inherit OwinMiddleware

Public class HelloMiddlerware: OwinMiddleware {public HelloMiddlerware (OwinMiddleware next): base (next) {} public override Task Invoke (IOwinContext context) {context. response. write ("hello" + DateTime. now );
// Pipeline continues to go down return Next. Invoke (context );}}

Startup

Public class Startup {public void Configuration (IAppBuilder app) {// For more information about how to configure an application, visit the http://go.microsoft.com/fwlink? LinkID = 316888 // The app can be freely combined with pipelines. use <HelloMiddlerware> (); // Run inserts a middleware and terminates the process of downloading the app. run (x => x. response. writeAsync ("good"); // This middleware will not execute the app. use <HelloMiddlerware> ();}}

 

Let's take a look at the execution sequence of the component.

    public class Startup    {        public void Configuration(IAppBuilder app)        {            app.Use((x, next) =>            {                x.Response.ContentType = "text/html";                return next.Invoke();            });            app.Use((x, next) =>            {                x.Response.WriteAsync("1 Before");                next.Invoke();                return x.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 result:

 

 

 

Extension

How the Startup class is associated

1. default name matching
You can define the Startup. cs class, as long as the namespace of this class is the same as the name of Assembly. The Configuration method in Startup. cs will be executed during OWIN pipeline initialization.

 

2. Use OwinStartup Attribute
This is the method used in our example to directly specify which class is the Startup class.

 

3. Set the parameter on the appSetting node in the configuration file.

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

 

 

Pipeline

The pipeline of Owin is registered on the pipeline of Httpapplication in essence on the IIS Host.

For example, app. UseStageMarker (PipelineStage. Authenticate );

 

 

Reference:

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

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

Related Article

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.