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 an 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 will jump right into code to get started.

This tutorial uses Visual Studio 2013 Release Candidate, but you can also use Visual Studio 2012. A few of the steps are different in Visual Studio 2012, which I note below.

Host OWIN in IIS

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

First, create a new ASP.NET Web Application project. (In Visual Studio 2012, use the ASP.NET Empty Web Application project type.)

In the New ASP.NET Project dialog, select the Empty template.

Add NuGet Packages

Next, add the required NuGet packages. From the Tools menu, select Library Package Manager, then selectPackage Manager 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(IAppBuilder app)
{
// New code:
app.Run(context =>
{
context.Response.ContentType = "text/plain";
return context.Response.WriteAsync("Hello, world.");
});
}

This code adds a simple piece of middleware to the OWIN pipeline, implemented as a function that receives aMicrosoft.Owin.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 are using Visual Studio 2012, 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(IAppBuilder app)
{
app.Run(context =>
{
context.Response.ContentType = "text/plain";
return context.Response.WriteAsync("Hello, world.");
});
}
}
}

Run the Application

Press F5 to begin debugging. Visual Studio will 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 Startup1 class from part 1 of this tutorial to the project. You don't need to modify this class.

Implement the application's Main method as follows.

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();
}
}
}

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 will see the "Hello world" page.

Add OWIN Diagnostics

The Microsoft.Owin.Diagnostics package contains middleware that catches unhandled exceptions and displays an HTML page with error details. This page functions much like the ASP.NET error page that is sometimes called the “yellow screen of death” (YSOD). Like the YSOD, the Katana error page is useful during development, but it’s a good practice to disable it in production mode.

To install the Diagnostics package in 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(IAppBuilder app)
{
// New code: Add the error page middleware to the pipeline.
app.UseErrorPage(); app.Run(context =>
{
// New code: Throw an exception for this URI path.
if (context.Request.Path == "/fail")
{
throw new Exception("Random exception");
} context.Response.ContentType = "text/plain";
return context.Response.WriteAsync("Hello, world.");
});
}

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

Next Steps

Getting Started with OWIN and Katana(Console 代替iis 制作 web服务的简单方案)的更多相关文章

  1. OWIN与Katana详解

    前言 我胡汉三又回来了,!!!!, 最近忙成狗,实在没空写博文,实在对不起自己,博客园上逛了逛发现 我大微软还是很给力的 asp.net core 1.0 .net core 1.0 即将发布,虽然. ...

  2. OWIN与Katana

    OWIN与Katana详解   前言 我胡汉三又回来了,!!!!, 最近忙成狗,实在没空写博文,实在对不起自己,博客园上逛了逛发现 我大微软还是很给力的 asp.net core 1.0 .net c ...

  3. OWIN and Katana

      OWIN(Open Web Interface for .NET)是在.net的web server和web应用之间定义了一套规范. Katana是微软实现了OWIN的一个Web Server的项 ...

  4. VS2012 Getting Started with Owin and Katana

    参考地址:http://www.asp.net/aspnet/overview/owin-and-katana/getting-started-with-owin-and-katana 小提示: 该示 ...

  5. 用Owin Host实现脱离IIS跑Web API单元测试

    开发笔记:用Owin Host实现脱离IIS跑Web API单元测试   今天在开发一个ASP.NET Web API项目写单元测试时,实在无法忍受之前的笨方法,决定改过自新. 之前Web API的单 ...

  6. [转]九个Console命令,让js调试更简单

    转自:九个Console命令,让js调试更简单 一.显示信息的命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <!DOCTYPE html> <html ...

  7. 【F12】Console命令,让js调试更简单

    Console命令,让js调试更简单 一.显示信息的命令 console.log("normal"); // 用于输出普通信息 console.info("informa ...

  8. owin 中间件 katana 如何解密cookie

    .NET MVC5 默认的用户登录组件是AspNet.Identity ,支持owin,并且微软自己实现的一套owin 中间件叫 katana 补充一下 katana项目源码地址:https://ka ...

  9. OWIN and Katana - 1

    翻译自 http://www.asp.net/aspnet/overview/owin-and-katana/an-overview-of-project-katana 十多年来,基于ASP.NET框 ...

随机推荐

  1. 选择排序的MPI实现

    #include "stdafx.h" #include "mpi.h" #include <stdio.h> #include <math. ...

  2. Agri Net POJ1258 && Constructing Roads POJ2421

    题意,在给出的图中,使用最小花费的边,使这个图仍然连通. #include <cstdio> #include <algorithm> #include <cstring ...

  3. Java IO5:序列化与反序列化

    一.序列化和反序列化的概念 把对象转换为字节序列的过程称为对象的序列化. 把字节序列恢复为对象的过程称为对象的反序列化. 对象的序列化主要有两种用途: 1) 把对象的字节序列永久地保存到硬盘上,通常存 ...

  4. Spring中的Resource

    Spring中的资源定义:Resource此接口的全名为:org.springframework.core.io.Resource比较常用的资源定义的实现类为:1.ClassPathResource ...

  5. BIRT使用1:简介、概念、元素、报表设计器组成

    前一篇博客对birt进行了一个初探,相信通过上篇博客大家对birt有个初步认识,接下来我们随着下面这张思维导图的展示,进入birt的使用学习. 这一篇博客是第一部分,主要介绍一下birt的简介.概念. ...

  6. Apache Wamp WampServer 配置多端口 多站点 虚拟目录

    第一步:配置Apache 的 httpd.conf #Listen 0.0.0.0:80Listen 80Listen 81 第二步:开启虚拟站点 所属文件:httpd.conf #Virtual h ...

  7. WPF中映射clr namspace

    1. xaml中直接映射为prefix xmlns:prefix="clr-namespace:MyApplication.Modules.Entity;assembly=MyAssembl ...

  8. Lua从入门到精通

    1. 入门指南 http://www.cnblogs.com/linbc/archive/2009/06/02/1494622.html

  9. LinkedBlockingQueue

    LinkedBlockingQueue是一个基于已链接节点的.范围任意的blocking queue的实现.    此队列按 FIFO(先进先出)排序元素.队列的头部 是在队列中时间最长的元素.队列的 ...

  10. mysql修改表、字段、库的字符集

    在一次导入数据表(MYISAM)的经历:复制过来的表打开后中文出现乱码,肯定是字符集出现了不致的问题,所以从原数据库导出.sql文件,修改其中的创建表的语句,加入字符集DEFAULT CHARSET= ...