SlideShare a Scribd company logo
NoIIS
OWIN, KATANA BY MICROSOFT
Bilal Haidar, http://blog.bilalhaidar.com
NOT ONLY IIS
AGENDA
• Microsoft Web Platform, Brief History
• OWIN is a Specification
• Katana by Microsoft
ASP.NET: BRIEF HISTORY
 ASP.NET first released in early 2002 with the .NET
Framework 1.0
 Optimized for 2 primary customers:
 Classic ASP developers
 Desktop LOB developers
THIS CREATED SOME
CHALLENGES…
 ASP.NET shipped as a part of .NET Framework – slow ship
cycle (time between releases was on the order of years)
 As the ASP.NET requirements grew, so did the size &
complexity of System.Web
 All of the features were turned on by default – you have to
opt out
 Only one hosting option – IIS
REVOLUTIONARY STEPS…
 2007-2008: ASP.NET MVC ships out of band from the .NET
Framework
 Enables more rapid ship cycle
 Open Source!
 Still tied to System.Web 
TOWARDS MODERN WEB
APPS…
“Shift from dynamic, server-generated Web pages to static initial markup
with dynamic sections of the page generated from client-side script
communicating with backend Web APIs through AJAX requests”
 2012: ASP.NET Web API released, NUGET package
 Designed with no dependencies on System.Web
 No dependency on System.Web 
 Includes self-hosting capability (NoIIS)
THE SELF-HOST
REALIZATION…
“The power and flexibility of Web API’s self-hosting capability proved
very attractive to developers who wanted a small, lightweight host of their
services”
 ASP.NET Web API enabled self-hosting
 Other frameworks had or were writing their own self-host
servers (e.g. SignalR)
“A modern Web application generally supports static file serving,
dynamic page generation, Web API, and SignalR.”
WHAT’S NEEDED?
“What was needed was a single hosting abstraction that would enable a
developer to compose an application from a variety of different
components and frameworks, and then run that application on a
supporting host.”
Welcome to OWIN
Open Web Interface for .NET
OWIN
“A specification aiming at creating abstraction between Web servers
and framework components” (http://www.owin.org)
• Inspired by the benefits achieved by Rack & Nodejs
• 2 Microsoft employee behind it (Benjamin Vanderveen & Louis
Dejardin)
• 2 main goals:
• New components could be more easily developed &
consumed
• Apps could be more easily ported between hosts &
potentially entire platforms/OS.
OWIN CORE ELEMENTS
• Environment dictionary
• Application Delegate
OWIN ENVIRONMENT
DICTIONARY
• A data structure: IDictionary<string, object>
• Stores all of the state necessary for processing an HTTP
request & response
“An OWIN-compatible Web server is responsible for populating the
environment dictionary with data such as the body streams and header
collections for an HTTP request and response. It is then the responsibility
of the application or framework components to populate or update the
dictionary values and write to the response body stream”
OWIN ENVIRONMENT
DICTIONARY
Required Key Name Value Description
Yes "owin.RequestBody" A Stream with the request body, if any.
Yes "owin.RequestHeaders" An IDictionary<string, string[]> of request headers.
Yes "owin.RequestMethod"
A string containing the HTTP request method of the request
(e.g., "GET", "POST").
Yes "owin.RequestPath"
A string containing the request path. The path MUST be relative to the "root" of
the application delegate.
Yes "owin.RequestPathBase"
A string containing the portion of the request path corresponding to the "root" of
the application delegate.
Yes "owin.RequestProtocol"
A string containing the protocol name and version
(e.g. "HTTP/1.0" or "HTTP/1.1").
Yes "owin.RequestQueryString"
A string containing the query string component of the HTTP request URI, without
the leading “?” (e.g., "foo=bar&baz=quux"). The value may be an empty string.
Yes "owin.RequestScheme" A string containing the URI scheme used for the request (e.g., "http", "https").
OWIN ENVIRONMENT
DICTIONARY
Required Key Name Value Description
Yes "owin.ResponseBody" A Stream used to write out the response body, if any
Yes "owin.ResponseHeaders" An IDictionary<string, string[]> of response headers.
No "owin.ResponseStatusCode"
An optional int containing the HTTP response status code as defined in RFC
2616 section 6.1.1. The default is 200.
No "owin.ResponseReasonPhrase"
An optional string containing the reason phrase associated the given status
code. If none is provided then the server SHOULD provide a default as described
in RFC 2616section 6.1.1
No "owin.ResponseProtocol"
An optional string containing the protocol name and version
(e.g. "HTTP/1.0" or "HTTP/1.1"). If none is provided then
the “owin.RequestProtocol” key’s value is the default.
Yes "owin.ResponseBody" A Stream used to write out the response body, if any.
Yes "owin.ResponseHeaders" An IDictionary<string, string[]> of response headers.
No "owin.ResponseStatusCode"
An optional int containing the HTTP response status code as defined in RFC
2616 section 6.1.1. The default is 200.
OWIN APPLICATION
DELEGATE
“The primary interface in OWIN is called the application delegate or
AppFunc.
An application delegate takes the IDictionary<string, object> environment
variable and returns a Task when it has finished processing.”
using AppFunc =
Func<IDictionary<string, object>, Task>;
OWIN APPLICATION
DELEGATE
“The application delegate is an implementation of the Func delegate type
where the function accepts the environment dictionary as input and
returns a task.”
“The asynchronous design enables the abstraction to be efficient with its
handling of computing resources, particularly in more I/O intensive
operations.”
“Because the application delegate is an atomic unit of execution and
because the environment dictionary is carried as a parameter on the
delegate, OWIN components could be easily chained together to create
complex HTTP processing pipelines.”
KATANA
 Microsoft’s OWIN implementation
 https://katanaproject.codeplex.com/
 Set of hosts and servers
 IIS or self-host
 Set of convenience classes
 OwinContext, OwinRequest, OwinResponse, etc.
 AppBuilderUseExtensions
 AuthenticationManager
 Set of middleware for common features
 Authentication
 Hosting content (e.g. static files)
 CORS
KATANA WEB STACK
 Host manages process lifetime (start & stop), it could be IIS or any other custom/standalone
executable.
 Server listens for HTTP requests, constructs the environment dictionary, Startups the application &
processes requests through an OWIN pipeline.
 Middleware are specific components that runs in the OWIN pipeline (e.g. WebAPI, SignalR, etc.). A
middleware is simply a component that exposes the application delegate.
 Application is the code you write!
client
Host
OWIN Server
Middleware Middleware Application
KATANA COMPONENTS
Host
Server
Middleware
Application
• IIS
• OwinHost.exe (OwinHost)
• Custom Process (e.g. Console app, Windows
Service)
• SystemWeb (Microsoft.Owin.Host.SystemWeb)
• HttpListener (Microsoft.Owin.Host.HttpListener)
• SignalR)
• Web API
• Nancy (MVC Framework)
• etc.
KATANA NUGET PACKAGES
 Self Hosting / No IIS
 IIS Hosting
