Next Generation ASP Owin (2)--katana Introduction and use

Source: Internet
Author: User

After reading the background of the Owin and the brief introduction, after understanding the ins and outs of the Owin specification, take a look at the implementation of Katana this Owin specification and see how it is used in our web development.

Read the catalogue:

I. Structure and content of the Katana project

1.1 Host
1.2 Server
1.3 Middleware
1.4 Application

Two. Katana Sample code Hello World

2.1 Running Hello World with IIS host
2.2 Migrating Hello World to the custom host

Three. OWIN startup configuration class detailed

3.1 How there is no owin norm in the idictionary<string, object> and Func<idictionary<string, Object>, Task>?
How is 3.2 host linked to the startup class?

I. Structure and content of the Katana project

By understanding the Katana project structure, we are able to understand the Owin specification in greater depth. The following diagram is the main structure of the Katana project.

The various sections in the explanation:

1.1 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.
The main role of host:
1. Managing the underlying process
2. When a request comes in, select the appropriate server and build the Owin pipeline to process the request.

The host we are most familiar with is iis/asp.net. However, IIS is both host and server. The standard HttpModule and HttpHandler types are used in IIS to respond to requests. Katana enables the Owin pipeline to run on IIS by building a similar adapter on IIS.
We can also implement a simple host by itself, which can be in the console or a Windows service process. At the same time Katana has provided a direct running Host--owinhost.exe

1.2 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.
The Katana project contains 2 server implementations:
Microsoft.Owin.Host.SystemWeb
This is used to correspond to IIS, because IIS is both host and server. So the role of this server implementation is to register the ASP. HttpModule and HttpHandler block the original processing flow, and then send the request to the Owin pipeline processing.
Microsoft.AspNet.Host.SystemWeb relies on Sysetm.web, and IIS is coupled so badly that it cannot be migrated to a non-IIS server

Microsoft.Owin.Host.HttpListener
Use HttpListener to open the socket port, listen for requests, and then send the request wrapper to the Owin pipeline for processing.

1.3 Middleware:

This is the component in the OWIN pipeline that is composed. It can be a complete framework from simple compression components to ASP. NET Web API.
When a request is sent from the client, the request is uploaded to the Owin pipeline for processing. This pipeline is initialized in startup code. The component that makes up the pipe is middleware.
To follow the Owin standard, middleware should implement

func<idictionary<stringobject;, task>

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

1.4 Application

This is your program code. Because Katana does not replace ASP, it is a new way to write and host components, so existing ASP. NET Web APIs and SignalR applications will remain the same because they can participate in the OWIN pipeline. In fact, for these types of applications, the Katana component is visible only with a small configuration class.
Owin and Katana are not a new way to develop, not to replace ASP, to implement host, server and Applicantion decoupling, is a new way of writing and hosting components . For example, using the Owin approach to developing Web APIs, we still use the ASP. Just the Web API has become a part of our Owin pipeline. In normal development, we don't feel the existence of Owin, just build our Owin pipeline in the startup code. Typically, for a large component such as Web APIs and SIGNALR, we are registered at the end of the Owin pipeline. But for components like authentication, the cache, we usually register to the front of the pipeline.

Two. Katana Sample code Hello World

The following example operates in VS 2013.

2.1 Running Hello World with IIS host

Using IIS as the host is the way we use it. Run our program by creating a Owin pipeline on IIS.
First, in VS2013, create a new ASP.

In the pop-up box, select the empty template

Add a support package from NuGet

The next step is to add the Owin support package. From tools-> Library Package manager-> package Manager Console. Then, in the Command window, run Install-package Microsoft.Owin.Host.SystemWeb.
You can also add them directly to the NuGet package management interface.

Add Startup Startup class
The function of the startup class is to initialize the Owin pipeline, where we add and initialize the middleware in the Owin pipeline.

In the Startup1.configuration method, add the following code:

 Public void Configuration (Iappbuilder app) {    //  New code:    App. Run (context =    {        "text/plain";         return to context. Response.writeasync ("Hello, World. ") " );    });}

What the above code does is to register a simple middleware in the Owin pipeline. What this middleware does is very simple: output Hello World when the request is received.
Start the program run, the results page is as follows:

2.2 Migrating Hello World to the custom host

Migrating from IIS host to a custom host is straightforward. With IIS host, IIS acts as both the Host and server roles. In the custom host, we will use the console program as the host and use the HttpListener class as the server to listen for HTTP requests sent by the fixed port.
Start by creating a simple console application that adds Microsoft.Owin.SelfHost with NuGet

Also add our Startup1.cs, and then transform our console program into a host host.

class program{    staticvoid Main (string[] args)    {        using (microsoft.owin.hosting.webapp.start<startup1> ("http://localhost:9000" )         {            Console.WriteLine ("Press [Enter] to quit ... " );            Console.ReadLine ();     }}}

Start the program, and then type http://localhost:9000 access in the browser.
As you can see, the Owin application, coupled with very little coupling, is very easy to migrate between different hosts. In the future, we will have more choices when there are more excellent host/server outside of IIS.

Three. OWIN startup configuration class detailed

Above, we ran a simple Hello World program, which was hosted by IIS and console respectively. Although just a simple Hello world, actually contains a lot of content, the following is introduced.

3.1 How there is no owin norm in the idictionary<string, object> and Func<idictionary<string, Object>, Task>?

In the previous article, when it came to the Owin specification, it was mentioned that the data transmitted in the Owin pipeline was idictionary<string, Object>, but not in our code.

The reason is that in Microsoft's Owin implementation, the dictionary type is wrapped into the iowincontext interface type. You can actually access the dictionary through the property environment , and also wrap the commonly used request, reponse property. So we don't have to deal directly with Idictionary<string, object>, the dictionary type is a very good design in the Owin specification, simple and generic, but in actual development, the direct manipulation of the dictionary type gets object, Then converting the type is not an intuitive and convenient process after all.

In our Startup.cs code, the following context parameter type is actually Iowincontext.



" Text/plain "
return to context. Response.writeasync ("Hello, World. ") "
});

On another question,func<idictionary<string, Object> where is task>? Let's look at the definition of the run function at a glance:

 Public Static void Run ( This iappbuilder app, Func<microsoft.owin.iowincontext, system.threading.tasks.task> handler);

In Startup.cs, the main thing to do is to use Iappbuilder to register middleware into the Owin pipeline. The Hello world, which we registered with lambda expressions, is actually a middleware component, but this middleware component is simply too simple.

How is 3.2 host linked to the startup class?

Regardless of whether you use IIS, IIS Express or Owin host, the service that Microsoft implements on these hosts will find the startup class according to the specific rules, execute the configuration method, and register the middleware.

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.

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

The AppSetting node settings in the configuration file

< appSettings >    <  key= "Owin:appstartup"  value= "Startupdemo.productionstartup"/ ></appSettings>

Through the above explanation, we hope to help you understand the use of katana in the actual project.
In the next article, more close to the actual combat, together to see us write middleware

Next Generation ASP Owin (2)--katana Introduction and use

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.