Autofac.Core.DependencyResolutionException

Autofac is new here, wanting to better understand the following exception, any help / pointers on this would be much appreciated!

  Unhandled Exception: 
     Autofac.Core.DependencyResolutionException: 
         None of the constructors found with 'Public binding flags' on type 'Test.Authorization.LoginService' can be invoked with the available services and parameters: 
          Cannot resolve parameter 'System.Func`1 [System.IO.Stream] readStream' of constructor 'Void .ctor (System.Func`1 [System.IO.Stream], System.Func`1 [System.IO.Stream] ) '.
     at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance (IComponentContext context, IEnumerable`1 parameters) in \ autofac \ src \ Source \ Autofac \ Core \ Activators \ Reflection \ ReflectionActivator.cs: line 117    
     at Autofac.Core.Resolving.InstanceLookup.Activate (IEnumerable`1 parameters) in \ autofac \ src \ Source \ Autofac \ Core \ Resolving \ InstanceLookup.cs: line 79
     at Autofac.Core.Resolving.InstanceLookup.Execute () in \ autofac \ src \ Source \ Autofac \ Core \ Resolving \ InstanceLookup.cs: line 62
     at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance (ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters) in \ autofac \ src \ Source \ Autofac \ Core \ Resolving \ ResolveOperation.cs: line
     at Autofac.Core.Resolving.InstanceLookup.ResolveComponent (IComponentRegistration registration, IEnumerable`1 parameters) in \ autofac \ src \ Source \ Autofac \ Core \ Resolving \ InstanceLookup.cs: line 115  
     at Autofac.Features.Collections.CollectionRegistrationSource.c__DisplayClass4.c__DisplayClass6.b__1 (IComponentRegistration cr) in \ autofac \ src \ Source \ Autofac \ Features \ Collections \ CollectionRegistrationSource.cs: line 80 at System.Linq.Enector. ()  
     at System.Linq.Buffer`1..ctor (IEnumerable`1 source)    
     at System.Linq.Enumerable.ToArray [TSource] (IEnumerable`1 source)
     at Autofac.Features.Collections.CollectionRegistrationSource.c__DisplayClass4.b__0 (IComponentContext c, IEnumerable`1 p) in \ autofac \ src \ Source \ Autofac \ Features \ Collections \ CollectionRegistrationSource.cs: line 80    
     at Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance (IComponentContext context, IEnumerable`1 parameters) in \ autofac \ src \ Source \ Autofac \ Core \ Activators \ Delegate \ DelegateActivator.cs: line 65
     at Autofac.Core.Resolving.InstanceLookup.Activate (IEnumerable`1 parameters) in \ autofac \ src \ Source \ Autofac \ Core \ Resolving \ InstanceLookup.cs: line 79   
     at Autofac.Core.Resolving.InstanceLookup.Execute () in \ autofac \ src \ Source \ Autofac \ Core \ Resolving \ InstanceLookup.cs: line 62   
     at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance (ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters) in \ autofac \ src \ Source \ Autofac \ Core \ Resolving \ ResolveOperation.cs: line    
     at Autofac.Core.Resolving.InstanceLookup.ResolveComponent (IComponentRegistration registration, IEnumerable`1 parameters) in \ autofac \ src \ Source \ Autofac \ Core \ Resolving \ InstanceLookup.cs: line 115  
     at Autofac.Core.Activators.Reflection.AutowiringParameter.c__DisplayClass2.b__0 () in \ autofac \ src \ Source \ Autofac \ Core \ Activators \ Reflection \ AutowiringParameter.cs: line 51    
     at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate () in \ autofac \ src \ Source \ Autofac \ Core \ Activators \ Reflection \ ConstructorParameterBinding.cs: line 113    
     at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance (IComponentContext context, IEnumerable`1 parameters) in \ autofac \ src \ Source \ Autofac \ Core \ Activators \ Reflection \ ReflectionActivator.cs: line 119    
     at Autofac.Core.Resolving.InstanceLookup.Activate (IEnumerable`1 parameters) in \ autofac \ src \ Source \ Autofac \ Core \ Resolving \ InstanceLookup.cs: line 79   
     at Autofac.Core.Resolving.InstanceLookup.b__0 () in \ autofac \ src \ Source \ Autofac \ Core \ Resolving \ InstanceLookup.cs: line 64   
     at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare (Guid id, Func`1 creator) in \ autofac \ src \ Source \ Autofac \ Core \ Lifetime \ LifetimeScope.cs: line 269    
     at Autofac.Core.Resolving.InstanceLookup.Execute () in \ autofac \ src \ Source \ Autofac \ Core \ Resolving \ InstanceLookup.cs: line 64   
     at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance (ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters) in \ autofac \ src \ Source \ Autofac \ Core \ Resolving \ ResolveOperation.cs: line    
     at Autofac.Core.Resolving.ResolveOperation.ResolveComponent (IComponentRegistration registration, IEnumerable`1 parameters) in \ autofac \ src \ Source \ Autofac \ Core \ Resolving \ ResolveOperation.cs: line 69 
     at Autofac.Core.Resolving.ResolveOperation.Execute (IComponentRegistration registration, IEnumerable`1 parameters) in \ autofac \ src \ Source \ Autofac \ Core \ Resolving \ ResolveOperation.cs: line 96 
     at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent (IComponentRegistration registration, IEnumerable`1 parameters) in \ autofac \ src \ Source \ Autofac \ Core \ Lifetime \ LifetimeScope.cs: line 235    
     at Autofac.Core.Container.ResolveComponent (IComponentRegistration registration, IEnumerable`1 parameters) in \ autofac \ src \ Source \ Autofac \ Core \ Container.cs: line 191    
     at Autofac.ResolutionExtensions.TryResolveService (IComponentContext context, Service service, IEnumerable`1 parameters, Object & instance) in \ autofac \ src \ Source \ Autofac \ ResolutionExtensions.cs: line 728    
     at Autofac.ResolutionExtensions.ResolveService (IComponentContext context, Service service, IEnumerable`1 parameters) in \ autofac \ src \ Source \ Autofac \ ResolutionExtensions.cs: line 346    
     at Autofac.ResolutionExtensions.Resolve (IComponentContext context, Type serviceType, IEnumerable`1 parameters) in \ autofac \ src \ Source \ Autofac \ ResolutionExtensions.cs: line 244 at Autofac.ResolutionExtensions.Resolve [TService] (IComponentCumerable IE, parameters) in \ autofac \ src \ Source \ Autofac \ ResolutionExtensions.cs: line 197    
     at Autofac.ResolutionExtensions.Resolve [TService] (IComponentContext context, Parameter [] parameters) in \ autofac \ src \ Source \ Autofac \ ResolutionExtensions.cs: line 213
public sealed class AuthorizationModule : Module { protected override void Load(ContainerBuilder builder) { builder .Register(LoginService) .AsImplementedInterfaces() .SingleInstance(); } static LoginService LoginService(IComponentContext c) { var file = c.Resolve<IFile>(); var dataPath = <Some Path>; return new LoginService (readStream: () => file.Open(dataPath), writeStream: () => file.Create(dataPath)); } } sealed class LoginService : TLoginService.Iface, IService { readonly Func<Stream> _readStream; readonly Func<Stream> _writeStream; readonly XmlSerializer _serializer; public LoginService( Func<Stream> readStream, Func<Stream> writeStream ) { Contract.Requires(readStream != null); Contract.Requires(writeStream != null); _readStream = readStream; _writeStream = writeStream; _serializer = new XmlSerializer(typeof (List<LoginRecord>)); } ... ... 
+4
source share
1 answer

Your LoginService class has a constructor similar to LoginService(Func<System.IO.Stream> stream) . Autofac cannot create the argument Func<System.IO.Stream> . Therefore, register it accordingly so that Autofac can create a LoginService .

For instance:

 builder.Register(c => new LoginService(d => File.Open("Foo)); 

Go to the Component Registration section of the Wiki to find which method suits you.

+5
source

Source: https://habr.com/ru/post/1413632/


All Articles