PM> Install-Package Microsoft.Owin.SelfHost
Attempting to resolve dependency 'Owin (≥ 1.0)'.
Attempting to resolve dependency 'Microsoft.Owin (≥ 2.1.0)'.
Attempting to resolve dependency 'Microsoft.Owin.Diagnostics (≥
2.1.0)'.
PM> Install-Package Microsoft.Owin.Host.SystemWeb
Attempting to resolve dependency 'Owin (≥ 1.0)'.
Attempting to resolve dependency 'Microsoft.Owin (≥ 2.1.0)'.
KATANA NUGET PACKAGES
 OwinHost / No IIS
PM> Install-Package OwinHost
PM> Install-Package Microsoft.Owin
Attempting to resolve dependency 'Owin (≥ 1.0)'.
KATANA STARTUP
 Startup class
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(IISHosting.Startup))]
namespace IISHosting
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseHandlerAsync((req, res) => {
res.ContentType = "text/plain";
return res.WriteAsync("Hello Katana!");
});
}
}
}
KATANA STARTUP
DETECTION
 Naming Convention: Katana looks for a class named Startup in
namespace matching the assembly name
 OwinStartup Attribute: Katana looks for the OwinStartup attribute to
locate the Startup class. The OwinStartup attribute overrides the naming
convention
 appSetting element in the Configuration file:
The appSetting element overrides the OwinStartup attribute
and naming convention
[assembly: OwinStartup(typeof(StartupDemo.TestStartup))]
<appSettings>
<add key="owin:appStartup" value="StartupDemo.TestStartup" />
</appSettings>
KATANA HOST
(CUSTOM PROCESS)
using System;
using Microsoft.Owin.Hosting;
namespace NoIISHosting
{
public class Program
{
static void Main(string[] args)
{
var baseUrl = "http://localhost:5000";
if (args[0] == "--url") { baseUrl = args[1]; }
using (WebApp.Start<Startup>(new StartOptions(baseUrl) { ServerFactory =
"Microsoft.Owin.Host.HttpListener"}))
{
Console.WriteLine(string.Format("Katana Server running @ {0}", baseUrl));
Console.WriteLine();
// Keep the server going until we're done
Console.WriteLine("Press [Enter] To Exit ...");
Console.ReadKey();
}
}
}
}
Starting SERVER
(HttpListener) & using
Startup
KATANA MIDDLEWARE
 Code to process requests
 A class with a constructor accepting:
 Func<IDictionary<string, object>, Task> (aka “AppFunc”)
representing the NEXT middleware in the pipeline
 Method named Invoke (by convention) accepting as input
IDictionary<string, object>
 One or more Middlewares can be chained in a pipeline
KATANA CONFIGURING
MIDDLEWARE
public class Startup
{
public void Configuration (IAppBuidler app)
{
app.Use(typeof(SomeMiddleware));
}
}
KATANA MIDDLEWARE
using AppFunc = Func<IDictionary<string, object>, Task>;
public class SimpleLogger
{
private readonly AppFunc _next;
private readonly SimpleLoggerOptions _options;
public SimpleLogger(AppFunc next, SimpleLoggerOptions options)
{
_next = next;
_options = options;
}
public async Task Invoke(IDictionary<string, object> environment)
{
foreach (var key in _options.RequestKeys)
{
_options.Log(key, environment[key]);
}
await _next(environment);
foreach (var key in _options.ResponseKeys)
{
_options.Log(key, environment[key]);
}
}
}
public class SimpleLoggerOptions
{
public IList<string> RequestKeys { get; set; }
public IList<string> ResponseKeys { get; set; }
public Action<string, object> Log { get; set; }
}
Request Processing
Response Processing
NEXT Middleware in
Pipeline
AppFunc
KATANA OWINCONTEXT
 An API providing a strongly typed wrapper for
environment dictionary
public async Task Invoke(IDictionary<string, object> env)
{
var ctx = new OwinContext(env);
var url = ctx.Request.Uri;
var accept = ctx.Request.Headers["Accept"];
var username = ctx.Request.User.Identity.Name;
ctx.Response.StatusCode = 200;
ctx.Response.Headers.Add("Myheader", new [] {"cusotm value"});
using (var sw = new StreamWriter(ctx.Response.Body))
{
await sw.WriteLineAsync("<h1>Hello Katana!</h1>");
}
}
SIGNALR 2.0 SELF HOSTING
 Console App hosting the server & configuring signalR in
OWIN pipeline
 Empty Website hosting signalR JS files & HTML
 Start by:
 Create Console App & install Nuget Packages:
PM> Install-Package Microsoft.AspNet.SignalR.SelfHost
PM> Install-Package Microsoft.Owin.Cors
SIGNALR 2.0 SELF HOSTING
 Add Program class to host the HttpListener server
using System;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Owin;
using Microsoft.Owin.Cors;
namespace SignalRSelfHost
{
public class Program
{
static void Main(string[] args)
{
var url = "http://localhost:8080";
using (WebApp.Start(url))
{
Console.WriteLine("Server running on {0}", url);
Console.ReadLine();
}
}
}
}
SIGNALR 2.0 SELF HOSTING
 Add Startup class to configure & add SignalR to OWIN
pipeline
using System;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Owin;
using Microsoft.Owin.Cors;
namespace SignalRSelfHost
{
class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
}
SIGNALR 2.0 SELF HOSTING
 Add MyHub class to communicate with clients connecting
to SignalR server
using System;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Owin;
using Microsoft.Owin.Cors;
namespace SignalRSelfHost
{
public class MyHub : Hub
{
public void Send(string name, string message)
{
Clients.All.addMessage(name, message);
}
}
}
Defined by Server,
Called by Client
Defined by Client,
Called by Server
SIGNALR 2.0 SELF HOSTING
 Add Empty Website to host SignalR JS files & HTML
page
 Install the following Nuget Packages:
 Finally, add an HTML page
PM> Install-Package Microsoft.AspNet.SignalR.JS
SIGNALR 2.0 SELF HOSTING
<script type="text/javascript">
$(function () {
//Set the hubs URL for the connection
$.connection.hub.url = "http://localhost:8080/signalr";
// Declare a proxy to reference the hub.
var chat = $.connection.myHub;
// Create a function that the hub can call to broadcast messages.
chat.client.addMessage = function (name, message) {
// Html encode display name and message.
var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').text(message).html();
// Add the message to the page.
$('#discussion').append('<li><strong>' + encodedName
+ '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
};
// Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#message').focus();
// Start the connection.
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
</script>
KATANA GOALS
 Composability
 Portability
 Performance & Scalability
Server Starts
Your app’s
startup
Auth
Static
files
Web
API
KATANA PORTABILITY
IIS
SystemWeb
Web API + SignalR + static files
Application
Windows (local or remote)
Console App / Owin Host
HttpListener
Web API + SignalR + static files
Application
Windows (local or remote)
OwinHost
HttpSys
Web API + SignalR + static files
Application
Windows Azure Worker Role
KATANA PERF / SCALE
 Composition model gives you control over performance
characteristics
 Async throughout
 Separate server and host components
REFERENCES
 Open Web Interface for .NET
 OWIN & Katana
 Mini-Training OWIN & Katana
 The Katana Project
 Getting Started with the Katana Project
 OWIN and Katana (The new HTTP Host for Web Apps)

More Related Content

PPTX
Introduction to OWIN
PPTX
Owin and katana
PPTX
Owin and Katana
PPTX
Owin from spec to application
PPTX
ASP.NET: Present and future
PPTX
Angular Owin Katana TypeScript
PPT
OWIN (Open Web Interface for .NET)
PPTX
Mini-Training Owin Katana
Introduction to OWIN
Owin and katana
Owin and Katana
Owin from spec to application
ASP.NET: Present and future
Angular Owin Katana TypeScript
OWIN (Open Web Interface for .NET)
Mini-Training Owin Katana

What's hot (20)

PPTX
Owin & katana
PPTX
ASP.NET Core 1.0
PDF
Asp.Net Core MVC , Razor page , Entity Framework Core
PPTX
Introducing ASP.NET Core 2.0
PPTX
Powershell For Developers
PPTX
Building a REST Service in minutes with Spring Boot
ODP
Developing Microservices using Spring - Beginner's Guide
PPTX
Learning ASP.NET 5 and MVC 6
PPTX
ASP.NET Core MVC + Web API with Overview
PPTX
Introducing ASP.NET vNext - A tour of the new ASP.NET platform
PPTX
Debugging your Way through .NET with Visual Studio 2015
PPTX
Angular on ASP.NET MVC 6
PPT
O que é esse tal de OWIN?
PPTX
Laravel introduction
PPTX
Microservices Platform with Spring Boot, Spring Cloud Config, Spring Cloud Ne...
PPTX
ASP.NET Core 2.1: The Future of Web Apps
PDF
Spring Boot
PPTX
Introduction to spring boot
ODP
Integrating Apache Syncope with Apache CXF
PDF
CXF 3.0, What's new?
Owin & katana
ASP.NET Core 1.0
Asp.Net Core MVC , Razor page , Entity Framework Core
Introducing ASP.NET Core 2.0
Powershell For Developers
Building a REST Service in minutes with Spring Boot
Developing Microservices using Spring - Beginner's Guide
Learning ASP.NET 5 and MVC 6
ASP.NET Core MVC + Web API with Overview
Introducing ASP.NET vNext - A tour of the new ASP.NET platform
Debugging your Way through .NET with Visual Studio 2015
Angular on ASP.NET MVC 6
O que é esse tal de OWIN?
Laravel introduction
Microservices Platform with Spring Boot, Spring Cloud Config, Spring Cloud Ne...
ASP.NET Core 2.1: The Future of Web Apps
Spring Boot
Introduction to spring boot
Integrating Apache Syncope with Apache CXF
CXF 3.0, What's new?
Ad

Viewers also liked (8)

PPTX
Real-Time Web Applications with ASP.NET WebAPI and SignalR
PPTX
DevIntersections 2014 Web API Slides
PPTX
Real World Asp.Net WebApi Applications
PPTX
Micro Services in .NET Core and Docker
PDF
How to Make Own Framework built on OWIN
PDF
web apiで遊び倒す
PPTX
Web API or WCF - An Architectural Comparison
PDF
Web 2.0 Business Models
Real-Time Web Applications with ASP.NET WebAPI and SignalR
DevIntersections 2014 Web API Slides
Real World Asp.Net WebApi Applications
Micro Services in .NET Core and Docker
How to Make Own Framework built on OWIN
web apiで遊び倒す
Web API or WCF - An Architectural Comparison
Web 2.0 Business Models
Ad

Similar to OWIN and Katana Project - Not Only IIS - NoIIS (20)

PPTX
A Brief History of OWIN
PPTX
OWIN Web API with Linky
PPTX
Owin and katana overview
PPTX
Owin and-katana-overview
PPTX
Campus days 2014 owin
PPTX
Websites, Web Services and Cloud Applications with Visual Studio
PPTX
Best of Microsoft Dev Camp 2015
PPTX
ASP.NET vNext ANUG 20140817
PPTX
Owin katana en
PPTX
Developing Web Sites and Services using Visual Studio 2013
PDF
OWIN Why should i care?
PPTX
Middleware webnextconf - 20152609
PPTX
Project K, Vnext and Owin
PPTX
Que hay de nuevo en Visual Studio 2013 y ASP.NET 5.1
PPTX
Overview of SignalR
PPTX
Introduction to ASP.NET 5
PDF
Introducing ASP.NET vNext – The Future of .NET on the Server | FalafelCON 2014
PPTX
Intro to.net core 20170111
A Brief History of OWIN
OWIN Web API with Linky
Owin and katana overview
Owin and-katana-overview
Campus days 2014 owin
Websites, Web Services and Cloud Applications with Visual Studio
Best of Microsoft Dev Camp 2015
ASP.NET vNext ANUG 20140817
Owin katana en
Developing Web Sites and Services using Visual Studio 2013
OWIN Why should i care?
Middleware webnextconf - 20152609
Project K, Vnext and Owin
Que hay de nuevo en Visual Studio 2013 y ASP.NET 5.1
Overview of SignalR
Introduction to ASP.NET 5
Introducing ASP.NET vNext – The Future of .NET on the Server | FalafelCON 2014
Intro to.net core 20170111

Recently uploaded (20)

PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Tartificialntelligence_presentation.pptx
PPTX
Spectroscopy.pptx food analysis technology
PPTX
A Presentation on Artificial Intelligence
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Mushroom cultivation and it's methods.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Machine Learning_overview_presentation.pptx
PDF
Approach and Philosophy of On baking technology
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Empathic Computing: Creating Shared Understanding
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Encapsulation_ Review paper, used for researhc scholars
NewMind AI Weekly Chronicles - August'25-Week II
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Tartificialntelligence_presentation.pptx
Spectroscopy.pptx food analysis technology
A Presentation on Artificial Intelligence
A comparative study of natural language inference in Swahili using monolingua...
Mobile App Security Testing_ A Comprehensive Guide.pdf
cloud_computing_Infrastucture_as_cloud_p
Unlocking AI with Model Context Protocol (MCP)
Digital-Transformation-Roadmap-for-Companies.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Mushroom cultivation and it's methods.pdf
Spectral efficient network and resource selection model in 5G networks
Machine Learning_overview_presentation.pptx
Approach and Philosophy of On baking technology
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Empathic Computing: Creating Shared Understanding

OWIN and Katana Project - Not Only IIS - NoIIS

  • 1. NoIIS OWIN, KATANA BY MICROSOFT Bilal Haidar, http://blog.bilalhaidar.com NOT ONLY IIS
  • 2. AGENDA • Microsoft Web Platform, Brief History • OWIN is a Specification • Katana by Microsoft
  • 3. ASP.NET: BRIEF HISTORY  ASP.NET first released in early 2002 with the .NET Framework 1.0  Optimized for 2 primary customers:  Classic ASP developers  Desktop LOB developers
  • 4. THIS CREATED SOME CHALLENGES…  ASP.NET shipped as a part of .NET Framework – slow ship cycle (time between releases was on the order of years)  As the ASP.NET requirements grew, so did the size & complexity of System.Web  All of the features were turned on by default – you have to opt out  Only one hosting option – IIS
  • 5. REVOLUTIONARY STEPS…  2007-2008: ASP.NET MVC ships out of band from the .NET Framework  Enables more rapid ship cycle  Open Source!  Still tied to System.Web 
  • 6. TOWARDS MODERN WEB APPS… “Shift from dynamic, server-generated Web pages to static initial markup with dynamic sections of the page generated from client-side script communicating with backend Web APIs through AJAX requests”  2012: ASP.NET Web API released, NUGET package  Designed with no dependencies on System.Web  No dependency on System.Web   Includes self-hosting capability (NoIIS)
  • 7. THE SELF-HOST REALIZATION… “The power and flexibility of Web API’s self-hosting capability proved very attractive to developers who wanted a small, lightweight host of their services”  ASP.NET Web API enabled self-hosting  Other frameworks had or were writing their own self-host servers (e.g. SignalR) “A modern Web application generally supports static file serving, dynamic page generation, Web API, and SignalR.”
  • 8. WHAT’S NEEDED? “What was needed was a single hosting abstraction that would enable a developer to compose an application from a variety of different components and frameworks, and then run that application on a supporting host.” Welcome to OWIN Open Web Interface for .NET
  • 9. OWIN “A specification aiming at creating abstraction between Web servers and framework components” (http://www.owin.org) • Inspired by the benefits achieved by Rack & Nodejs • 2 Microsoft employee behind it (Benjamin Vanderveen & Louis Dejardin) • 2 main goals: • New components could be more easily developed & consumed • Apps could be more easily ported between hosts & potentially entire platforms/OS.
  • 10. OWIN CORE ELEMENTS • Environment dictionary • Application Delegate
  • 11. OWIN ENVIRONMENT DICTIONARY • A data structure: IDictionary<string, object> • Stores all of the state necessary for processing an HTTP request & response “An OWIN-compatible Web server is responsible for populating the environment dictionary with data such as the body streams and header collections for an HTTP request and response. It is then the responsibility of the application or framework components to populate or update the dictionary values and write to the response body stream”
  • 12. OWIN ENVIRONMENT DICTIONARY Required Key Name Value Description Yes "owin.RequestBody" A Stream with the request body, if any. Yes "owin.RequestHeaders" An IDictionary<string, string[]> of request headers. Yes "owin.RequestMethod" A string containing the HTTP request method of the request (e.g., "GET", "POST"). Yes "owin.RequestPath" A string containing the request path. The path MUST be relative to the "root" of the application delegate. Yes "owin.RequestPathBase" A string containing the portion of the request path corresponding to the "root" of the application delegate. Yes "owin.RequestProtocol" A string containing the protocol name and version (e.g. "HTTP/1.0" or "HTTP/1.1"). Yes "owin.RequestQueryString" A string containing the query string component of the HTTP request URI, without the leading “?” (e.g., "foo=bar&baz=quux"). The value may be an empty string. Yes "owin.RequestScheme" A string containing the URI scheme used for the request (e.g., "http", "https").
  • 13. OWIN ENVIRONMENT DICTIONARY Required Key Name Value Description Yes "owin.ResponseBody" A Stream used to write out the response body, if any Yes "owin.ResponseHeaders" An IDictionary<string, string[]> of response headers. No "owin.ResponseStatusCode" An optional int containing the HTTP response status code as defined in RFC 2616 section 6.1.1. The default is 200. No "owin.ResponseReasonPhrase" An optional string containing the reason phrase associated the given status code. If none is provided then the server SHOULD provide a default as described in RFC 2616section 6.1.1 No "owin.ResponseProtocol" An optional string containing the protocol name and version (e.g. "HTTP/1.0" or "HTTP/1.1"). If none is provided then the “owin.RequestProtocol” key’s value is the default. Yes "owin.ResponseBody" A Stream used to write out the response body, if any. Yes "owin.ResponseHeaders" An IDictionary<string, string[]> of response headers. No "owin.ResponseStatusCode" An optional int containing the HTTP response status code as defined in RFC 2616 section 6.1.1. The default is 200.
  • 14. OWIN APPLICATION DELEGATE “The primary interface in OWIN is called the application delegate or AppFunc. An application delegate takes the IDictionary<string, object> environment variable and returns a Task when it has finished processing.” using AppFunc = Func<IDictionary<string, object>, Task>;
  • 15. OWIN APPLICATION DELEGATE “The application delegate is an implementation of the Func delegate type where the function accepts the environment dictionary as input and returns a task.” “The asynchronous design enables the abstraction to be efficient with its handling of computing resources, particularly in more I/O intensive operations.” “Because the application delegate is an atomic unit of execution and because the environment dictionary is carried as a parameter on the delegate, OWIN components could be easily chained together to create complex HTTP processing pipelines.”
  • 16. KATANA  Microsoft’s OWIN implementation  https://katanaproject.codeplex.com/  Set of hosts and servers  IIS or self-host  Set of convenience classes  OwinContext, OwinRequest, OwinResponse, etc.  AppBuilderUseExtensions  AuthenticationManager  Set of middleware for common features  Authentication  Hosting content (e.g. static files)  CORS
  • 17. KATANA WEB STACK  Host manages process lifetime (start & stop), it could be IIS or any other custom/standalone executable.  Server listens for HTTP requests, constructs the environment dictionary, Startups the application & processes requests through an OWIN pipeline.  Middleware are specific components that runs in the OWIN pipeline (e.g. WebAPI, SignalR, etc.). A middleware is simply a component that exposes the application delegate.  Application is the code you write! client Host OWIN Server Middleware Middleware Application
  • 18. KATANA COMPONENTS Host Server Middleware Application • IIS • OwinHost.exe (OwinHost) • Custom Process (e.g. Console app, Windows Service) • SystemWeb (Microsoft.Owin.Host.SystemWeb) • HttpListener (Microsoft.Owin.Host.HttpListener) • SignalR) • Web API • Nancy (MVC Framework) • etc.
  • 19. KATANA NUGET PACKAGES  Self Hosting / No IIS  IIS Hosting PM> Install-Package Microsoft.Owin.SelfHost Attempting to resolve dependency 'Owin (≥ 1.0)'. Attempting to resolve dependency 'Microsoft.Owin (≥ 2.1.0)'. Attempting to resolve dependency 'Microsoft.Owin.Diagnostics (≥ 2.1.0)'. PM> Install-Package Microsoft.Owin.Host.SystemWeb Attempting to resolve dependency 'Owin (≥ 1.0)'. Attempting to resolve dependency 'Microsoft.Owin (≥ 2.1.0)'.
  • 20. KATANA NUGET PACKAGES  OwinHost / No IIS PM> Install-Package OwinHost PM> Install-Package Microsoft.Owin Attempting to resolve dependency 'Owin (≥ 1.0)'.
  • 21. KATANA STARTUP  Startup class using Microsoft.Owin; using Owin; [assembly: OwinStartup(typeof(IISHosting.Startup))] namespace IISHosting { public class Startup { public void Configuration(IAppBuilder app) { app.UseHandlerAsync((req, res) => { res.ContentType = "text/plain"; return res.WriteAsync("Hello Katana!"); }); } } }
  • 22. KATANA STARTUP DETECTION  Naming Convention: Katana looks for a class named Startup in namespace matching the assembly name  OwinStartup Attribute: Katana looks for the OwinStartup attribute to locate the Startup class. The OwinStartup attribute overrides the naming convention  appSetting element in the Configuration file: The appSetting element overrides the OwinStartup attribute and naming convention [assembly: OwinStartup(typeof(StartupDemo.TestStartup))] <appSettings> <add key="owin:appStartup" value="StartupDemo.TestStartup" /> </appSettings>
  • 23. KATANA HOST (CUSTOM PROCESS) using System; using Microsoft.Owin.Hosting; namespace NoIISHosting { public class Program { static void Main(string[] args) { var baseUrl = "http://localhost:5000"; if (args[0] == "--url") { baseUrl = args[1]; } using (WebApp.Start<Startup>(new StartOptions(baseUrl) { ServerFactory = "Microsoft.Owin.Host.HttpListener"})) { Console.WriteLine(string.Format("Katana Server running @ {0}", baseUrl)); Console.WriteLine(); // Keep the server going until we're done Console.WriteLine("Press [Enter] To Exit ..."); Console.ReadKey(); } } } } Starting SERVER (HttpListener) & using Startup
  • 24. KATANA MIDDLEWARE  Code to process requests  A class with a constructor accepting:  Func<IDictionary<string, object>, Task> (aka “AppFunc”) representing the NEXT middleware in the pipeline  Method named Invoke (by convention) accepting as input IDictionary<string, object>  One or more Middlewares can be chained in a pipeline
  • 25. KATANA CONFIGURING MIDDLEWARE public class Startup { public void Configuration (IAppBuidler app) { app.Use(typeof(SomeMiddleware)); } }
  • 26. KATANA MIDDLEWARE using AppFunc = Func<IDictionary<string, object>, Task>; public class SimpleLogger { private readonly AppFunc _next; private readonly SimpleLoggerOptions _options; public SimpleLogger(AppFunc next, SimpleLoggerOptions options) { _next = next; _options = options; } public async Task Invoke(IDictionary<string, object> environment) { foreach (var key in _options.RequestKeys) { _options.Log(key, environment[key]); } await _next(environment); foreach (var key in _options.ResponseKeys) { _options.Log(key, environment[key]); } } } public class SimpleLoggerOptions { public IList<string> RequestKeys { get; set; } public IList<string> ResponseKeys { get; set; } public Action<string, object> Log { get; set; } } Request Processing Response Processing NEXT Middleware in Pipeline AppFunc
  • 27. KATANA OWINCONTEXT  An API providing a strongly typed wrapper for environment dictionary public async Task Invoke(IDictionary<string, object> env) { var ctx = new OwinContext(env); var url = ctx.Request.Uri; var accept = ctx.Request.Headers["Accept"]; var username = ctx.Request.User.Identity.Name; ctx.Response.StatusCode = 200; ctx.Response.Headers.Add("Myheader", new [] {"cusotm value"}); using (var sw = new StreamWriter(ctx.Response.Body)) { await sw.WriteLineAsync("<h1>Hello Katana!</h1>"); } }
  • 28. SIGNALR 2.0 SELF HOSTING  Console App hosting the server & configuring signalR in OWIN pipeline  Empty Website hosting signalR JS files & HTML  Start by:  Create Console App & install Nuget Packages: PM> Install-Package Microsoft.AspNet.SignalR.SelfHost PM> Install-Package Microsoft.Owin.Cors
  • 29. SIGNALR 2.0 SELF HOSTING  Add Program class to host the HttpListener server using System; using Microsoft.AspNet.SignalR; using Microsoft.Owin.Hosting; using Owin; using Microsoft.Owin.Cors; namespace SignalRSelfHost { public class Program { static void Main(string[] args) { var url = "http://localhost:8080"; using (WebApp.Start(url)) { Console.WriteLine("Server running on {0}", url); Console.ReadLine(); } } } }
  • 30. SIGNALR 2.0 SELF HOSTING  Add Startup class to configure & add SignalR to OWIN pipeline using System; using Microsoft.AspNet.SignalR; using Microsoft.Owin.Hosting; using Owin; using Microsoft.Owin.Cors; namespace SignalRSelfHost { class Startup { public void Configuration(IAppBuilder app) { app.UseCors(CorsOptions.AllowAll); app.MapSignalR(); } } }
  • 31. SIGNALR 2.0 SELF HOSTING  Add MyHub class to communicate with clients connecting to SignalR server using System; using Microsoft.AspNet.SignalR; using Microsoft.Owin.Hosting; using Owin; using Microsoft.Owin.Cors; namespace SignalRSelfHost { public class MyHub : Hub { public void Send(string name, string message) { Clients.All.addMessage(name, message); } } } Defined by Server, Called by Client Defined by Client, Called by Server
  • 32. SIGNALR 2.0 SELF HOSTING  Add Empty Website to host SignalR JS files & HTML page  Install the following Nuget Packages:  Finally, add an HTML page PM> Install-Package Microsoft.AspNet.SignalR.JS
  • 33. SIGNALR 2.0 SELF HOSTING <script type="text/javascript"> $(function () { //Set the hubs URL for the connection $.connection.hub.url = "http://localhost:8080/signalr"; // Declare a proxy to reference the hub. var chat = $.connection.myHub; // Create a function that the hub can call to broadcast messages. chat.client.addMessage = function (name, message) { // Html encode display name and message. var encodedName = $('<div />').text(name).html(); var encodedMsg = $('<div />').text(message).html(); // Add the message to the page. $('#discussion').append('<li><strong>' + encodedName + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>'); }; // Get the user name and store it to prepend to messages. $('#displayname').val(prompt('Enter your name:', '')); // Set initial focus to message input box. $('#message').focus(); // Start the connection. $.connection.hub.start().done(function () { $('#sendmessage').click(function () { // Call the Send method on the hub. chat.server.send($('#displayname').val(), $('#message').val()); // Clear text box and reset focus for next comment. $('#message').val('').focus(); }); }); }); </script>
  • 34. KATANA GOALS  Composability  Portability  Performance & Scalability
  • 36. KATANA PORTABILITY IIS SystemWeb Web API + SignalR + static files Application Windows (local or remote) Console App / Owin Host HttpListener Web API + SignalR + static files Application Windows (local or remote) OwinHost HttpSys Web API + SignalR + static files Application Windows Azure Worker Role
  • 37. KATANA PERF / SCALE  Composition model gives you control over performance characteristics  Async throughout  Separate server and host components
  • 38. REFERENCES  Open Web Interface for .NET  OWIN & Katana  Mini-Training OWIN & Katana  The Katana Project  Getting Started with the Katana Project  OWIN and Katana (The new HTTP Host for Web Apps)