user1620696
user1620696

Reputation: 11405

OWIN and Katana - What the middlewares really are?

When working with Katana Project we deal a lot with middlewares. On ASP.NET website they say

As previously mentioned, when the server accepts a request from a client, it is responsible for passing it through a pipeline of OWIN components, which are specified by the developer’s startup code. These pipeline components are known as middleware.

That's fine but I quite don't get it. At first I thought that middlewares were the ASP.NET components like WebAPI, SignalR and all of that. However, when studying authentication I saw the Cookie Authentication Middleware. This one is not an entire framework like WebAPI so it doesn't fit my initial idea of middleware.

So what Katana middlewares really are? They are just pieces of code that can be integrated on the execution pipeline and do work on the environment dictionary? And so, they can be simple components like an authentication middleware or interfaces to communicate with entire frameworks like WebAPI?

Upvotes: 3

Views: 912

Answers (2)

panesofglass
panesofglass

Reputation: 267

Middlewares are as simple as thinking of mathematical function composition. The OWIN (nearly) spec'd signature is Func<AppFunc, AppFunc>, where using AppFunc = Func<IDictionary<string, object>, Task>.

If you think in terms of function composition, the purpose becomes clear:

val f : int -> int
let f x = x*x

val g : int -> int
let g x = x+x

You can call these manually:

val result1 : int -> int
let result1 x = g(f(x))

Or you can compose the functions to create a new function:

val result1 : int -> int
let newFunc = g • f
// or in F#
let newFunc = g << f

Back to OWIN, again using F# notation to keep it simple:

type AppFunc = IDictionary<string, obj> -> Task
val app : AppFunc
val middleware : AppFunc -> AppFunc

Applying my app to middleware creates a new app':

let app' : AppFunc = middleware app

A concrete example is that of a logging middleware. Following the composition, you will find that the request flows like this:

request -> loggingMiddleware -> app -> loggingMiddleware -> response

giving you an opportunity to log both the incoming request and outgoing response. This is effectively the same as Web API's HttpMessageHandler.

Katana makes this simpler for C# developers through IAppBuilder and the .Use extension, as well as each middleware library exposing a shortcut extension method, such as .UseWebApi or .UseSignalR.

Also, Katana uses middlewares to mount frameworks so that it can use a fall-through mechanism, based on a 404 response status code, to try to handle the request with another framework. You can handle this differently by just mounting frameworks at different paths, but this mechanism works well if you want to handle different parts of your app under a unified path using different frameworks.

Upvotes: 6

Lex Li
Lex Li

Reputation: 63264

If you are familiar with ASP.NET application lifecycle and its processing pipeline,

http://www.iis.net/learn/application-frameworks/building-and-running-aspnet-applications/how-to-take-advantage-of-the-iis-integrated-pipeline

You probably get some basic ideas about what is the middleware. The pipeline itself (mainly types in System.Web) is a middleware, that bridges your ASP.NET applications (WebForms, MVC) to the underlying host (web servers, such as IIS, IIS Express, Cassini, selfhost and so on).

However, the classic System.Web is highly coupled, and then comes OWIN and Katana. If you do dive into Katana's code base, you will see itself is a pipeline. It is much more flexible and highly customizable, so saying it as middleware is now more concise than ever.

ASP.NET vNext completely gets rid of System.Web, so that you can see how Katana plays an important role in the next few months.

Upvotes: 2

Related Questions