OWIN (2)-introduction and use of Katana, owinkatana

Source: Internet
Author: User

OWIN (2)-introduction and use of Katana, owinkatana

Next, let's take a look at the background and brief introduction of OWIN. After understanding the ins and outs of the OWIN specification, let's take a look at the implementation of Katana's OWIN specification and how to use it in our Web development.

Reading directory:

I. Katana project structure and content

1.1 Host
1.2 Server
1.3 Middleware
1.4 Application

Ii. Katana sample code Hello World

2.1 run Hello World with IIS Host
2.2 migrate Hello World to the custom Host

Iii. Explanation of OWIN Startup configuration class

3.1 why are there no IDictionary <string, object> and Func <IDictionary <string, object>, Task> In the OWIN specification?
3.2 How does Host link to the Startup class?

I. Katana project structure and content

By understanding the Katana project structure, we can have a deeper understanding of the OWIN specifications. The following figure shows the main architecture of the Katana project.

Host:
1. Manage underlying processes
2. When there is a request, select the corresponding Server and build the OWIN pipeline to process the request.

The most familiar Host is IIS/Asp.net. However, IIS is both Host and Server. in IIS, the standard HttpModule and HttpHandler types are used to respond to requests. Katana builds a similar adapter on IIS so that the OWIN pipeline can run on IIS.
We can also implement a simple Host on our own, which can be in the Console or a Windows Service process. At the same time, katanahas run the host--owinhost.exe command directly.

1.2 Server

It is responsible for binding to the TCP port, listening to the requests sent from the port, and packaging the request information in the dictionary format according to the OWIN specification, and passing it to the lower-layer Middleware.
The Katana project contains two Server implementations:
Microsoft. Owin. Host. SystemWeb
This is used to correspond to IIS, because IIS is both Host and Server. therefore, the role of this Server is to register ASP. NET HttpModule and HttpHandler block the original processing process and send the request to the OWIN pipeline for processing.
Microsoft. AspNet. Host. SystemWeb depends on Sysetm. Web, which is highly coupled with IIS, so it cannot be migrated to non-IIS servers.

Microsoft. Owin. Host. HttpListener
Use HttpListener to open the Socket port, listen to the request, and then send the request packaging to the OWIN pipeline for processing.

1.3 Middleware:

This is a component in the OWIN pipeline. It can be a complete framework from simple compression components to ASP. NET Web APIs.
When a request is sent from the client, the request is sent to the OWIN pipeline for processing. This pipeline is initialized in the startup code. The component that makes up the MPs queue is Middleware.
To comply with the OWIN standard, Middleware should implement

Func<IDictionary<string, object>, Task>

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

1.4 Application

This is your program code. Because Katana does not replace ASP. NET, but a new way of writing and hosting components, so the existing ASP. NET Web APIs and SignalR applications will remain unchanged because these frameworks can be used 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 completely new development method, and do not replace ASP. NET. They are a new way of writing and hosting components by decoupling Host, Server and Applicantion. For example, if we use OWIN to develop Web APIs, we still use Asp.net Web APIs, but Web APIs become an integral part of our OWIN pipeline. During normal development, we do not feel the existence of the OWIN, but build our OWIN pipeline in the startup code. Generally, for large components such as Web API and SignalR, we register them at the end of the OWIN pipeline. However, components such as authentication and cache are usually registered to the front of the pipeline.

Ii. Katana sample code Hello World

The following example is used in VS 2013.

2.1 run Hello World with IIS Host

Using IIS as the host is a common method. Create an OWIN pipeline on IIS to run our program.
First, create a new Asp.net program in VS2013.

Public void Configuration (IAppBuilder app) {// New code: app. run (context => {context. response. contentType = "text/plain"; return context. response. writeAsync ("Hello, world. ");});}

The above code registers a simple Middleware to the OWIN pipeline. This middleware is very simple: when the request is received, the Hello World is output.
Run the program. The result page is as follows:

HttpListener classListening for Http requests sent from fixed ports.
Create a simple Console application and use Nuget to add Microsoft. Owin. SelfHost

Class Program {static void 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 in the browser.
As you can see, the OWIN applications have very few coupling associations and it is very easy to migrate between different hosts. In the future, when more excellent Host/Server besides IIS emerge, we will have more options.

Iii. Explanation of OWIN Startup configuration class

Above, we run a simple Hello World program using IIS and Console as the host. Although it is just a simple Hello World, there are still a lot of content in it. here we will introduce it one by one.

3.1 why are there no IDictionary <string, object> and Func <IDictionary <string, object>, Task> In the OWIN specification?

In the previous article, when talking about the OWIN specification, we mentioned that the data transmitted in the OWIN pipeline is in the form of IDictionary <string, object>, but it does not appear in our code.

The reason is that in Microsoft's OWIN implementation, the dictionary type is encapsulated into the IOwinContext interface type. In fact, the dictionary can be accessed through the attribute Environment, and common request and reponse attributes can also be packaged. In this way, we do not need to deal with IDictionary <string, object> directly. In the OWIN specification, the dictionary type is a very well designed and simple and common, but in actual development, it is not an intuitive and convenient process to directly retrieve the object from the dictionary type and then convert the object type.

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

app.Run(context => 
{
context.Response.ContentType = "text/plain";
return context.Response.WriteAsync("Hello, world.");
});

For another question, where is the Func <IDictionary <string, object>, Task>? Let's take a look at the definition of the Run function:

public static void Run(this IAppBuilder app, Func<Microsoft.Owin.IOwinContext, System.Threading.Tasks.Task> handler);

In Startup. cs, the main task is to use IAppBuilder to register Middleware to the OWIN pipeline. The Hello World we registered using lambda expressions is actually a Middleware component, but this Middleware component is too simple.

3.2 How does Host link to the Startup class?

Whether you use IIS, IIS Express, or OWIN Host, the services implemented by Microsoft on these hosts will find the Startup class according to specific rules, execute the Configuration method, and register Middleware.

Default name match
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.

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

In the configuration file's appSetting node settings

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

Through the above explanation, we hope to help you understand the use of Katana in actual projects.
In the next article, let's take a closer look at how we compile Middleware.




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.