Understanding and practice of Owin (ii) Development of –host and server

Source: Internet
Author: User
Tags hosting

Original: Understanding and Practice of Owin (ii) Development of –host and server

For developers, the code is the best documentation, as the previous blog post said, we will further understand the implementation and role of Owin based on some specific calling code of the Kanata project.

Today we are going to implement a simple application for host and server.

Our development environment is: VS2013 Update 3,. Net Framework 4.5.1

Host development

As mentioned in the previous blog post, host has the following features:

    • Implementing a Host Process
    • Responsible for server startup and shutdown
    • Responsible for loading of middleware and application

The simplest hosting process is the console application, so we start by building the console program

Note that this program and its entry class (typically Program.cs) are what we call the host host process implementation.

and the specific host and server implementation we need to rely on the implementation of the Kanata project.

First, the host implementation of Kanata is obtained through NuGet. Package name is : Microsoft.Owin.Hosting

Note: The current version is 3.0.1, the 2 core components of Owin: Owin and Microsoft.owin will be loaded simultaneously.

Webapp.start

The main purpose of host is to start and shut down the server, the implementation of this function is the Microsoft.Owin.Hosting under the WebApp class, see the Class structure :

As we can see,WebApp only has a number of start functions, which are auxiliary to the Startoptions class, and depending on these 2 classes, the host will be able to start and close the server.

The start function has several overloads, so let's look at one of the most typical implementations. (other implementations are similar, if you want to use see related documents)

public static system.idisposable Start(Microsoft.Owin.Hosting.StartOptions Options , system.action<iappbuilder> startup)

    • The options parameter defines the parameters that are required for the server to start.
    • The startup parameter is a function that takes the Owin.iappbuilder interface as the parameter, through this function, completes the load work which the server needs middleware, this is also one of host's key functions!
    • The function returns a class that implements the IDisposable interface, and it can be expected that when this class is Dispose, the server that is started by this function will also close and die.
Startoptions

Let's take a look at the 2 more critical attributes in this startoptions:

Public system.collections.generic.ilist<string> Urls {get;}

The URLs parameter is the hostname and port that defines the server listener in the HTTP standard URL as a format string.

The standard format is http (s)://hostname:port ;  such as http://localhost:8080; https://192.168.1.1:9000;

Note that URLs can be added more than one, indicating that different hostname and port mappings are supported. In addition, a format such as http://*:9000 is supported, indicating that all hostname are mapped, and that the IIS mapping configuration is actually similar.

public string Serverfactory {set; get;}

The key comes, this is the server implementation class assembly name, it is not difficult to think that through this property, the host and server is fully decoupled. Our host can be combined with any server implementation that conforms to the Owin interface. Of course, the current use of your own server implementation, see the next section.

Server implementations and references

Http server is not one or two code can be implemented, here we continue to stand on the shoulders of Kanata, borrowing its implementation of the server. His server implementation package is named: Microsoft.Owin.Host.HttpListener ( I think the Host here is renamed server more appropriate)

First, or use NuGet to get the server component

installation, and introduction of the project, with the implementation of the server, let us complete the Startoptions definition code:

            //initializing the startoptions parameterstartoptions Options=Newstartoptions (); //Server URL Settingsoptions. Urls.add ("http://localhost:9000"); Options. Urls.add ("http://192.168.1.1:8080"); //Server implementation Class Library settingsoptions. Serverfactory="Microsoft.Owin.Host.HttpListener";

Note that I have specifically put 2 port different address, these 2 URLs can play an effect; In addition Microsoft.Owin.Host.HttpListener is actually the default Host serverfatory implementation (the last line of code can be omitted); But we have to understand that this can be decoupled, and we can bridge the other server implementations completely.

The startup function and the use

The startup function is very simple, just a function with no return value, only one owin.iappbuilder parameter (functions name arbitrary, not necessarily called startup). such as the following code:

        Private Static void Startup (Owin.iappbuilder app)        {            // here through the app handle, add all required middleware} to the current server        

In this function, through the handle provided by the app parameter, step by step to add all the middleware required by the server, of course these middleware are free to mix, free disassembly, very flexible.

Startup and shutdown of the server

With the options and startup functions, we can start our server, and the code for integration is as follows:

        /// <summary>        ///Owin Host main process entry function/// </summary>        Static voidMain () {//initializing the startoptions parameterStartoptions options =Newstartoptions (); //Server URL SettingsOptions. Urls.add ("http://localhost:9000"); Options. Urls.add ("http://192.168.1.1:8080"); //Server implementation Class Library settingsOptions. Serverfactory ="Microsoft.Owin.Host.HttpListener";
//start the server with current options and startup using(Webapp.start (Options, Startup)) {//displays startup information, which resides in the current process through ReadLineConsole.WriteLine ("Owin host/server started,press Enter to exit it ..."); Console.ReadLine (); }//server shuts down in Dispose } Private Static voidStartup (Owin.iappbuilder app) {//here, with the app handle, add all the required middleware to the current server}

The shutdown of the server is simple, and when dispose is triggered by the end of the using, the server is automatically shut down.

The server is run by a separate thread, so the host process must reside, where the current thread resides with Console.ReadLine (), ensuring that the server runs for a long time.

Other points of attention
    • There are not enough permissions on many machines, please run VS2013 with Administrator account.
    • Access to 2 addresses in the browser can be connected, but reponse is empty, which is in line with the previous article said: Server is only an empty implementation, in the absence of any middleware load (our startup function is empty), All request can only get an empty response argument.
    • The addition of different middleware enables different reponse to be returned, which will be discussed in a later space.
    • Try to get the component packages we need with nuget to ensure component correctness and version uniformity.

In conclusion, we have developed a console application is the program that hosts the process, by Kanata the host Implement Microsoft.Owin.Hosting the server that started it itself achieve Microsoft.Owin.Host.HttpListener, no middleware currently in the party and Application implementation.

Here again, Kanata's host and server implementations can be replaced by their own or third-party implementations, provided that they conform to the Owin standard.

Understanding and practice of Owin (ii) Development of –host and server

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.