简体   繁体   中英

Unity 2 IDisposable Issue in Console Application C#

I am using Unity 2.1.505.0 in a console application, which consists of an IDisposable implementation, CarRepository . However, the Dispose() is never called when out of its scope. Below is the code sample:

    internal class Program
    {
        private static void Main(string[] args)
        {           
            CarShop.Entry();
        }
    }

    class CarShop
    {
       static UnityContainer unityContainer = new UnityContainer();

        public static void Entry()
        {           
            unityContainer.RegisterType<ICarRepository, CarRepository>();
            var carShop = new CarShop();
            carShop.BuyCar();
        }

      public void BuyCar()
      {
          CheckCar();
      }

      private void CheckCar()
      {
          var carService = unityContainer.Resolve<CarService>();
          var car = carService.GetCar(1);  
         // **Dispose() method is not called out of this scope**
      }
    }

   class CarService
    {
        private ICarRepository carRepository;
        public CarService(ICarRepository carRepository)
        {
            this.carRepository = carRepository;
        }

        public Car GetCar(int id)
        {
            return this.carRepository.GetCar(id);
        }
    }

    interface ICarRepository
    {
        Car GetCar(int id);
    }

    class CarRepository : ICarRepository, IDisposable
    {
        public Car GetCar(int id)
        {
            return null;
        }

        public void Dispose()  
        {

        }
    }

    public class Car
    {
    }

Any idea??

Update

As @nemesv suggests the solution below:

unityContainer.RegisterType<ICarRepository, CarRepository>(
        new HierarchicalLifetimeManager())

 using (var childContainer = unityContainer.CreateChildContainer())
    {
        var carService = childContainer.Resolve<CarService>();
        var car = carService.GetCar(1);            
    }

It works. But unlike Unity.MVC , it requires explicit creation of childContainer . Is there transparent way of dispose of IDispose object, like Unity.MVC. Or other tools in the future.

The RegisterType method by default uses the TransientLifetimeManager which does not store a reference to the created objects so it does not call the Dispose for you.

Among the built LifetimeManager implementations only the ContainerControlledLifetimeManager disposes the managed objects when the container itself is disposed.

You can read about the Lifetime Managers here: Understanding Lifetime Managers

If your goal is to dispose the CarRepository after an usage then you can achieve it with using the HierarchicalLifetimeManager and child containers :

public static void Entry()
{           
    unityContainer.RegisterType<ICarRepository, CarRepository>(
        new HierarchicalLifetimeManager());
    var carShop = new CarShop();
    carShop.BuyCar();
}

private void CheckCar()
{
    using (var childContainer = unityContainer.CreateChildContainer())
    {
        var carService = childContainer.Resolve<CarService>();
        var car = carService.GetCar(1);            
    }
    // **Dispose() method is not called out of this scope**
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM