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

UseInstance with interface-based serviceType does not replace previous instance #237

Closed
detoxhby opened this issue Mar 12, 2020 · 5 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@detoxhby
Copy link

detoxhby commented Mar 12, 2020

After using IResolverContext.UseInstance the first time, the next UseInstance with same interface-based serviceType (and even implementation type) will not replace previous instance.
(even with or without IfAlreadyRegistered.Replace)

Example:

interface IUser
{
   string UniqueId { get; set; }
}

class User : IUser, IEquatable<User>
{
    public Guid Id { get; set; }
    string UniqueId { get; set; }
    // ...
   // IEquatable.Equals(User) and overriden object.Equals are provided
}

[Fact]
public void Test()
{
    // base ctor creates Scope = Container.OpenScope()
    var origUser = Scope.Resolve<IUser>(); // this is from normal registration
    var newUser = new User { Id = Guid.NewGuid() };
    
    Scope.UseInstance(newUser, IfAlreadyRegistered.Replace); // this is FINE, this instance is used
    Scope.UseInstance(origUser, IfAlreadyRegistered.Replace); // this is NOT happening
   // ... nor any subsequent calls UseInstance
}

not really for me Solution

ℹ️ Using direct class type User will make it work properly.

This is not an option for me due to abstraction, I need to use the interface type.


Any help is appreciated if I can work around this problem.
(tried to override GetHashCode() as Container.UseInstance is based on that, but it is used on ServiceType which is the interface and RuntimeHelpers.GetHashCode(typeof(IUser)) does not change during type, shouldn't be a problem)

@dadhi
Copy link
Owner

dadhi commented Mar 12, 2020

Thanks for posting. I will check.

@dadhi dadhi self-assigned this Mar 12, 2020
@dadhi dadhi closed this as completed in 54e897c Mar 12, 2020
@dadhi dadhi added the bug Something isn't working label Mar 12, 2020
@dadhi dadhi added this to the 4.1.2 milestone Mar 12, 2020
@dadhi
Copy link
Owner

dadhi commented Mar 12, 2020

@detoxhby
Please check my commit 54e897c#diff-74f9667b68d5a245baf32f72dac57ec3.

The UseInstance is obsolete (actually not in v4.1 yet but I won't be fixing the bugs for it)

Please use Use for replacing in scope or RegisterInstance for other scenarios.

I will release version 4.1.2 with the fix shortly.

@detoxhby
Copy link
Author

Nice! 👍

Btw I've tried Use() as well looking at the container code as it is (will be) marked as obsolete, but didn't work even for first time. I hope this cache invalidation will make it happen.

@dadhi
Copy link
Owner

dadhi commented Mar 12, 2020

v4.1.2 is out on NuGet

@detoxhby
Copy link
Author

✅ Checked new version: works fine!

Thanks for the fast response, keep up the good work! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants