这里就直接进入主题了,关于OWIN中间件的介绍,在此不做过多阐述。以控制台为示例,向大家演示了如何使用OWIN的中间件。
首先我们需要应用的类库有:
- Owin
- Microsoft.Owin
- Microsoft.Owin.Hosting
- Microsoft.Owin.Host.HttpListener
以上类库,nuget引用即可。
其次,给控制台程序,添加一个应用程序启动类。
using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
using MiddleWareApp.Extensions;//引用扩展类
/******************************************************************************************************************
*
*
* 说 明: 应用程序启动类(版本:Version1.0.0)
* 作 者:李朝强
* 日 期:2015/05/19
* 修 改:
* 参 考:http://my.oschina.net/lichaoqiang/
* 备 注:暂无...
*
*
* ***************************************************************************************************************/
[assembly: OwinStartup(typeof(MiddleWareApp.Startup1))]
namespace MiddleWareApp
{
/// <summary>
/// 启动类
/// </summary>
public class Startup1
{
/// <summary>
///
/// </summary>
/// <param name="app"></param>
public void Configuration(IAppBuilder app)
{
//app.Use<MiddleWare.CustomMiddleWare>();--POST
//app.Use(typeof(MiddleWare.CustomMiddleWare));//注册自定义中间件
//app.Use(typeof(MiddleWare.StaticMiddleWare));//注册第二个中间件
app.UseCustomMiddleWare(new CustomOptions()
{
//自定义配置信息
Name = "CustomMiddleWare"
});
app.UseStaticMiddleWare(new StaticOptions()
{
});
app.Run((context) =>
{
context.Response.ContentType = "text/plain";
Console.WriteLine("来自请求:{0}", context.Request.Uri);
return context.Response.WriteAsync("000");
});
}
}
}
在控制台主程序函数中,启动WebApp。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Owin.Hosting;
namespace MiddleWareApp
{
class Program
{
/// <summary>
/// 主程序
/// </summary>
/// <param name="args"></param>
private static void Main(string[] args)
{
using (WebApp.Start<Startup1>(new StartOptions("http://localhost:9966/")
{
}))
{
Console.WriteLine("OWIN服务器已经启动!按任意键退出");
Console.ReadLine();
}
}
}
}
这里尝试监听,监听端口9966。需要注意的是,WebApp.Start方法,返回的是一个集成IDsipose接口的对象,我们如果要监听HTTP请求,使用using的时候,注意,它一旦释放,客户端请求将失败,道理很简单,就是不能把WebApp.Start方法启动后返回的对象给释放了,否则,一切监听和中间件将无济于事。
我这里定义了两个中间件,分别为CustomMiddleWare和StaticMidlleWare。于此同时,我为了使用中间件的选项参数,定义了一个中间件基类MiddleWareBase(抽象类)。CustomMiddleWare直接继承
Microsoft.Owin.OwinMiddleware
StaticMidlleWare则继承
MiddleWareBase<T> 类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Owin;
/******************************************************************************************************************
*
*
* 说 明: 授权(版本:Version1.0.0)
* 作 者:李朝强
* 日 期: 2017-03-26 18:40:20
* 修 改:2017-03-26 18:40:20
* 参 考:http://my.oschina.net/lichaoqiang/ http://www.lichaoqiang.com
* 备 注:暂无...
*
*
* ***************************************************************************************************************/
namespace MiddleWareApp.MiddleWare
{
/// <summary>
/// 中间件基类
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class MiddleWareBase<T> : Microsoft.Owin.OwinMiddleware
{
/// <summary>
/// 设置选项
/// </summary>
public T Options { get; protected set; }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="next"></param>
protected MiddleWareBase(OwinMiddleware next)
: base(next)
{
}
/// <summary>
/// 构造函数重载
/// </summary>
/// <param name="next"></param>
/// <param name="options"></param>
protected MiddleWareBase(OwinMiddleware next, T options)
: base(next)
{
this.Options = options;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Owin;
using MiddleWareApp.Extensions;
/******************************************************************************************************************
*
*
* 说 明: 授权(版本:Version1.0.0)
* 作 者:李朝强
* 日 期: 2017-03-26 17:57:30
* 修 改:2017-03-26 17:57:30
* 参 考:http://my.oschina.net/lichaoqiang/ http://www.lichaoqiang.com
* 备 注:暂无...
*
*
* ***************************************************************************************************************/
namespace MiddleWareApp.MiddleWare
{
/// <summary>
/// CustomMiddleWare
/// </summary>
public class CustomMiddleWare : Microsoft.Owin.OwinMiddleware
{
/// <summary>
/// Options
/// </summary>
public CustomOptions Options { get; private set; }
/// <summary>
/// 构造
/// </summary>
/// <param name="next"></param>
public CustomMiddleWare(OwinMiddleware next)
: base(next)
{
}
/// <summary>
/// 构造
/// </summary>
/// <param name="middleware"></param>
/// <param name="options"></param>
public CustomMiddleWare(OwinMiddleware middleware, CustomOptions options)
: base(middleware)
{
this.Options = options;
}
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override Task Invoke(Microsoft.Owin.IOwinContext context)
{
var p = Options;
context.Response.Write("<p>C<strong>ustomMiddleWare</strong>!This is an middleware!</p>");
return Next.Invoke(context);//传递下一个中间件
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Owin;
using MiddleWareApp.Extensions;
/******************************************************************************************************************
*
*
* 说 明: 授权(版本:Version1.0.0)
* 作 者:李朝强
* 日 期: 2017-03-26 18:16:06
* 修 改:2017-03-26 18:16:06
* 参 考:http://my.oschina.net/lichaoqiang/ http://www.lichaoqiang.com
* 备 注:暂无...
*
*
* ***************************************************************************************************************/
namespace MiddleWareApp.MiddleWare
{
/// <summary>
/// 我们定义的第二个中间件
/// </summary>
public class StaticMiddleWare : MiddleWareBase<StaticOptions>
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="next"></param>
/// <param name="options"></param>
public StaticMiddleWare(OwinMiddleware next, StaticOptions options)
: base(next, options)
{
}
/// <summary>
/// Invoke
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override Task Invoke(IOwinContext context)
{
context.Response.Write("<p><strong>StaticMiddleWare</strong>!This is a middleware.</p>");
return Next.Invoke(context);//交给下一个中间件处理
}
}
}
为中间件,定义选项参数,扩展给IAppBuilder类型方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Owin;
/******************************************************************************************************************
*
*
* 说 明: 授权(版本:Version1.0.0)
* 作 者:李朝强
* 日 期: 2017-03-26 18:23:57
* 修 改:2017-03-26 18:23:57
* 参 考:http://my.oschina.net/lichaoqiang/ http://www.lichaoqiang.com
* 备 注:暂无...
*
*
* ***************************************************************************************************************/
namespace MiddleWareApp.Extensions
{
/// <summary>
/// <![CDATA[自定义扩展类]]>
/// </summary>
public static class CustomExtension
{
/// <summary>
/// <![CDATA[使用自定义中间件]]>
/// </summary>
/// <param name="app"></param>
/// <param name="options"></param>
public static void UseCustomMiddleWare(this IAppBuilder app, CustomOptions options)
{
app.Use(typeof(MiddleWare.CustomMiddleWare), options);//注意:Use方法是通过构造函数注入到中间件中的
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MiddleWareApp.MiddleWare;
using Owin;
/******************************************************************************************************************
*
*
* 说 明: 授权(版本:Version1.0.0)
* 作 者:李朝强
* 日 期: 2017-03-26 18:46:32
* 修 改:2017-03-26 18:46:32
* 参 考:http://my.oschina.net/lichaoqiang/ http://www.lichaoqiang.com
* 备 注:暂无...
*
*
* ***************************************************************************************************************/
namespace MiddleWareApp.Extensions
{
/// <summary>
/// StaticExtension
/// </summary>
public static class StaticExtension
{
/// <summary>
/// UseStaticMiddleWare
/// </summary>
/// <param name="app"></param>
/// <param name="options"></param>
public static void UseStaticMiddleWare(this IAppBuilder app, StaticOptions options)
{
app.Use<StaticMiddleWare>(options);
}
}
}
定义两个option对象:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/******************************************************************************************************************
*
*
* 说 明: 授权(版本:Version1.0.0)
* 作 者:李朝强
* 日 期: 2017-03-26 18:25:12
* 修 改:2017-03-26 18:25:12
* 参 考:http://my.oschina.net/lichaoqiang/ http://www.lichaoqiang.com
* 备 注:暂无...
*
*
* ***************************************************************************************************************/
namespace MiddleWareApp.Extensions
{
/// <summary>
/// 自定义中间件,设置选项
/// </summary>
public class CustomOptions
{
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/******************************************************************************************************************
*
*
* 说 明: StaticOptions(版本:Version1.0.0)
* 作 者:李朝强
* 日 期: 2017-03-26 18:46:06
* 修 改:2017-03-26 18:46:06
* 参 考:http://my.oschina.net/lichaoqiang/ http://www.lichaoqiang.com
* 备 注:暂无...
*
*
* ***************************************************************************************************************/
namespace MiddleWareApp.Extensions
{
/// <summary>
/// StaticOptions
/// </summary>
public class StaticOptions
{
}
}
至此,关于OWIN中间件的常见用法,介绍到此,看完,有没有觉得OWIN中间件So easy!只要明白它的原理,怎么个玩法,看你的了!看代码看的手痒痒的,动手试一试吧。
更深一步的用法,不在此文中探讨!