Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- // Add the Owin Usings:
- using Owin;
- using Microsoft.Owin.Hosting;
- using Microsoft.Owin;
- namespace WebAPI
- {
- // use an alias for the OWIN AppFunc:
- using AppFunc = Func<IDictionary<string, object>, Task>;
- class Program
- {
- static void Main(string[] args)
- {
- WebApp.Start<Startup>("http://localhost:8080");
- Console.WriteLine("Server Started; Press enter to Quit");
- Console.ReadLine();
- }
- }
- public class MyMiddlewareConfigOptions
- {
- string _greetingTextFormat = "{0} from {1}{2}";
- public MyMiddlewareConfigOptions(string greeting, string greeter)
- {
- GreetingText = greeting;
- Greeter = greeter;
- Date = DateTime.Now;
- }
- public string GreetingText { get; set; }
- public string Greeter { get; set; }
- public DateTime Date { get; set; }
- public bool IncludeDate { get; set; }
- public string GetGreeting()
- {
- string DateText = "";
- if (IncludeDate)
- {
- DateText = string.Format(" on {0}", Date.ToShortDateString());
- }
- return string.Format(_greetingTextFormat, GreetingText, Greeter, DateText);
- }
- }
- public static class AppBuilderExtensions
- {
- public static void UseMyMiddleware(this IAppBuilder app, MyMiddlewareConfigOptions configOptions)
- {
- app.Use<MyMiddlewareComponent>(configOptions);
- }
- public static void UseMyOtherMiddleware(this IAppBuilder app)
- {
- app.Use<MyOtherMiddlewareComponent>();
- }
- }
- public class MyMiddlewareComponent
- {
- AppFunc _next;
- // Add a member to hold the greeting:
- string _greeting;
- public MyMiddlewareComponent(AppFunc next, string greeting)
- {
- _next = next;
- _greeting = greeting;
- }
- public async Task Invoke(IDictionary<string, object> environment)
- {
- IOwinContext context = new OwinContext(environment);
- // Insert the _greeting into the display text:
- await context.Response.WriteAsync(string.Format("<h1>{0}</h1>", _greeting));
- await _next.Invoke(environment);
- }
- }
- public class MyOtherMiddlewareComponent
- {
- AppFunc _next;
- public MyOtherMiddlewareComponent(AppFunc next)
- {
- _next = next;
- }
- public async Task Invoke(IDictionary<string, object> environment)
- {
- IOwinContext context = new OwinContext(environment);
- await context.Response.WriteAsync("<h1>Hello from My Second Middleware</h1>");
- await _next.Invoke(environment);
- }
- }
- public class Startup
- {
- public void Configuration(IAppBuilder app)
- {
- // Set up the configuration options:
- var options = new MyMiddlewareConfigOptions("Greetings!", "John");
- options.IncludeDate = true;
- // Pass options along in call to extension method:
- //app.UseMyMiddleware(options);
- app.Use<MyMiddlewareComponent>(options);
- app.UseMyOtherMiddleware();
- }
- }
- }
- app.Use<MyMiddlewareComponent>("somestring");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement