Download - Campus days 2014 owin
#CampusDays
OWIN & Composable Web Apps
About me
Christian Horsdal
Independent Consultant
www.horsdal-consult.dk
@chr_horsdal
#C
am
pusD
ays
Flexible
Componentized
Reusable
Testable
#C
am
pusD
ays
DemoHello OWIN World
Web Applications Handle HTTP Requests
5
6
Handler
Log request
Log response
Authenticate
Open DbContext
Close DbContext
Things You Might Do In the Pipeline
Open & dispose resources
Logging
Authentication
Authorization
Quick monitoring endpoints
Sanitize inputs
Content Negotiation
CORS
7
Things You Might Do In the Pipeline
Open & dispose resources
Logging
Authentication
Authorization
Quick monitoring endpoints
Sanitize inputs
Content Negotiation
CORS
8
Cross-cutting concerns
Re-use
Test separately
Update separately
Distribute separately
Do One Thing And Do It Well
9
Compose Pipeline However You Want
10
11
Handler
Log request
Log response
Authenticate
Open DbContext
Close DbContext
Minify HTML
OWIN
OWIN
• Open Web Interfaces for .NET
• Community driven standard
• Defines mechanics of pipeline
13
OWIN Terminology
Server
• The web server
•And/or an OWIN adapter
Middleware
• Those pieces in the pipeline
Application
• The handler
14
No shared assembly
OWIN Terminolgy 16
using AppFunc = Func<IDictionary<string, object>, Task>;
OWIN – Request Environment
Key Name
"owin.RequestBody"
"owin.RequestHeaders"
"owin.RequestMethod"
"owin.RequestPath"
"owin.RequestPathBase"
"owin.RequestProtocol"
"owin.RequestQueryString"
"owin.RequestScheme"
OWIN – Response Environment
Required Key Name
Yes "owin.ResponseBody"
Yes "owin.ResponseHeaders"
No "owin.ResponseStatusCode"
No "owin.ResponseReasonPhrase"
No "owin.ResponseProtocol"
OWIN Terminology 19
using MidFunc = Func<AppFunc, AppFunc>;
Handler
Log request
Log response
Authenticate
Open DbContext
Close DbContext
OWIN “Terminolgy” 20
using BuildFunc = Action<MidFunc>;
No shared assembly
22
#C
am
pusD
ays
DemoWrite an OWIN Middleware
23
24
OWIN and Web Frameworks
Compatible Frameworks
• ASP.NET MVC 6
• WebApi
• Nancy
• Simple.Web
25
ASP.NET 5 26
public class Startup{
public void Configure(IApplicationBuilder app){app.UseOwin();
}}
ASP.NET < 5 + Katana 27
public class Startup{
public void Configuration(IAppBuilder builder){
builder.Use(new MidFunc(next => (AppFunc)Invoke));}
public Task Invoke(IDictionary<string, object> environment) {…
}}
Katana 28
public class Startup{
public void Configuration(IAppBuilder app){app.Run(Invoke);
}
public Task Invoke(IOwinContext context){context.Response.ContentType = "text/plain";return context.Response.WriteAsync("Hello World");
}}
WebApi + Katana 29
Katana startup.cs code
public class Startup{public void Configuration(IAppBuilder builder){
HttpConfiguration config = new HttpConfiguration();config.Routes
.MapHttpRoute("Default", "{controller}/{id}",new { controller = "Home", id = RouteParameter.Optional
config.Formatters.XmlFormatter.UseXmlSerializer = true;config.Formatters.Remove(config.Formatters.JsonFormatter);
builder.UseWebApi(config);}
}
OWIN Testing
Integration test the complete pipeline
Integration test parts of the pipeline
While in process
30
31
Handler
Log request
Log response
Authenticate
Open DbContext
Close DbContext
Minify HTML
#C
am
pusD
ays
DemoUse our middleware with an app
32
#C
am
pusD
ays
Flexible
Componentized
Reusable
Testable
Christian Horsdal
@chr_horsdal
www.horsdal-consult.dk
EVENT SPONSORER
EXPO SPONSORER
TRACK SPONSORER
#C
am
pusD
ays
Q&A#Ask me about everything!
Join me at the Microsoft Booth the next 30 minutes @Meet The Experts
Dont forget to: Evaluate this session!