Understanding ASP.Net - Part1- Owin and Katana Introduction
Introduction
In this
series of articles, we’ll learn all about OWIN features included in ASP.Net 4
and above. We will learn what OWIN is, how it works with ASP.Net and what
features it provides.
From
past few years web application development has been extremely evolved and its
moving from monolithic frameworks toward more modular, loosely coupled
individual working components that can be used and swapped easily without
effecting rest of the application. Basically, the architecture says, use only
what you need and this gives us benefits like better performance and
flexibility.
ASP.Net
web stack including ASP.Net (MVC, WebForms, Web Pages, Web APIs, SignalR) is dependent
on System.Web assembly, in fact every feature of ASP.NET is written in
System.Web and System.Web assembly is part of .Net framework. This is where
problems start occurring because we can’t add new functionality without a new
framework release. As .Net is huge and it has other components as well besides
ASP.Net so frequent release of a new framework is not possible. Which means we
can’t frequently add new features into ASP.Net in this modern evolutionary era
of web technologies. Another problem of ASP.Net is its hard reliance on ISS.
ISS is Microsoft Internet Information Services which provides hosting services
for .Net web apps at production.
Due to rapid change in web technologies Asp.Net team is also changing
the framework toward modern web development architecture which contains
individual working pluggable components that can be used and swapped with each
other independently from host. This is where OWIN plays its role and it
provides solution to problems that we have with existing architecture. Now
let’s see how it fits into picture.
What OWIN
is?
OWIN is an acronym which stands for Open Web Interface for
.Net. The name doesn’t really explain what OWIN is but instead it gives us some
hints, that it’s something related to Web
and its Open and it’s for .Net.
The www.owin.org website gave
following definition of OWIN,
The goal of OWIN is to decouple a web application
from a web server so that we don’t have to worry about how the application will
be deployed instead we just focus on solutions of our problems. OWIN provides
an abstraction layer b/w a web server and a web application and now the
question is how someone abstract a web server from web application.
Abstracting away a web server from a web application
will be for sure a very complex job and it should be understandable to the average
developer. But OWIN made it very simple and it abstracted a web server with a
standard .Net delegate called AppFunc that takes System.Collections.Generic. IDictionary<string, object> as an argument and returns a System.Threading.Tasks.Task.
Every OWIN middleware takes an AppFunc as parameter to
communicate with each other and with the Server as well. The dictionary object is
called environment dictionary and it contains every information about http
request that server passed to application n and
the returning task object tells the server when the application has done
processing.
The goal of OWIN is to decouple a web application from a web server so that we don’t have to worry about how the application will be deployed instead we just focus on solutions of our problems. OWIN provides an abstraction layer b/w a web server and a web application and now the question is how someone abstract a web server from web application.
Remember OWIN is just a specification it’s not a framework
that you can download and install or get it from NuGet Package Manager. It’s
just a specification and it can have many implementations. Katana is one such
implementation of OWIN that Microsoft written for .Net web developers and
introduced it into ASP.Net 4 framework release and ASP.Net Core is way ahead in
that direction.
Parts of OWIN
Application
Now we know about what Owin is and functionality it provides,
now let’s look one by one at different parts involved in execution of OWIN app
from starting the app to receiving incoming http requests and return http responses.
Host: Host is
someone that hosts everything else and that starts application and other parts
of it. For an OWIN host can be any running windows process from console app to
windows service and even IIS.
Server: Server is
responsible for listening incoming http requests and returning responses to
client. In case of IIS both server and host is IIS.
Pipeline: Server
passes the request into application through a pipeline of OWIN middlewares
using AppFunc. OWIN middleware is a self-contained piece of code that can
inspect and modify both incoming requests and outgoing responses. When a
middleware receives a request using AppFunc it perform some processing and when
it completes its work it passes the dictionary object to next middleware. The
idea is very similar to http modules but instead OWIN middleware is not event
driven and its independent of IIS.
Application: Application is part of the system that is responsible
for generating the actual response for application that will be sent back to
client. Technically there is no difference between an OWIN middleware and an
application despite of application is being intended to generate response. So, these are the parts that are involved in
building an OWIN based application.
Application Flow
Let’s take a step by step tour of an OWIN based application
from incoming request to outgoing response.
Step #1: At first a
client of some kind creates a connection to server and sends an http request.
Step #2: When the
server receives request from client it then parses the request into predefined
pieces of information, store them into dictionary object with specific keys and
passes it to application.
Step #3: The application
then passes the environment dictionary to the first OWIN middleware in pipeline
to do its things. The middleware returns a System.Threading.Tasks.Task to tell the server when it’s completed its
processing.
Remember OWIN is also part of the application.
Step #4: When first
middleware completed its processing it then passes the dictionary object to
next middleware in pipeline and so on until the request reaches at end of
pipeline. When application part of the system receives dictionary object from
last middleware in pipeline it then generates response for client based on request
information in object.
Step #5: When
application done preparing response and adding necessary response headers it
then signals the web server to do its first write to response stream. The
server then first sends response headers information to client available at
that point. After that no one can modify response headers information because its
already sent to the client but instead response body can be modified because
connection to response stream is still opened.
Step #6: At this
point application part of system passes the dictionary object to the last
middleware pipeline allowing it to do its things. Remember at this point
connection to response stream is still opened and OWIN middlewares can happily write
into response stream. Whatever will be written to response stream will be sent
back to client as part of application response.
When the last middleware done its processing, it passes
dictionary object to previous middleware and so on until dictionary object
reaches the beginning of pipeline.
Step #7: Once the
dictionary object reaches at the beginning of pipeline then application
notifies the server that it completed its processing by the returning System.Threading.Tasks.Task.The server then sends response back to client before closing connection to response
stream. As I told earlier application and middleware are exactly same
things despite the fact that application is specifically built for generating
response. A middleware can also short circuit the pipeline and generate and
return the response to client before the request reached the application.
Environment
Dictionary Keys
Owin
specification defined a lot of keys that will be available in environment
dictionary when server passes request to OWIN middleware using AppFunc. Here
are some keys that you will be using in OWIN environment dictionary object and
the names of keys are nice looking self-explanatory.
- owin.RequestPath(string): contains information about requested
path.
- owin.RequestHeaders(IDictionary<string,string[]>):
contains information
about request headers.
- owin.RequestBody(Stream): contains a stream connection to
request body.
- owin.ResponseStatusCode(Int32): contains response status code
information.
- owin.ResponseHeaders(IDictionary<string,string[]>):
contains information
about response headers.
- owin.ResponseBody(Stream): contains the actual response body
Stream connection.
You can see
all the keys start with owin prefix so we may not mistakenly override any key
already defined in environment dictionary.
In
environment dictionary, there is also key defined for a call back to register
and this call back “OnSendingHeaders” will be called before when the server
will send response headers to client. Registering to call back will give use a
nice opportunity to modify the response headers at last point when they will be
just about sent to the client.
·
[server:OnSendingHeaders
(Action<Action<object>,object>)]
There is
also a key defined to access the version of OWIN to enable and disable specific
features base on different versions of OWIN.
·
owin.Version(string)
What Project
Katana is?
As I have mentioned earlier OWIN is just specification.
Project Katana is Microsoft implementation of OWIN specification for .Net web
developers. Katana is an open source project and is available at https://github.com/aspnet/AspNetKatana/. Project
Katana is not by the book implementation of OWIN instead people at Microsoft
added some extra features into it for the ease of use to developers. Owin is
already included for use in ASP.Net 4 with Project Katana and it’s the integral
part of framework now. Some identity related features of ASP.Net are
implemented as OWIN middlewares and available at Nuget to download. ASP.Net
identity features like authentication and authorization are implemented as OWIN
middlewares so it can be used with any OWIN based technology. Let me show you
inside look of an ASP.Net MVC project with framework release ASP.Net 4 and above.
Now Let’s just create a new ASP.Net MVC application with
individual user accounts selected as authentication type.
Now press Ok to create the project, once the project is ready
open packages.config file from solution explorer.
Inside the file, you will see some OWIN packages are already
installed in your project and most of them are identity related that will be
used to add OWIN based authentication and authorization features in project.
To manage the length
of this article we have to wrap up by discussing the summary of what we have
learned so far. In the next article, we will learn how we can build our own OWIN
pipeline and middlewares. I will be writing more on OWIN features included in ASP.Net
4 and above, So I’ll see you in next writing.
Summary
In this writing we have discussed following
concepts briefly.
- · Modern web development architecture is based on individual working components decoupled from other parts of the application.
- · The goal of OWIN is to decouple .Net web applications from web servers.
- · OWIN application has different working parts like (Host, Server, Middleware Pipeline, Application).
- · Katana is Microsoft implementation of OWIN specification built for .Net web developers and its open source and available at Github.
Comments
Post a Comment