Owin and Katana

11,632 views
11,358 views

Published on

The slides used during the talk "Owin and Katana" from the NCrafts Conference (http://ncrafts.io) on the 16 May in Paris.

Published in: Technology
2 Comments
11 Likes
Statistics
Notes
No Downloads
Views
Total views
11,632
On SlideShare
0
From Embeds
0
Number of Embeds
6,359
Actions
Shares
0
Downloads
114
Comments
2
Likes
11
Embeds 0
No embeds

No notes for slide

Owin and Katana

  1. 1. Katana & OWIN A new lightweight Web server for .NET Simone Chiaretta @simonech http://codeclimber.net.nz Ugo Lattanzi @imperugo http://tostring.it
  2. 2. Agenda  What is OWIN  OWIN Specs  Introducing Katana  Katana pipeline  Using Katana  Building OWIN middleware  A look at the future
  3. 3. What is OWIN
  4. 4. What is OWIN OWIN defines a standard interface between .NET web servers and web applications. The goal of the OWIN interface is to decouple server and application, encourage the development of simple modules for .NET web development, and, by being an open standard, stimulate the open source ecosystem of .NET web development tools. http://owin.org
  5. 5. What is OWIN The design of OWIN is inspired by node.js, Rack (Ruby) and WSGI (Phyton). In spite of everything there are important differences between Node and OWIN. OWIN specification mentions a web server like something that is running on the server, answer to the http requests and forward them to our middleware. Differently Node.Js is the web server that runs under your code, so you have totally the control of it.
  6. 6. IIS and OS  It is released with the OS  It means you have to wait the new release of Windows to have new features (i.e.: WebSockets are available only on the latest version of Windows)  There aren’t update for webserver;  Ask to your sysadmin “I need the latest version of Windows because of Web Sockets“
  7. 7. System.Web  I was 23 year old (now I’m 36) when System.Web was born!  It’s not so cool (in fact it’s missing in all new FW)  Testing problems  2.5 MB for a single dll  Performance
  8. 8. Support OWIN/Katana Many application frameworks support OWIN/Katana  Web API  SignalR  Nancy  ServiceStack  FubuMVC  Simple.Web  RavenDB
  9. 9. OWIN specs
  10. 10. OWIN Specs: AppFunc using AppFunc = Func< IDictionary<string, object>, // Environment Task>; // Done http://owin.org
  11. 11. OWIN Specs: Environment Some compulsory keys in the dictionary (8 request, 5 response, 2 others)  owin.RequestBody – Stream  owin.RequestHeaders - IDictionary<string, string[]>  owin.Request*  owin.ResponseBody – Stream  owin.ResponseHeaders - IDictionary<string, string[]>  owin.ResponseStatusCode – int  owin.Response*  owin.CallCancelled - CancellationToken
  12. 12. OWIN Specs: Layers • Startup, initialization and process management Host • Listens to socket • Calls the first middleware Server • Pass-through components Middleware • That’s your code Application
  13. 13. Introducing Katana aka Fruit Ninja
  14. 14. Why Katana  ASP.NET made to please ASP Classic (HTTP req/res object model) and WinForm devs (Event handlers): on fits all approach, monolithic (2001)  Web evolves faster then the FW: first OOB release of ASP.NET MVC (2008)  Trying to isolate from System.Web and IIS with Web API (2011)  OWIN and Katana fits perfectly with the evolution, removing dependency on IIS
  15. 15. Katana pillars  It’s Portable  Components should be able to be easily substituted for new components as they become available  This includes all types of components, from the framework to the server and host
  16. 16. Katana pillars  It’s Modular/flexible  Unlike many frameworks which include a myriad of features that are turned on by default, Katana project components should be small and focused, giving control over to the application developer in determining which components to use in her application.
  17. 17. Katana pillars  It’s Lightweight/performant/scalable  Fewer computing resources;  As the requirements of the application demand more features from the underlying infrastructure, those can be added to the OWIN pipeline, but that should be an explicit decision on the part of the application developer
  18. 18. Katana Pipeline
  19. 19. Katana Pipeline Host IIS OwinHost.exe Custom Host Server System.Web HttpListener Middleware Logger WebApi And more Application That’s your code
  20. 20. Using Katana
  21. 21. Hello Katana: Hosting on IIS
  22. 22. Hello Katana: Hosting on IIS
  23. 23. Hello Katana: Hosting on IIS public void Configuration(IAppBuilder app) { app.Use<yourMiddleware>(); app.Run(context => { context.Response.ContentType = "text/plain"; return context.Response.WriteAsync("Hello Paris!"); }); }
  24. 24. Changing Host: OwinHost
  25. 25. Changing Host: OwinHost
  26. 26. Changing Host: Self-Host
  27. 27. Changing Host: Self-Host static void Main(string[] args) { using (WebApp.Start<Startup>("http://localhost:9000")) { Console.WriteLine("Press [enter] to quit..."); Console.ReadLine(); } }
  28. 28. Real World Katana  Just install the framework of choice and use it as before
  29. 29. Real World Katana: WebAPI
  30. 30. Real World Katana: WebAPI public void Configuration(IAppBuilder app) { HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute("default", "api/{controller"); app.UseWebApi(config); }
  31. 31. Katana Diagnostic install-package Microsoft.Owin.Diagnostics
  32. 32. Securing Katana  Can use traditional cookies (Form Authentication)  CORS  Twitter  Facebook  Google  Active Directory
  33. 33. OWIN Middleware
  34. 34. OWIN Middleware: IAppBuilder  Non normative conventions  Formalizes application startup pipeline namespace Owin { public interface IAppBuilder { IDictionary<string, object> Properties { get; } IAppBuilder Use(object middleware, params object[] args); object Build(Type returnType); IAppBuilder New(); } }
  35. 35. Building Middleware: Inline app.Use(new Func<AppFunc, AppFunc>(next => (async env => { var response = environment["owin.ResponseBody"] as Stream; await response.WriteAsync(Encoding.UTF8.GetBytes("Before"),0,6); await next.Invoke(env); await response.WriteAsync(Encoding.UTF8.GetBytes(“After"),0,5); })));
  36. 36. Building Middleware: raw OWIN using AppFunc = Func<IDictionary<string, object>, Task>; public class RawOwinMiddleware { private AppFunc next; public RawOwinMiddleware(AppFunc next) { this.next = next; } public async Task Invoke(IDictionary<string, object> env) { var response = env["owin.ResponseBody"] as Stream; await response.WriteAsync(Encoding.UTF8.GetBytes("Before"),0,6); await next.Invoke(env); await response.WriteAsync(Encoding.UTF8.GetBytes(“After"),0,5); } }
  37. 37. Building Middleware: Katana way public class LoggerMiddleware : OwinMiddleware { public LoggerMiddleware(OwinMiddleware next) : base(next) {} public override async Task Invoke(IOwinContext context) { await context.Response.WriteAsync("before"); await Next.Invoke(context); await context.Response.WriteAsync("after"); } }
  38. 38. A look at the future
  39. 39. Project K and ASP.NET vNext  Owin/Katana is the first stone of the new ASP.NET  Project K where the K is Katana  Microsoft is rewriting from scratch  vNext will be fully OSS (https://github.com/aspnet);  MVC, WEB API and SignalR will be merged (MVC6)  It uses Roslyn for compilation (build on fly)  It runs also on *nix, OSx  Cloud and server-optimized  POCO Controllers
  40. 40. OWIN Succinctly Soon available online on http://www.syncfusion.com/resources/techportal/ebooks
  41. 41. Demo code https://github.com/imperugo/ncrafts.owin.katana
  42. 42. Merci Merci pour nous avoir invités à cette magnifique conférence.

×