Upload
ugo-lattanzi
View
119
Download
0
Tags:
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
Katana & OWINA new lightweight Web server for .NET
Simone Chiaretta
@simonech
http://codeclimber.net.nz
Ugo Lattanzi
@imperugo
http://tostring.it
Agenda What is OWIN OWIN Specs Introducing Katana Katana pipeline Using Katana Building OWIN middleware A look at the future
What is OWIN
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
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.
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“
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
Support OWIN/KatanaMany application frameworks support OWIN/Katana
Web API SignalR Nancy ServiceStack FubuMVC Simple.Web RavenDB
OWIN specs
OWIN Specs: AppFunc
using AppFunc = Func<IDictionary<string, object>, //
EnvironmentTask>; // Done
http://owin.org
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
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
Introducing Katana aka Fruit Ninja
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
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
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.
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
Katana Pipeline
Katana Pipeline
Host
IIS
OwinHost.exe
Custom Host
Using Katana
Hello Katana: Hosting on IIS
Hello Katana: Hosting on IIS
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!");});
}
Changing Host: OwinHost
Changing Host: OwinHost
Changing Host: Self-Host
Changing Host: Self-Hoststatic void Main(string[] args){
using (WebApp.Start<Startup>("http://localhost:9000"))
{
Console.WriteLine("Press [enter] to quit...");Console.ReadLine();
}}
Real World Katana Just install the framework of choice and use it as before
Real World Katana: WebAPI
Real World Katana: WebAPIpublic void Configuration(IAppBuilder app){
HttpConfiguration config = new HttpConfiguration();config.Routes.MapHttpRoute("default", "api/{controller");
app.UseWebApi(config); }
Katana Diagnosticinstall-package Microsoft.Owin.Diagnostics
Securing Katana Can use traditional cookies (Form Authentication) CORS Twitter Facebook Google Active Directory
OWIN Middleware
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(); }}
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);
})));
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);}
}
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");
} }
A look at the future
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
OWIN SuccinctlySoon available online on
http://www.syncfusion.com/resources/techportal/ebooks
Demo codehttps://github.com/imperugo/ncrafts.owin.katana
MerciMerci pour nous avoir invités à cette magnifique conférence.