dadhi / DryIoc

DryIoc is fast, small, full-featured IoC Container for .NET
MIT License
1.03k stars 122 forks source link

How to allow Recursive Dependency #505

Closed umar-ulabs closed 2 years ago

umar-ulabs commented 2 years ago

The following error is thrown in xamarin forms app :DryIoc.ContainerException: code: Error.RecursiveDependencyDetected; How and where can I override the rules in the app to allow the above feature?

dadhi commented 2 years ago

Not enough context. Give me at least full exception stack trace or better the failing test or example.

umar-ulabs commented 2 years ago

Following is the exception thrown in the mainservice {Prism.Ioc.ContainerResolutionException: An unexpected error occurred while resolving 'IDataService' ---> DryIoc.ContainerException: code: Error.RecursiveDependencyDetected; message: Recursive dependency is detected when resolving ICService as parameter "cService" (IsSingletonOrDependencyOfSingleton) <--recursive in Singleton VService: IVService as parameter "vService" FactoryId=192 (IsSingletonOrDependencyOfSingleton) in Singleton SService: ISService as parameter "sService" FactoryId=189 (IsSingletonOrDependencyOfSingleton) in Singleton CService: ICService as parameter "cService" FactoryId=193 (IsSingletonOrDependencyOfSingleton) <--recursive in resolution root Singleton CService:ICService FactoryId=188 (IsSingletonOrDependencyOfSingleton, IsResolutionCall) from container with scope {Name=null} with Rules with {TrackingDisposableTransients, UseDynamicRegistrationsAsFallbackOnly, FuncAndLazyWithoutRegistration, SelectLastRegisteredFactory} and without {ThrowOnRegisteringDisposableTransient, UseFastExpressionCompilerIfPlatformSupported} with FactorySelector=SelectLastRegisteredFactory with Made={FactoryMethod=ConstructorWithResolvableArguments}. at DryIoc.Throw.It (System.Int32 error, System.Object arg0, System.Object arg1, System.Object arg2, System.Object arg3) [0x00000] in /_/src/DryIoc/Container.cs:13642 at DryIoc.Request.WithResolvedFactory (DryIoc.Factory factory, System.Boolean skipRecursiveDependencyCheck, System.Boolean skipCaptiveDependencyCheck, System.Boolean copyRequest) [0x001d0] in /_/src/DryIoc/Container.cs:9291 at DryIoc.Factory.GetExpressionOrDefault (DryIoc.Request request) [0x00000] in /_/src/DryIoc/Container.cs:10124 at DryIoc.ReflectionFactory.CreateExpressionOrDefault (DryIoc.Request request) [0x0033c] in /_/src/DryIoc/Container.cs:10917 at DryIoc.Factory.GetExpressionOrDefault (DryIoc.Request request) [0x002c6] in /_/src/DryIoc/Container.cs:10228 at DryIoc.ReflectionFactory.CreateExpressionOrDefault (DryIoc.Request request) [0x0033c] in /_/src/DryIoc/Container.cs:10917 at DryIoc.Factory.GetExpressionOrDefault (DryIoc.Request request) [0x002c6] in /_/src/DryIoc/Container.cs:10228 at DryIoc.ReflectionFactory.CreateExpressionOrDefault (DryIoc.Request request) [0x0033c] in /_/src/DryIoc/Container.cs:10917 at DryIoc.Factory.GetExpressionOrDefault (DryIoc.Request request) [0x002c6] in /_/src/DryIoc/Container.cs:10228 at DryIoc.ReflectionFactory.CreateExpressionOrDefault (DryIoc.Request request) [0x0033c] in /_/src/DryIoc/Container.cs:10917 at DryIoc.Factory.GetExpressionOrDefault (DryIoc.Request request) [0x002c6] in /_/src/DryIoc/Container.cs:10228 at DryIoc.Container.ResolveAndCache (System.Int32 serviceTypeHash, System.Type serviceType, DryIoc.IfUnresolved ifUnresolved) [0x0008f] in /_/src/DryIoc/Container.cs:391 at DryIoc.Container.DryIoc.IResolver.Resolve (System.Type serviceType, DryIoc.IfUnresolved ifUnresolved) [0x0011c] in /_/src/DryIoc/Container.cs:356 at DryIoc.Container.DryIoc.IResolver.Resolve (System.Type serviceType, System.Object serviceKey, DryIoc.IfUnresolved ifUnresolved, System.Type requiredServiceType, DryIoc.Request preResolveParent, System.Object[] args) [0x00039] in /_/src/DryIoc/Container.cs:428 at DryIoc.Resolver.Resolve (DryIoc.IResolver resolver, System.Type serviceType, System.Object[] args, DryIoc.IfUnresolved ifUnresolved, System.Type requiredServiceType, System.Object serviceKey) [0x00000] in /_/src/DryIoc/Container.cs:8087 at Prism.DryIoc.DryIocContainerExtension.Resolve (System.Type type, System.ValueTuple2[System.Type,System.Object][] parameters) [0x0001c] in //src/Containers/Prism.DryIoc.Shared/DryIocContainerExtension.cs:294 --- End of inner exception stack trace --- at Prism.DryIoc.DryIocContainerExtension.Resolve (System.Type type, System.ValueTuple`2[System.Type,System.Object][] parameters) [0x00053] in //src/Containers/Prism.DryIoc.Shared/DryIocContainerExtension.cs:298 at Prism.DryIoc.DryIocContainerExtension.Resolve (System.Type type) [0x00000] in //src/Containers/Prism.DryIoc.Shared/DryIocContainerExtension.cs:272 at Prism.Ioc.IContainerProviderExtensions.Resolve[T] (Prism.Ioc.IContainerProvider provider) [0x00000] in //src/Prism.Core/Ioc/IContainerProviderExtensions.cs:18 at MainService.OnRequest in MainService.cs:41 }`

dadhi commented 2 years ago

@umar-ulabs If you look at the exception message below without line wrap, you see what services in object graph are repeated (marked as <-- recursive).

So the recursive dependencies are:

The strange thing here is why you have a ICService cService parameter in CService : ICService ? Are you using a different implementations of ICService or what?

I can't say more without looking at source code.

Prism.Ioc.ContainerResolutionException: An unexpected error occurred while resolving 'IDataService' ---> DryIoc.ContainerException: code: Error.RecursiveDependencyDetected;
message: Recursive dependency is detected when resolving 
ICService as parameter "cService" (IsSingletonOrDependencyOfSingleton) <--recursive
  in Singleton VService: IVService as parameter "vService" FactoryId=192 (IsSingletonOrDependencyOfSingleton)
  in Singleton SService: ISService as parameter "sService" FactoryId=189 (IsSingletonOrDependencyOfSingleton)
  in Singleton CService: ICService as parameter "cService" FactoryId=193 (IsSingletonOrDependencyOfSingleton) <--recursive
  in resolution root Singleton CService:ICService FactoryId=188 (IsSingletonOrDependencyOfSingleton, IsResolutionCall)
  from container with scope {Name=null}
 with Rules with {TrackingDisposableTransients, UseDynamicRegistrationsAsFallbackOnly, FuncAndLazyWithoutRegistration, SelectLastRegisteredFactory} and without {ThrowOnRegisteringDisposableTransient, UseFastExpressionCompilerIfPlatformSupported}
 with FactorySelector=SelectLastRegisteredFactory
 with Made={FactoryMethod=ConstructorWithResolvableArguments}.
dadhi commented 2 years ago

Closing then