42
Katana & OWIN A new lightweight Web server for .NET Simone Chiaretta @simonech http://codeclimber.net.nz Ugo Lattanzi @imperugo http://tostring.it

Owin and Katana

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Owin and Katana

Katana & OWINA new lightweight Web server for .NET

Simone Chiaretta

@simonech

http://codeclimber.net.nz

Ugo Lattanzi

@imperugo

http://tostring.it

Page 2: Owin and Katana

Agenda What is OWIN OWIN Specs Introducing Katana Katana pipeline Using Katana Building OWIN middleware A look at the future

Page 3: Owin and Katana

What is OWIN

Page 4: Owin and Katana

What is OWINOWIN 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

Page 5: Owin and Katana

What is OWINThe 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.

Page 6: Owin and Katana

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“

Page 7: Owin and Katana

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

Page 8: Owin and Katana

Support OWIN/KatanaMany application frameworks support OWIN/Katana

Web API SignalR Nancy ServiceStack FubuMVC Simple.Web RavenDB

Page 9: Owin and Katana

OWIN specs

Page 10: Owin and Katana

OWIN Specs: AppFunc

using AppFunc = Func<IDictionary<string, object>, //

EnvironmentTask>; // Done

http://owin.org

Page 11: Owin and Katana

OWIN Specs: EnvironmentSome 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

Page 12: Owin and Katana

OWIN Specs: LayersHost

• Startup, initialization and process management

Server

• Listens to socket• Calls the first middleware

Middleware

• Pass-through components

Application

• That’s your code

Page 13: Owin and Katana

Introducing Katana aka Fruit Ninja

Page 14: Owin and Katana

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

Page 15: Owin and Katana

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

Page 16: Owin and Katana

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.

Page 17: Owin and Katana

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

Page 18: Owin and Katana

Katana Pipeline

Page 19: Owin and Katana

Katana Pipeline

Host

IIS

OwinHost.exe

Custom Host

Page 20: Owin and Katana

Using Katana

Page 21: Owin and Katana

Hello Katana: Hosting on IIS

Page 22: Owin and Katana

Hello Katana: Hosting on IIS

Page 23: Owin and Katana

Hello Katana: Hosting on IISpublic void Configuration(IAppBuilder app){

app.Use<yourMiddleware>();app.Run(context =>{context.Response.ContentType = "text/plain";

return context.Response.WriteAsync("Hello Paris!");});

}

Page 24: Owin and Katana

Changing Host: OwinHost

Page 25: Owin and Katana

Changing Host: OwinHost

Page 26: Owin and Katana

Changing Host: Self-Host

Page 27: Owin and Katana

Changing Host: Self-Hoststatic void Main(string[] args){     

using (WebApp.Start<Startup>("http://localhost:9000"))

{

Console.WriteLine("Press [enter] to quit...");Console.ReadLine();     

}}

Page 28: Owin and Katana

Real World Katana Just install the framework of choice and use it as before

Page 29: Owin and Katana

Real World Katana: WebAPI

Page 30: Owin and Katana

Real World Katana: WebAPIpublic void Configuration(IAppBuilder app){

HttpConfiguration config = new HttpConfiguration();config.Routes.MapHttpRoute("default", "api/{controller");

app.UseWebApi(config); }

Page 31: Owin and Katana

Katana Diagnosticinstall-package Microsoft.Owin.Diagnostics

Page 32: Owin and Katana

Securing Katana Can use traditional cookies (Form Authentication) CORS Twitter Facebook Google Active Directory

Page 33: Owin and Katana

OWIN Middleware

Page 34: Owin and Katana

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(); }}

Page 35: Owin and Katana

Building Middleware: Inlineapp.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);

})));

Page 36: Owin and Katana

Building Middleware: raw OWINusing 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);}

}

Page 37: Owin and Katana

Building Middleware: Katana waypublic 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");

} }

Page 38: Owin and Katana

A look at the future

Page 39: Owin and Katana

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

Page 40: Owin and Katana

OWIN SuccinctlySoon available online on

http://www.syncfusion.com/resources/techportal/ebooks

Page 41: Owin and Katana

Demo codehttps://github.com/imperugo/ncrafts.owin.katana

Page 42: Owin and Katana

MerciMerci pour nous avoir invités à cette magnifique conférence.