This leads directly to the topic. The introduction of OWIN middleware is not discussed here.Using the console as an example, we show you how to use OWIN's middleware.
The first class libraries we need to apply are:
- Owin
- Microsoft.Owin
- Microsoft.Owin.Hosting
- Microsoft.Owin.Host.HttpListener
The above class libraries can be referenced by nuget.
Second, add an application startup class to the console program.
using System; using System.Threading.Tasks; using Microsoft.Owin; using Owin; using MiddleWareApp.Extensions;//Reference Extension Class /****************************************************************************************************************** * * * Description: Application Startup Class (Version 1.0.0) * Author: Li Chaoqiang * Date: 2015/05/19 * Modification: * Reference: http://my.oschina.net/lichaoqiang/ * Note: Not yet... * * * ***************************************************************************************************************/ [assembly: OwinStartup(typeof(MiddleWareApp.Startup1))] namespace MiddleWareApp { /// <summary> ///Startup Class /// </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)); //Register custom Middleware //app.Use(typeof(MiddleWare.StaticMiddleWare)); //Register the second Middleware app.UseCustomMiddleWare(new CustomOptions() { //Custom Configuration Information Name = "CustomMiddleWare" }); app.UseStaticMiddleWare(new StaticOptions() { }); app.Run((context) => { context.Response.ContentType = "text/plain"; Console.WriteLine("From request:{0}", context.Request.Uri); return context.Response.WriteAsync("000"); }); } } }
In the console main program function, start 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> ///Main program /// </summary> /// <param name="args"></param> private static void Main(string[] args) { using (WebApp.Start<Startup1>(new StartOptions("http://localhost:9966/") { })) { Console.WriteLine("OWIN Server has started!press any key to exit"); Console.ReadLine(); } } } }
Try listening here, listening on port 9966.It is important to note that the WebApp.Start method returns an object with an integrated IDsipose interface. If we listen for HTTP requests, when using using, note that once it is released, the client request will fail. The simple reason is that the object returned after starting the WebApp.Start method cannot be released, otherwiseAll monitoring and middleware will be useless.
I have defined two middleware here, CustomMiddleWare and StaticMidlleWare.At the same time, I defined a middleware base class MiddleWareBase (abstract class) to use the middleware option parameter.CustomMiddleWare Direct Inheritance
Microsoft.Owin.OwinMiddleware
StaticMidlleWare inherits
MiddleWareBase<T> Class
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Owin; /****************************************************************************************************************** * * * Description: Authorization (Version 1.0.0) * Author: Li Chaoqiang * Date: 2017-03-26 18:40:20 * Modification: 2017-03-26 18:40:20 * Reference: http://my.oschina.net/lichaoqiang/ http://www.lichaoqiang.com * Note: Not yet... * * * ***************************************************************************************************************/ namespace MiddleWareApp.MiddleWare { /// <summary> ///Middleware Base Class /// </summary> /// <typeparam name="T"></typeparam> public abstract class MiddleWareBase<T> : Microsoft.Owin.OwinMiddleware { /// <summary> ///Set Options /// </summary> public T Options { get; protected set; } /// <summary> ///Constructor /// </summary> /// <param name="next"></param> protected MiddleWareBase(OwinMiddleware next) : base(next) { } /// <summary> ///Constructor overload /// </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; /****************************************************************************************************************** * * * Description: Authorization (Version 1.0.0) * Author: Li Chaoqiang * Date: 2017-03-26 17:57:30 * Modification: 2017-03-26 17:57:30 * Reference: http://my.oschina.net/lichaoqiang/ http://www.lichaoqiang.com * Note: Not yet... * * * ***************************************************************************************************************/ namespace MiddleWareApp.MiddleWare { /// <summary> /// CustomMiddleWare /// </summary> public class CustomMiddleWare : Microsoft.Owin.OwinMiddleware { /// <summary> /// Options /// </summary> public CustomOptions Options { get; private set; } /// <summary> ///Construction /// </summary> /// <param name="next"></param> public CustomMiddleWare(OwinMiddleware next) : base(next) { } /// <summary> ///Construction /// </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);//Pass Next Middleware } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Owin; using MiddleWareApp.Extensions; /****************************************************************************************************************** * * * Description: Authorization (Version 1.0.0) * Author: Li Chaoqiang * Date: 2017-03-26 18:16:06 * Modification: 2017-03-26 18:16:06 * Reference: http://my.oschina.net/lichaoqiang/ http://www.lichaoqiang.com * Note: Not yet... * * * ***************************************************************************************************************/ namespace MiddleWareApp.MiddleWare { /// <summary> ///Second middleware we defined /// </summary> public class StaticMiddleWare : MiddleWareBase<StaticOptions> { /// <summary> ///Constructor /// </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);//Hand it over to the next Middleware } } }
For middleware, define option parameters, extend to IAppBuilder type methods
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Owin; /****************************************************************************************************************** * * * Description: Authorization (Version 1.0.0) * Author: Li Chaoqiang * Date: 2017-03-26 18:23:57 * Modification: 2017-03-26 18:23:57 * Reference: http://my.oschina.net/lichaoqiang/ http://www.lichaoqiang.com * Note: Not yet... * * * ***************************************************************************************************************/ namespace MiddleWareApp.Extensions { /// <summary> /// <! [CDATA [custom extension class]> /// </summary> public static class CustomExtension { /// <summary> /// <! [CDATA [using custom middleware]> /// </summary> /// <param name="app"></param> /// <param name="options"></param> public static void UseCustomMiddleWare(this IAppBuilder app, CustomOptions options) { app.Use(typeof(MiddleWare.CustomMiddleWare), options);//Note: The Use r method is injected into the middleware through a constructor } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MiddleWareApp.MiddleWare; using Owin; /****************************************************************************************************************** * * * Description: Authorization (Version 1.0.0) * Author: Li Chaoqiang * Date: 2017-03-26 18:46:32 * Modification: 2017-03-26 18:46:32 * Reference: http://my.oschina.net/lichaoqiang/ http://www.lichaoqiang.com * Note: Not yet... * * * ***************************************************************************************************************/ 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); } } }
Define two option s objects:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; /****************************************************************************************************************** * * * Description: Authorization (Version 1.0.0) * Author: Li Chaoqiang * Date: 2017-03-26 18:25:12 * Modification: 2017-03-26 18:25:12 * Reference: http://my.oschina.net/lichaoqiang/ http://www.lichaoqiang.com * Note: Not yet... * * * ***************************************************************************************************************/ namespace MiddleWareApp.Extensions { /// <summary> ///Customize middleware, set options /// </summary> public class CustomOptions { /// <summary> ///Name /// </summary> public string Name { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; /****************************************************************************************************************** * * * Description: StaticOptions (Version 1.0.0) * Author: Li Chaoqiang * Date: 2017-03-26 18:46:06 * Modification: 2017-03-26 18:46:06 * Reference: http://my.oschina.net/lichaoqiang/ http://www.lichaoqiang.com * Note: Not yet... * * * ***************************************************************************************************************/ namespace MiddleWareApp.Extensions { /// <summary> /// StaticOptions /// </summary> public class StaticOptions { } }
So far, about the common use of OWIN middleware, introduce here, see if you feel OWIN middleware So easy! Just understand its principle, how to play, see you!See code to see itchy hands, try it.
Further usage is not discussed in this article!