How to get current container context?

Jun 30, 2010 at 9:20 AM
Edited Jun 30, 2010 at 9:24 AM

Hi,
i am using a ServicePool for deferring injection to avoid circular injections:

    public class ServicePool
    {
     private UnityContainer _container; // this is the problem
        public IUserService UserService
        {
            get
            {
                return _container.Resolve<UserService>();
            }
        }
  }

This Pool is injected in the services. Since injection in the pool is only done when needed, no circular injection happens here:
    public class UserService : IUserService
    {
        [Dependency]
        public ServicePool ServicePool { get; set; }
    [...]
   }

But now i have the problem that the pool does not know the current container context. How can the pool know which container was used to inject the pool itself?
This is needed especially for testing issues to do a usefull inection. Example:

  IUserService AnotherInstance;
  Container.RegisterInstance(AnotherInstance);

This will notwork. AnotherInstance will get injected with the container. This will trigger the injection of the pool, but the pool will use an other container.

The container should do something like:
return <TheContainerUsedToResolveMe>.Resolve<IUserService>();

Any ideas?
Thanks in advance

Jun 30, 2010 at 11:01 PM

Add a dependency of type IUnityContainer (must be the interface, not the concrete UnityContainer) and resolve the pool out of the container. It'll get the current container injected into it.

 

Jul 2, 2010 at 8:23 AM

Thank you,
I was not able to test this yet, but it looks like the solution i was searching for!

Is this documented anywhere since it did not find this information in the unity documentation.