Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot Unbind/Rebind AsSingle bindings #57

Open
doctorseus opened this issue Oct 19, 2022 · 2 comments
Open

Cannot Unbind/Rebind AsSingle bindings #57

doctorseus opened this issue Oct 19, 2022 · 2 comments

Comments

@doctorseus
Copy link

Describe the bug
This bug already exists since a while and also has been mentioned in modesttree#19.
When a binding is marked as AsSingle() it can not be unbound/rebound.

To Reproduce

Container.Bind<string>().ToSelf().FromInstance("Test").AsSingle();
Container.Rebind<string>().ToSelf().FromInstance("Test2").AsSingle();
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.

Expected behavior
No exception should be thrown.

Extenject and Unity info (please complete the following information):

  • Zenject version: master
  • Unity version: 2021.3.8f1

Additional context
From what I have seen in the code it makes sense that this doesn't work. When a provider is registered it will call this at some point:

But when unbinding the provider will just be removed from the list, but types which were registered as singleton at some point are never removed from the _boundSingletons list.

@jesus-mgtc
Copy link

jesus-mgtc commented Jan 11, 2023

EDITED:
If you're having the same issue, replace .AsSingle() by .AsCached() and it will work just fine, as .AsCached() also works with a single instance.

Hello! Any update on this? We're having the exact same issue when doing:

Container.Bind<string>().FromInstance("Test").AsSingle();
// ...
Container.Unbind<string>();
// ...
Container.Bind<string>().FromInstance("Test2").AsSingle();

Any update would be appreciated. Thanks in advance and keep up the good work!

@liderako
Copy link

liderako commented Aug 16, 2023

I have the same problem, rebind doesn't work if I use rebind after awake. But it's working in InstallBindings before awake.
I didn't find a solution to this problem, so my plan just to change inject logic for components with rebind by logic without zenject.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants