modesttree / Zenject

Dependency Injection Framework for Unity3D
MIT License
2.52k stars 273 forks source link

Cannot Unbind/Rebind AsSingle bindings #19

Open Bit-Rot opened 5 years ago

Bit-Rot commented 5 years ago

After creating a binding as a singleton, that value cannot then be unbound/rebound via Container.Unbind() or Container.Rebind().

The following two code snippets throw the error: ZenjectException: Assert hit! Attempted to use AsSingle multiple times for type 'string'. As of Zenject 6+, AsSingle as can no longer be used for the same type across different bindings. See the upgrade guide for details.

Example 1:

Container.Bind<string>().To<string>().FromInstance("test1").AsSingle();
Container.Unbind<string>();
Container.Bind<string>().To<string>().FromInstance("test2").AsSingle();

Example 2:

Container.Bind<string>().To<string>().FromInstance("test1").AsSingle();
Container.Rebind<string>().To<string>().FromInstance("test2").AsSingle();

My use case is that I'd like to have a separate singleton instance of a class for each container. I was hoping to run the following installer in each container, so that each container has some contextual information I can use to improve logging:

IContainerInfo containerInfo = new ContainerInfo(m_Settings.ContainerName);
Container.Rebind<IContainerInfo>().To<IContainerInfo>().FromInstance(containerInfo).AsSingle();
grofit commented 3 years ago

I am hitting this too but when using the non generic version where you pass in the type as an arg. I remember a while ago that there was some discussion around unbind and there may have been another method which ACTUALLY unbound something but for the life of me cannot remember what it was (or I am completely wrong).

Either way I am having things not being unbound when calling Unbind(type) on them.