下一代Asp.net开发规范OWIN(3)—— Middleware
2014-09-12 09:50
363 查看
Middleware是OWIN管道的基本组成单元,最后拼接的OWIN管道来处理客户端请求,输出网页。这篇文章,首先看看Web Form, MVC, Web API如何结合OWIN使用。 然后将如何编写Middleware和编写一个具体的Cache Middleware.
阅读目录:
一. 原有的Web Form, MVC项目如何结合OWIN?
1.1 通过路由配置,将程序分成多个部分,一些部分由Asp.net Web Form或者MVC处理,另外一部分由OWIN管道处理。
1.2 在Web Form, MVC之前插入OWIN
二. Web API以Middleware注册到OWIN管道
三. 自定义Cache Middleware
3.1 HelloWorld Middleware
3.2 Cache Middleware
四,总结
那么在当前的情况下,OWIN和Web Form, MVC的结合开发一般是两种形式:
1. 通过路由配置,将程序分成多个部分,一些部分由Asp.net Web Form或者MVC处理,另外一部分由OWIN管道处理。
如上面代码,在Application_Start函数或者路由配置函数中,分别为/owin路径和/special配置了不同的OWIN管道。
完整的代码,请移步这里http://aspnet.codeplex.com/sourcecontrol/latest#Samples/Katana/AspNetRoutes/Global.asax.cs
2. 在Web Form, MVC之前插入OWIN
在Web Form和MVC项目中,也可以添加Startup.cs, 指定成为OWIN的初始化类型,那么请求会先经过OWIN管道处理,最后转向Web Form或者MVC程序。这种方式,常常用来配置log, authentication, cache等等这些Middleware.
具体方法如下:
将该Middleware注册到OWIN管道后,执行得到的网页:
View Code
最后,将CacheMiddleware添加到OWIN管道中发挥作用,注意注册管道的顺序问题,Middleware是一定要在HelloWorldMiddleware之前的。
OWIN的优势在上面的例子中应该有些体现,就是Middleware之间通过数据和行为规范, 大家可以一起无缝地协同工作,任何第三方的Middleware都可以非常简单的集成到OWIN管道中,这应该是OWIN最大的魅力所在,开放的魅力。
同时, OWIN的目标是将Web Form, MVC, Web API统一到一个大的平台下,这将更加有助于混合编程。
阅读目录:
一. 原有的Web Form, MVC项目如何结合OWIN?
1.1 通过路由配置,将程序分成多个部分,一些部分由Asp.net Web Form或者MVC处理,另外一部分由OWIN管道处理。
1.2 在Web Form, MVC之前插入OWIN
二. Web API以Middleware注册到OWIN管道
三. 自定义Cache Middleware
3.1 HelloWorld Middleware
3.2 Cache Middleware
四,总结
一,原有的Web Form, MVC项目如何结合OWIN?
坏消息,非常抱歉,尽管OWIN是革命性的,但是Web Form和MVC现在还不能作为一个中间件集成到OWIN管道中。原因在第一篇中Asp.net的历史中有分析过,原因就是Web Form和MVC依赖于System.Web.dll中的很多类型。而在OWIN管道中,是无法提供这些依赖的。不过好消息是,在Asp.net vNext中,将会彻底告别System.Web.dll依赖, 那个时候,Asp.net vNext将是集大成者。听说vNext项目组正在和Mono团队一起工作,使得Asp.net vNext开发的项目能够在*nix, osx系统上运行。那么在当前的情况下,OWIN和Web Form, MVC的结合开发一般是两种形式:
1. 通过路由配置,将程序分成多个部分,一些部分由Asp.net Web Form或者MVC处理,另外一部分由OWIN管道处理。
// How to hook OWIN pipelines into the normal Asp.Net route table side by side with other components. protected void Application_Start(object sender, EventArgs e) { //owin开头的访问路径将会发送到startup.cs初始化的OWIN管道处理 RouteTable.Routes.MapOwinPath("/owin"); //special开头的访问路径将会由OwinApp2管道来处理 RouteTable.Routes.MapOwinPath("/special", app => { app.Run(OwinApp2.Invoke); }); }
如上面代码,在Application_Start函数或者路由配置函数中,分别为/owin路径和/special配置了不同的OWIN管道。
完整的代码,请移步这里http://aspnet.codeplex.com/sourcecontrol/latest#Samples/Katana/AspNetRoutes/Global.asax.cs
2. 在Web Form, MVC之前插入OWIN
在Web Form和MVC项目中,也可以添加Startup.cs, 指定成为OWIN的初始化类型,那么请求会先经过OWIN管道处理,最后转向Web Form或者MVC程序。这种方式,常常用来配置log, authentication, cache等等这些Middleware.
二,Web API以Middleware注册到OWIN管道
Web API由于无任何依赖于System.web.dll, 所以Web API可以作为Middleware注册到OWIN管道中。具体方法如下:
public class Startup { // Invoked once at startup to configure your application. public void Configuration(IAppBuilder builder) { HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute("Default", "api/{controller}/{customerID}", new { controller = "Customer", customerID = RouteParameter.Optional });//定义web api route //xml格式输出结果 config.Formatters.XmlFormatter.UseXmlSerializer = true; config.Formatters.Remove(config.Formatters.JsonFormatter); // config.Formatters.JsonFormatter.UseDataContractJsonSerializer = true; //将web api以Middleware注册到OWIN管道中 builder.UseWebApi(config); } }
三,自定义Cache Middleware
3.1 HelloWorld Middleware
先建一个Middleware, 通过继承OwinMiddleware基类。这个Middleware的功能非常简单,就是打印当前的系统时间。public class HelloWorldMiddleware : OwinMiddleware { public HelloWorldMiddleware(OwinMiddleware next) : base(next) { } public override Task Invoke(IOwinContext context) { var response = "Hello World! It is " + DateTime.Now; context.Response.Write(response); return Next.Invoke(context); } }
将该Middleware注册到OWIN管道后,执行得到的网页:
public class HelloWorldMiddleware : OwinMiddleware { public HelloWorldMiddleware(OwinMiddleware next) : base(next) { } public override Task Invoke(IOwinContext context) { var response = "Hello World! It is " + DateTime.Now; if (context.Environment.ContainsKey("caching.addToCache"))//这里直接从OWIN管道的字典中,检查是否有add cache, 如果存在,就将输出内容缓存到cache中,过期时间为10分钟。 { var addToCache = (Action<IOwinContext, string, TimeSpan>)context.Environment["caching.addToCache"]; addToCache(context, response, TimeSpan.FromMinutes(10)); } context.Response.Write(response); return Task.FromResult(0); } }
View Code
最后,将CacheMiddleware添加到OWIN管道中发挥作用,注意注册管道的顺序问题,Middleware是一定要在HelloWorldMiddleware之前的。
public class Startup { public void Configuration(IAppBuilder app) { app.Use<CacheMiddleware>(); app.Use<HelloWorldMiddleware>(); } }
四,总结
通过上面的示例,希望对大家如何编写Middleware有些基本的概念。OWIN的优势在上面的例子中应该有些体现,就是Middleware之间通过数据和行为规范, 大家可以一起无缝地协同工作,任何第三方的Middleware都可以非常简单的集成到OWIN管道中,这应该是OWIN最大的魅力所在,开放的魅力。
同时, OWIN的目标是将Web Form, MVC, Web API统一到一个大的平台下,这将更加有助于混合编程。
相关文章推荐
- 下一代Asp.net开发规范OWIN(3)—— Middleware
- <转>下一代Asp.net开发规范OWIN(1)—— OWIN产生的背景以及简单介绍
- 下一代Asp.net开发规范OWIN(1)—— OWIN产生的背景以及简单介绍
- 下一代Asp.net开发规范OWIN(2)—— Katana介绍以及使用
- 下一代Asp.net开发规范OWIN(1)—— OWIN产生的背景以及简单介绍
- 下一代Asp.net开发规范OWIN(2)—— Katana介绍以及使用
- [ASP.NET] 下一代ASP.NET开发规范:OWIN
- 兼容Mono的下一代云环境Web开发框架ASP.NET vNext
- 开发代码编写规范文档(适用于asp.net_C#)
- 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(四)-- Middleware
- [摘]ASP.NET Atlas是Microsoft开发的下一代网络应用程序框架.....
- ASP.NET Core 开发-中间件(Middleware)
- LayIM.AspNetCore Middleware 开发日记(七)Asp.Net.Core.SignalR闪亮登场
- asp.net控件开发技巧(1)使用HtmlTextWriter类规范输出标签
- ASP.NET程序开发规范
- ASP.NET网站开发规范
- 《ASP.NET网站开发》系列笔记(1)——编程规范
- 兼容Mono的下一代云环境Web开发框架ASP.NET vNext
- asp.net控件开发技巧(1)使用HtmlTextWriter类规范输出标签
- ASP.NET的软件开发规范_转载