Disposing resources at the end of ASP.NET Core request

Today, let’s have a look at dealing with disposable objects in ASP.NET Core. Imagine you have an object that you’d like to reuse throughout the duration of the HTTP request, but at the end, it must clean up some of its open resources or handles. This is a common requirement for i.e. file writers, DB connections, other unmanaged resources or external processes – to mention just a few.

With ASP.NET Core DI

Typically, if you rely on the ASP.NET Core Dependency Injection capabilities, you can achieve that by registering the dependency as “Scoped”, and then just resolve/inject it wherever needed.

This also means that if the service is disposable, that is, needs to clean up some of the resources it opened, that will also happen for you automatically.

With RegisterForDispose

A different way of handling this requirement, is to use an ASP.NET Core feature that allows you to explicitly register resources for disposal at the end of the HTTP request/response pipeline. This is done via HttpResponse object, which exposes a RegisterForDispose method. It can be very useful when you are creating disposable objects on the fly, rather than wrapping them into services or factories that are managed by the DI container.

You can pass any IDisposable to the RegisterForDispose method, and ASP.NET Core would guarantee it is going to be disposed.

It is worth mentioning, that this is a similar functionality to the one that existed in the past in ASP.NET Web API.

The example of using RegisterForDispose is shown below, in this case, in a middleware component.

The snippet above, creates a temporary file inside a middleware component, and writes something to it. The writer is then not disposed straight away (notice the lack of using block), but rather registered for disposal and saved into the HttpContext.Items so that it can be used in other places of the execution pipeline – i.e. inside an MVC filter, MVC controller or other middleware components.

For example, a controller can now unpackage the disposable writer and continue using it:

We could even simplify the code a bit, and add an extension method that would chain the creation of the IDisposable and its registration for disposal.

This way, we can now write code the following way:

Which, in my view, is a little more expressive of the intent – clearly stating that the object created here, will be disposed.

This technique (RegisterForDispose) is used internally by ASP.NET Core for a number of things – for example, to support buffered file streams on file uploads, or to ensure the WindowsIdentity gets properly disposed in case of IIS integration.

Be Sociable, Share!

  • http://iamfreee.github.io Carlos Florencio

    Very nice tip! Thanks

  • clement_911

    Nice one!
    On a related topic, do you know if there is a way to tell the asp.net core DI system to eagerly instantiate a singleton service, rather than delaying untilthe first time it is needed? Aufofac has the IStartable interface but I can’t find any equivalent in the asp.net core di system… http://docs.autofac.org/en/latest/lifetime/startup.html
    Thanks

  • http://IndexOutOfRange.com szymon warda

    Excellent pattern when dealing with async.
    Thanks