Created
September 12, 2014 06:02
-
-
Save RhysC/812838d38a3ddf05d0ee to your computer and use it in GitHub Desktop.
OWIN sample midlewares in a console app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Threading.Tasks; | |
| using Microsoft.Owin; | |
| using Owin; | |
| using OwinSpike.Simple.Console; | |
| //Console app | |
| //NuGet packages : | |
| //<package id="Microsoft.Owin.SelfHost" version="3.0.0" targetFramework="net45" /> | |
| [assembly: OwinStartup(typeof(Startup1))] | |
| namespace OwinSpike.Simple.Console | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| const string httpLocalhost = "http://localhost:9000"; | |
| using (Microsoft.Owin.Hosting.WebApp.Start<Startup1>(httpLocalhost)) | |
| { | |
| System.Console.WriteLine("Open a browser to " + httpLocalhost); | |
| System.Console.WriteLine("Press [enter] to quit..."); | |
| System.Console.ReadLine(); | |
| } | |
| } | |
| } | |
| public class Startup1 | |
| { | |
| public void Configuration(IAppBuilder app) | |
| { | |
| //This is our start up code that will run based on the assembly level OwinStartup attribute defined above | |
| //In here we register all of our middlewarez - below is a demo of the various options you have available in whihc you can register | |
| //Register an implemetation of Microsoft.Owin.OwinMiddleware | |
| app.Use<MyOwinMiddlewareImpl>(); // OR app.Use(typeof(MyOwinMiddlewareImpl)); | |
| //Register an implemetation of Microsoft.Owin.OwinMiddleware that has an additonal ctor dependency | |
| app.Use<MyOwinMiddlewareImplWithDependencies>("BOBBY BROWN"); // OR app.Use(typeof(MyOwinMiddlewareImplWithDependencies), "BOBBY BROWN"); | |
| //Register an anon delgate (lambda) that acts as middleware | |
| app.Use(async (context, next) => | |
| { | |
| await Task.Run(() => System.Console.WriteLine("Entering the ANON middleware!")); | |
| await next.Invoke(); | |
| await Task.Run(() => System.Console.WriteLine("Exiting the ANON middleware!")); | |
| }); | |
| //Register an named delgate (method) that acts as middleware | |
| app.Use(MyPrivateMiddlewareMethod); | |
| //Register a simgle instance class that will be reused in the midleware pipeline | |
| app.Use(new MyOwinMiddlewareImplAsAnInstance()); | |
| //RUN THE APP | |
| app.Run(context => | |
| { | |
| context.Response.ContentType = "text/plain"; | |
| return context.Response.WriteAsync("Hello, world."); | |
| }); | |
| } | |
| private async Task MyPrivateMiddlewareMethod(IOwinContext context, Func<Task> next) | |
| { | |
| var logger = Helper.GetLogger(ConsoleColor.Blue); | |
| await Task.Run(() => logger("Entering MyPrivateMiddlewareMethod middleware!")); | |
| await next.Invoke(); | |
| await Task.Run(() => logger("Exiting MyPrivateMiddlewareMethod middleware!")); | |
| } | |
| } | |
| public class MyOwinMiddlewareImpl : OwinMiddleware | |
| { | |
| private readonly Action<string> _logger; | |
| private readonly DateTime _timestamp; | |
| public MyOwinMiddlewareImpl(OwinMiddleware next) | |
| : base(next) | |
| { | |
| _timestamp = DateTime.Now; | |
| _logger = Helper.GetLogger(ConsoleColor.Green); | |
| } | |
| public override async Task Invoke(IOwinContext context) | |
| { | |
| await Task.Run(() => _logger(string.Format("Entering OwinMiddlewareImpl middleware{0}!", _timestamp))); | |
| await Next.Invoke(context); | |
| await Task.Run(() => _logger(string.Format("Exiting OwinMiddlewareImpl middleware{0}!", _timestamp))); | |
| } | |
| } | |
| public class MyOwinMiddlewareImplWithDependencies : OwinMiddleware | |
| { | |
| private readonly string _name; | |
| private readonly Action<string> _logger; | |
| public MyOwinMiddlewareImplWithDependencies(OwinMiddleware next, string name) | |
| : base(next) | |
| { | |
| _name = name; | |
| _logger = Helper.GetLogger(ConsoleColor.Red); | |
| } | |
| public override async Task Invoke(IOwinContext context) | |
| { | |
| await Task.Run(() => _logger(string.Format("Entering MyOwinMiddlewareImplWithDependencies middleware w/ {0}!", _name))); | |
| await Next.Invoke(context); | |
| await Task.Run(() => _logger(string.Format("Exiting MyOwinMiddlewareImplWithDependencies middleware w/ {0}!", _name))); | |
| } | |
| } | |
| public class MyOwinMiddlewareImplAsAnInstance | |
| { | |
| private readonly DateTime _timestamp; | |
| private Func<IDictionary<string, object>, Task> _next; | |
| private readonly Action<string> _logger; | |
| public MyOwinMiddlewareImplAsAnInstance() | |
| { | |
| _timestamp = DateTime.Now; | |
| _logger = Helper.GetLogger(ConsoleColor.Yellow); | |
| } | |
| public void Initialize(Func<IDictionary<string, object>, Task> next) | |
| { | |
| _next = next; | |
| } | |
| public async Task Invoke(IDictionary<string, object> environment) | |
| { | |
| await Task.Run(() => _logger(string.Format("Entering MyOwinMiddlewareImplAsAnInstance with timestamp {0}!", _timestamp))); | |
| await _next.Invoke(environment); | |
| await Task.Run(() => _logger(string.Format("Exiting MyOwinMiddlewareImplAsAnInstance with timestamp {0}!", _timestamp))); | |
| } | |
| } | |
| public static class Helper | |
| { | |
| public static Action<string> GetLogger(ConsoleColor color) | |
| { | |
| return (str) => | |
| { | |
| var time = DateTime.Now.ToString("0:hh:mm:ss"); | |
| var colour = System.Console.ForegroundColor; | |
| System.Console.ForegroundColor = color; | |
| System.Console.WriteLine("[{0}] {1}", time, str); | |
| System.Console.ForegroundColor = colour; | |
| }; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment