Owin is the new open source standard for building and hosting web applications, bringing huge improvements to web development both in terms of flexibility and developer productivity. It decouples ASP.NET applications from IIS, allowing sites to be hosted on a variety of servers. It also allows for simple chaining of middleware together into a pipeline, so that infrastructure code can be broken down into separate and reusable modules. Thanks to Owin, it's now much easier to put together a web application that meets the precise needs of a customer and runs in in their environment of choice.
This article will solve a particular problem that may be encountered when moving to Owin for applications using Microsoft's Unity dependency container.
Unity allows object lifetimes to be tied to a single request by using 'PerRequestLifetimeManager' when registering types:
However, separate cleanup code is required. Traditionally, the developer would register the Http Module `UnityPerRequestHttpModule`. This is no longer an option with Owin. Http Modules are a tool from IIS (Internet Information Services), which is the platform specific detail the application has been decoupled from.
The simplest option is to omit the cleanup up. I’ve created a simple website as a demonstration of this (available at https://github.com/NathanLBCooper/OwinPerRequestUnityMiddleware). The screenshot shows the site running with the naïve use of PerRequestLifetimeManager, without the http module or other cleanup code. The number in in the right hand column shows how many objects are awaiting disposal.
There is a serious problem here. While the garbage collector will eventually deal with these 40 objects, they have all been temporarily leaked. The object behind the scenes here is trivially simple, but this leaking could possibly cause serious problems in a real application. Luckily, there is a simple solution and that is to harness Owin’s powerful middleware.
The first thing that needs to be done is to write the cleanup procedure. Here is some simple code to pull the per-request objects from the container and dispose them:
The cleanup procedure needs to go into the Owin Pipeline so that it runs after each request. Below is a diagram of showing when middleware is invoked. It forms a chain of execution, the request is passed to the application and the response is passed back.
The cleanup code needs to run after the request has been deal with, as the response propagates out. Here is an implementation of that:
The middleware is now complete. In order to use it the application, we need to register it in the Owin Startup class. Before doing that let’s write an extension method that we call to register the middleware. I think it’s useful to keep track of registered middleware by adding a key to IAppBuilder.Properties, which will be a responsibility of this method.
Now to call this method from the Startup class:
And that is it. The middleware is in place and the objects are now being properly disposed of. The example web application will now show one not yet disposed object, which is exactly the number needed to deal with the request. The application can continue to use both Unity and Owin without any unpleasant resource leaks.
The middleware is also available as a nuget package at: https://www.nuget.org/packages/RequestLifetimeMiddleware/ together with instructions for installation.
All source code is available at: https://github.com/NathanLBCooper/OwinPerRequestUnityMiddleware.