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
using System.Collections.Generic;using System.Linq;using Autofac;using Autofac.Core;namespaceAutofacResolveWeirdness{classProgram{staticvoidMain(string[]args){varbuilder=new ContainerBuilder();
builder.RegisterModule<PreparingModule>();varcontainer= builder.Build();using(varnestedContainer= container.BeginLifetimeScope(containerBuilder =>{// note the instance per lifetime containerBuilder.RegisterType<Foo>().InstancePerLifetimeScope();})){try{
nestedContainer.Resolve<Foo>();}catch(DependencyResolutionExceptione){// it fails and enters here.}varnestedFoos= nestedContainer.Resolve<IEnumerable<Foo>>();// the nested container has successfully resolved a list of Foo!!// It is now in the container because it is instance per lifetime.varfoo= nestedContainer.Resolve<Foo>();// success!}using(varnestedContainer= container.BeginLifetimeScope(containerBuilder =>{// instance per dependency containerBuilder.RegisterType<Foo>();})){try{
nestedContainer.Resolve<Foo>();}catch(DependencyResolutionExceptione){// it fails and enters here.}// the following works, as shown in the previous container, it scan the global scope.varnestedFoos= nestedContainer.Resolve<IEnumerable<Foo>>();try{// as it is instance per dependency, the following resolution will continue to fail.
nestedContainer.Resolve<Foo>();}catch(DependencyResolutionExceptione){// it continues to fails and enters here.}}}}publicclassPreparingModule:Module{protectedoverridevoidAttachToComponentRegistration(IComponentRegistrycomponentRegistry,IComponentRegistrationregistration){
registration.Preparing +=OnRegistrationPreparing;}privatevoidOnRegistrationPreparing(objectsender,PreparingEventArgse){varbarParameter=new ResolvedParameter((info,context)=> info.ParameterType ==typeof(Bar),(info,context)=>new Bar());
e.Parameters = e.Parameters.Union(new[]{barParameter});}}publicclassFoo{publicFoo(Barbar){}}publicclassBar{}}
I'm not sure whether this is a bug or not, however it did catch me out and was wondering why some objects were able to be injected with Bar while other couldn't until I narrowed down the problem to the above situation.
Should the nested container be able to resolve Foo object to begin with??
The nested container don't appear to make use of PreparingModule in certain circumstances. ie Resolve() vs Resolve<IEnumerable>()
I am expecting a more consistent behaviour as in, they either both fails or both succeeds.
As a workaround, I am currently explicitly registering PreparingModule again in the nested scope and all is consistent and resolves in every case as one would expect.
The text was updated successfully, but these errors were encountered:
This appears to be a duplicate of #218 - module AttachToComponentRegistration handlers don't know anything about new child lifetime scopes. That would cause the observed issue. No resolution at this time; we're open to pull requests.
I'm not sure whether this is a bug or not, however it did catch me out and was wondering why some objects were able to be injected with Bar while other couldn't until I narrowed down the problem to the above situation.
Should the nested container be able to resolve Foo object to begin with??
The nested container don't appear to make use of PreparingModule in certain circumstances. ie Resolve() vs Resolve<IEnumerable>()
I am expecting a more consistent behaviour as in, they either both fails or both succeeds.
As a workaround, I am currently explicitly registering PreparingModule again in the nested scope and all is consistent and resolves in every case as one would expect.
The text was updated successfully, but these errors were encountered: