Getting Started with OWIN and Katana (Console replaces IIS as a simple solution for making Web services)

Source: Internet
Author: User

Open Web Interface for. NET (OWIN) defines an abstraction between. NET Web servers and Web applications. By decoupling-the Web server from the application, OWIN makes it easier-to-create middleware for. NET web Development. Also, OWIN makes it easier to port Web applications to other Hosts-for example, self-hosting in a Windows service or other Process.

OWIN is a community-owned specification, not a implementation. The Katana project is a set of Open-source OWIN components developed by Microsoft. For a general overview of both OWIN and Katana, see an overview of Project Katana. In this article, I'll jump right into the code to get started.

This tutorial uses Visual Studio Release candidate, and you can also use Visual Studio 2012. A Few of the steps is different in Visual Studio, which I note below.

Host OWIN in IIS

In this section, we'll host OWIN in IIS. This option gives the flexibility and composabity of a OWIN pipeline together with the mature feature set of IIS. Using This option, the OWIN application runs in the ASP.

First, create a new ASP. NET WEB Application project. (in Visual Studio, use the ASP. NET. Web application project type.)

In the New ASP . Dialog, select the Empty template.

ADD NuGet Packages

Next, add the required NuGet packages. From the Tools menu, select Library Package Manager, and then select PackageManager Console. In the Package Manager Console window, type the following command:

install-package Microsoft.Owin.Host.SystemWeb –Pre

Add a Startup Class

Next, add an OWIN startup class. In Solution Explorer, right-click the project and select Add, then select New Item. In the Add New Item Dialog, select Owin Startup class. For more info on configuring the startup class, see OWIN Startup Class Detection.

ADD the following code to the Startup1.Configuration method:

Public void Configuration(IappbuilderApp){ //New code:   App. Run (context => {< Span class= "PLN" > Context. Response. Contenttype =  "Text/plain"  return Context. Response. Writeasync ( "Hello, world."  }); }             /span>                

This code adds a simple piece of middleware to the OWIN pipeline, implemented as a function that receives aMICROSOFT.O Win. Iowincontext instance. When the server receives an HTTP request, the OWIN pipeline invokes the middleware. The middleware sets the content type for the response and writes the response body.

Note:the OWIN Startup class template is available in Visual Studio 2013. If you is using Visual Studio, just add a new empty class named Startup1 , and paste in the following code:

Using System;Using System.Threading.Tasks;Using Microsoft.Owin;Using Owin;[Assembly: Owinstartup(typeof(Owinapp.Startup1))]Namespace Owinapp{ Public Class Startup1 { Public void Configuration(IappbuilderApp) {App.run (context => Span class= "pun" >{ Context. Response. Contenttype =  "Text/plain"  return Context. Response. Writeasync ( "Hello, world."  });  } }}  /span>                 
Run the application

Press F5 to begin debugging. Visual Studio would open a browser window to http://localhost:Port/. The page should look like the following:

Self-host OWIN in a Console application

It's easy-to-convert this application from IIS hosting to Self-hosting in a custom process. With IIS hosting, IIS acts as both the HTTP server and as the process that host the sever. With self-hosting, your application creates the process and uses the HttpListener class as the HTTP server.

In Visual Studio, create a new console application. In the Package Manager Console window, type the following command:

Install-Package Microsoft.Owin.SelfHost -Pre

Add a class from Part 1 of this tutorial to the Startup1 project. You don ' t need to modify the This class.

Implement the application ' s Main method as follows.

Class Program{ Static void Main(String[]Args) { Using (Microsoftowin. Hosting. Webapp. Start<startup1> ())  {  Console. Writeline ( "press [Enter] to quit ..." console. Readline} }}    /span>                

When you run the console application, the server starts listening to http://localhost:9000. If you navigate to this address in a Web browser, you'll see the ' Hello World ' page.

ADD OWIN Diagnostics

The Microsoft.Owin.Diagnostics package contains middleware a catches unhandled exceptions and displays an HTML page wit H Error details. This page functions much like the ASP. NET error page is sometimes called the ' yellow screen of Death ' (YSOD). Like the Ysod, the Katana error page was useful during development, but it's a good practice to disable it in production mo De.

To install the diagnostics. Your project, type the following command in the Package Manager Console window:

install-package Microsoft.Owin.Diagnostics –Pre

Change the code in your Startup1.Configuration method as follows:

Public void Configuration(IappbuilderApp){ New code:add the error page middleware to the pipeline. app.  Useerrorpage();   App.Run(Context= { New Code:throw An exception for the This URI path. if  (context. Request. Path ==  "/fail"   { throw  exception ( "Random Exception"  }            /span>               Context. Response. Contenttype =  "Text/plain"  return Context. Response. Writeasync ( "Hello, world."  }); }             /span>           

Now use CTRL+F5 to run the application without debugging, so that Visual Studio won't break on the exception. The application behaves the same as before, until you navigate to Http://localhost/fail, at which point the application th Rows the exception. The error page middleware would catch the exception and display an HTML page with a information about the error. You can click the tabs to see the stack, query string, cookies, request header, and OWIN environment variables.

Next Steps
    • OWIN Startup Class Detection
    • Use the OWIN to self-host ASP Web API
    • Use OWIN to Self-host SignalR

Getting Started with OWIN and Katana (Console replaces IIS as a simple solution for making Web services)

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.