Unity and TransientLifetimeManager
Asked Answered
F

2

7

This is a small question, just to make sure I'm understanding Unity correctly.

I'm using Unity in an ASP.NET MVC application, and I have registered a type as follows:

 container.RegisterType<IPizzaService, PizzaService>(); 

And I'm using it in a controller like:

public class PizzaController : Controller
{
    private IPizzaService _pizzaService;

    public PizzaController(IPizzaService pizzaService)
    {
        _pizzaService = pizzaService;
    }

    [HttpGet]
    public ActionResult Index()
    {
        var pizzasModel = _pizzaService.FindAllPizzas();
        ...        
    }        
}

Every time a page request is done, a new instance of IPizzaService is injected and used. So this all works fine.

My question: do I have to do anything special to dispose this instance? I assume that, once the request has ended, the controller is disposed and the PizzaService instance eventually gets garbage collected.

If I need deterministic disposal of an instance because it uses an entity framework context or an unmanaged resource for example, I have to override Dispose of the controller, and there make sure I call the dispose of the instances myself.

Right? If not, please explain why :)

Thanks!

Fernald answered 30/10, 2014 at 10:19 Comment(2)
Lots of information here but I don't think the Transient manager requires you to dispose it yourself.Mercedes
@JeroenVannevel Actually, the TransientManager does not call dispose for you.Nucleon
N
1

IMO, whatever creates a disposable object is responsible for disposing it. When the container injects a disposable object via RegisterType<I, T>(), I want to guarantee that the object is ready to be used. However, using RegisterInstance<I>(obj) does dispose of your object automatically.

This can be difficult with an IOC container, and is impossible with Unity out of the box. However, there is some really nifty code out there that I use all the time:

http://thorarin.net/blog/post/2013/02/12/Unity-IoC-lifetime-management-IDisposable-part1.aspx

The blog has code for a DisposingTransientLifetimeManager and DisposingSharedLifetimeManager. Using the extensions, the container calls Dispose() on your disposable objects.

One caveat is that you'll need to reference the proper (older) version of Microsoft.Practices.Unity.Configuration.dll & Microsoft.Practices.Unity.dll.

Nucleon answered 30/10, 2014 at 11:2 Comment(4)
Thanks, but in the specific case I described in my question, is there an explicit dispose needed or will the garbage collector eventually dispose it?Fernald
The GC will eventually dispose everything. But that could be a long time. Garbage collection is managed within a process. In IIS, one process = one application pool, but possibly more than one .NET application domain (web sites can share the same application pool). Therefore, one GC may be shared among several web requests. So you should continue to dispose of resources as soon as you can, instead of waiting for the GC to do it.Nucleon
So that means that all examples showcasing Unity and TransientLifetimeManager are actually doing it wrong? Then, in what cases is TransientLifetimeManager used?Fernald
If you're using RegisterType<I, T>(), then Unity will not call Dispose() on the objects that Unity creates. You might be looking at examples using RegisterInstance<T>(obj). In that case, the TransientLifetimeManager treats your object like a Singleton and disposes of it for you. I will make the wording of the answer more precise. Thanks.Nucleon
A
0

ContainerControlledTransientManager was added in Unity on Jan 11, 2018

Add container 'owned' transient lifetime manager ContainerControlledTransientManager #37

So, ContainerControlledTransientManager is required. This lifetime manager is the same as TransientLifetimeManager except if the object implements IDisposable it will keep strong reference to object and dispose it when container is disposed. If created object is not disposable, container does not maintain any object references so when that object is released GC will collect it immediately.

Archerfish answered 7/12, 2021 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.