Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. // Add the Owin Usings:
  9. using Owin;
  10. using Microsoft.Owin.Hosting;
  11. using Microsoft.Owin;
  12.  
  13.  
  14.  
  15. namespace WebAPI
  16. {
  17. // use an alias for the OWIN AppFunc:
  18. using AppFunc = Func<IDictionary<string, object>, Task>;
  19.  
  20. class Program
  21. {
  22. static void Main(string[] args)
  23. {
  24. WebApp.Start<Startup>("http://localhost:8080");
  25. Console.WriteLine("Server Started; Press enter to Quit");
  26. Console.ReadLine();
  27. }
  28. }
  29.  
  30. public class MyMiddlewareConfigOptions
  31. {
  32. string _greetingTextFormat = "{0} from {1}{2}";
  33. public MyMiddlewareConfigOptions(string greeting, string greeter)
  34. {
  35. GreetingText = greeting;
  36. Greeter = greeter;
  37. Date = DateTime.Now;
  38. }
  39.  
  40. public string GreetingText { get; set; }
  41. public string Greeter { get; set; }
  42. public DateTime Date { get; set; }
  43.  
  44. public bool IncludeDate { get; set; }
  45.  
  46. public string GetGreeting()
  47. {
  48. string DateText = "";
  49. if (IncludeDate)
  50. {
  51. DateText = string.Format(" on {0}", Date.ToShortDateString());
  52. }
  53. return string.Format(_greetingTextFormat, GreetingText, Greeter, DateText);
  54. }
  55. }
  56.  
  57. public static class AppBuilderExtensions
  58. {
  59. public static void UseMyMiddleware(this IAppBuilder app, MyMiddlewareConfigOptions configOptions)
  60. {
  61. app.Use<MyMiddlewareComponent>(configOptions);
  62. }
  63.  
  64. public static void UseMyOtherMiddleware(this IAppBuilder app)
  65. {
  66. app.Use<MyOtherMiddlewareComponent>();
  67. }
  68. }
  69.  
  70. public class MyMiddlewareComponent
  71. {
  72. AppFunc _next;
  73.  
  74. // Add a member to hold the greeting:
  75. string _greeting;
  76.  
  77. public MyMiddlewareComponent(AppFunc next, string greeting)
  78. {
  79. _next = next;
  80. _greeting = greeting;
  81. }
  82.  
  83. public async Task Invoke(IDictionary<string, object> environment)
  84. {
  85. IOwinContext context = new OwinContext(environment);
  86.  
  87. // Insert the _greeting into the display text:
  88. await context.Response.WriteAsync(string.Format("<h1>{0}</h1>", _greeting));
  89. await _next.Invoke(environment);
  90. }
  91. }
  92.  
  93. public class MyOtherMiddlewareComponent
  94. {
  95. AppFunc _next;
  96. public MyOtherMiddlewareComponent(AppFunc next)
  97. {
  98. _next = next;
  99. }
  100. public async Task Invoke(IDictionary<string, object> environment)
  101. {
  102. IOwinContext context = new OwinContext(environment);
  103. await context.Response.WriteAsync("<h1>Hello from My Second Middleware</h1>");
  104. await _next.Invoke(environment);
  105. }
  106. }
  107.  
  108.  
  109.  
  110. public class Startup
  111. {
  112. public void Configuration(IAppBuilder app)
  113. {
  114. // Set up the configuration options:
  115. var options = new MyMiddlewareConfigOptions("Greetings!", "John");
  116. options.IncludeDate = true;
  117.  
  118. // Pass options along in call to extension method:
  119. //app.UseMyMiddleware(options);
  120. app.Use<MyMiddlewareComponent>(options);
  121. app.UseMyOtherMiddleware();
  122. }
  123. }
  124. }
  125.  
  126. app.Use<MyMiddlewareComponent>("somestring");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement