惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
人人都是产品经理
人人都是产品经理
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
小众软件
小众软件
B
Blog
博客园 - 叶小钗
Microsoft Azure Blog
Microsoft Azure Blog
Apple Machine Learning Research
Apple Machine Learning Research
A
About on SuperTechFans
J
Java Code Geeks
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recent Announcements
Recent Announcements
宝玉的分享
宝玉的分享
Martin Fowler
Martin Fowler
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
V
V2EX

博客园 - JustRun

Webpack中publicPath设置 忘记Mysql的root密码怎么办? Visual Studio 2015上安装Entity Framework Power Tools Ubuntu下安装中文输入法 Ubuntu如何选择更新源 Orchard中如何配置远端发布 .Net缓存管理框架CacheManager 全新的membership框架Asp.net Identity(2)——绕不过的Claims 全新的membership框架Asp.net Identity(1)——.Net membership的历史 泛型使用中,解决类型转换问题 .Net中使用SendGrid Web Api发送邮件(附源码) 使用Filter跟踪Asp.net MVC页面加载时间 下一代Asp.net开发规范OWIN(2)—— Katana介绍以及使用 下一代Asp.net开发规范OWIN(1)—— OWIN产生的背景以及简单介绍 对象映射工具AutoMapper介绍 AngularJs中,如何在render完成之后,执行Js脚本 .Net Collection的一些理解——记录一次向实习生的答疑 代替Reflection(反射)的一些方法 使用PhoneGap开启移动开发之旅
下一代Asp.net开发规范OWIN(3)—— Middleware
JustRun · 2014-09-12 · via 博客园 - JustRun

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

四,总结

一,原有的Web Form, MVC项目如何结合OWIN?

坏消息,非常抱歉,尽管OWIN是革命性的,但是Web Form和MVC现在还不能作为一个中间件集成到OWIN管道中。原因在第一篇中Asp.net的历史中有分析过,原因就是Web FormMVC依赖于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管道后,执行得到的网页:

image

只要我们不断的刷新网页,每次显示的时间都会不同,因为每次都会重新读取系统时间,重新呈现页面。

3.2 Cache Middleware

实现cache middleware的思路比较简单,以访问的Url为key, 以输出的内容为value。第一次访问的时候,会缓存下来输出的内容,在下次访问的时候,将直接返回缓存的内容,而不是重新生成。具体代码如下:

public class CacheMiddleware : OwinMiddleware
   {
       private readonly IDictionary<string, CacheItem> _responseCache = new Dictionary<string, CacheItem>(); //Cache存储的字典

       public CacheMiddleware(OwinMiddleware next)
           : base(next)
       {
       }

       public override Task Invoke(IOwinContext context)
       {
           context.Environment["caching.addToCache"] = new Action<IOwinContext, string, TimeSpan>(AddToCache);
           var path = context.Request.Path.Value;

           //如果访问的路径没有缓存,就传递到OWIN管道的下一层中处理

           if (!_responseCache.ContainsKey(path))           {
               return Next.Invoke(context);
           }
           var cacheItem = _responseCache[path];

           //检查缓存是否到期
           if (cacheItem.ExpiryTime <= DateTime.Now)
           {
               _responseCache.Remove(path);
               return Next.Invoke(context);
           }

           //直接从缓存中输出,而不是重新render页面
           context.Response.Write(cacheItem.Response);
           return Task.FromResult(0);
       }

       //添加cache的方法,将会以委托的方式存放到OWIN管道字典中,这样任何OWIN的Middleware都能够调用,从而保存数据到缓存

       public void AddToCache(IOwinContext context, string response, TimeSpan cacheDuration)      

       {
           _responseCache[context.Request.Path.Value] = new CacheItem { Response = response, ExpiryTime = DateTime.Now + cacheDuration };
       }

       private class CacheItem
       {
           public string Response { get; set; }//保存缓存的内容
           public DateTime ExpiryTime { get; set; }//确定缓存的时间
       }
   }

View Code

接下来,我们要改造HelloWorldMiddleware, 在HelloWorldMiddleware输出后,我们把输出的内容保存到Cache中。具体代码如下:

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统一到一个大的平台下,这将更加有助于混合编程。