You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi everyone,
I'm finding a problem using a conditional registrations, when I use at the end one registration without condition, the previous registrations (with condition) don't work in one specific case, when registrations with condition have a lower scope than the default registration (without condition), and it seems that the default one have the precedence, ignoring previous conditional registrations.
[Fact]publicvoidTestWithDefaultScopeToSingleton(){varcontainer=new Container();// it works even for Reuse.SingletonvarscopeA= Reuse.Transient;varscopeB= Reuse.Singleton;
container.Register(typeof(IService),typeof(MyServiceA),
reuse: scopeA
, setup: Setup.With(condition:request => request.DirectParent.ServiceType ==typeof(ServiceConsumerA)));// the intention is having a default resolution if previous conditions aren't satisfied !!
container.Register(typeof(IService),typeof(MyServiceB),
reuse: scopeB
);
container.Register(typeof(ServiceConsumerA));
container.Register(typeof(ServiceConsumerB));varconsumerA= container.Resolve<ServiceConsumerA>();varconsumerB= container.Resolve<ServiceConsumerB>();
Assert.NotNull(consumerA);
Assert.NotNull(consumerB);// here is the mismatch from consumerA.Service type !!.
Assert.Equal(typeof(MyServiceA), consumerA.Service.GetType());
Assert.Equal(typeof(MyServiceB), consumerB.Service.GetType());}
If you try it only changing the scope of scopeA in Singleton it works.
[Fact]publicvoidTestWithIdenticalScopes(){varcontainer=new Container();// it works even for Reuse.SingletonvarscopeA= Reuse.Transient;varscopeB= Reuse.Transient;
container.Register(typeof(IService),typeof(MyServiceA),
reuse: scopeA
, setup: Setup.With(condition:request => request.DirectParent.ServiceType ==typeof(ServiceConsumerA)));// the intention is having a default resolution if previous conditions aren't satisfied !!
container.Register(typeof(IService),typeof(MyServiceB),
reuse: scopeB
);
container.Register(typeof(ServiceConsumerA));
container.Register(typeof(ServiceConsumerB));varconsumerA= container.Resolve<ServiceConsumerA>();varconsumerB= container.Resolve<ServiceConsumerB>();
Assert.NotNull(consumerA);
Assert.NotNull(consumerB);// those asserts work, so types match !
Assert.Equal(typeof(MyServiceA), consumerA.Service.GetType());
Assert.Equal(typeof(MyServiceB), consumerB.Service.GetType());}
Probably something is wrong in the example, mainly in the registration of default registration (without condition).
The complete unit test is attached in this post.
Any help will be appreciated :).
Thanks
The text was updated successfully, but these errors were encountered:
Discussed in #630
Originally posted by TheHunter February 2, 2024
ContainerTest.txt
Hi everyone,
I'm finding a problem using a conditional registrations, when I use at the end one registration without condition, the previous registrations (with condition) don't work in one specific case, when registrations with condition have a lower scope than the default registration (without condition), and it seems that the default one have the precedence, ignoring previous conditional registrations.
Take a look to this example:
If you try it only changing the scope of scopeA in Singleton it works.
Probably something is wrong in the example, mainly in the registration of default registration (without condition).
The complete unit test is attached in this post.
Any help will be appreciated :).
Thanks
The text was updated successfully, but these errors were encountered: