Getting Started with OWIN and Katana(Console 代替iis 制作 web服务的简单方案)

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

转载于:https://www.cnblogs.com/haoliansheng/p/4246032.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值