• about reply
Solidsoft Reply Logo
Menu
  • What we do
  • Pharmaceutical Sector
  • The Solid Blog
  • Newsroom
  • Contact Us
  • about Reply
Solidsoft Reply Logo

Search

Focus On

Blog

Managing object lifetimes with Owin and Unity

Author: Nathan Cooper

FOCUS ON: Blog,

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:

Managing object lifetimes with Owin and Unity

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.

Managing object lifetimes with Owin and Unity

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:

Managing object lifetimes with Owin and Unity

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.

Managing object lifetimes with Owin and Unity

The cleanup code needs to run after the request has been deal with, as the response propagates out. Here is an implementation of that:

Managing object lifetimes with Owin and Unity

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.

Managing object lifetimes with Owin and Unity

Now to call this method from the Startup class:

Managing object lifetimes with Owin and Unity

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.

Managing object lifetimes with Owin and Unity

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.

RELATED CONTENTS

11.07.2017

Blog

Comparison between DPA & GDPR

Before we start comparing, it’s worth highlighting that this is a bit of an “oranges & apples” thing because, technically speaking, the UK DPA (Data Protection Act) 1998 was enacted to bring British law into line with the 1995 EU DPD (Data Protection Directive, aka 95/46/EC) which is the one that is, now, being repealed and superseded by the GDPR (General Data Protection Regulation, aka 2016/679) that was adopted in 2016.

28.07.2017

Blog

MODERN VS TRADITIONAL TECHNOLOGY INTEGRATION

Every business today is looking for ways to unlock the value of their data, connect disparate systems, increase efficiency, optimise the use of current IT while at the same time keeping the organisation secure. Why? Because competitive advantage, positive customer experience and a bulging bottom line are all dependent on it.

20.07.2017

Blog

5 REAL-WORLD DRIVERS OF MODERN INTEGRATION

Technology is becoming increasingly flexible, mobile and connected- the evidence is probably either in your pocket or your hand.
Smart devices are now built around a huge number of modern applications, many of which are tied directly into business functions – in essence the line between consumer and business has blurred.

 
 
 
 
Reply ©​​ 2023​ - Company Information -
 PrivacyCookie Settings​
  • About Reply​​​
  • Inves​tors​​
  • Newsroom
  • Follow Reply on
  • ​
  • ​
​
  • ​About Solidsoft Reply
  • Privacy & Cookies Policy
  • Information (Client)
  • Information (Supplier)​