8

I'm new to Autofac and I want to have a better understanding with the following exception. Any help/pointers on this would be highly appreciated!

Getting following Autofac.Core.DependencyResolutionException.

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 124
    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.Enumerable.WhereSelectArrayIterator`2.MoveNext()  
    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 124    
    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 124    
    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](IComponentContext context, IEnumerable`1 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

Here's my cs code:

    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>));
     }
     ...
     ...

1 Answer 1

10

Your LoginService class has a constructor similar to LoginService(Func<System.IO.Stream> stream). Autofac is unable to create the Func<System.IO.Stream> argument. So register it appropriately so that Autofac can create the LoginService.

Eg:

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! The above mentioned code is indeed what is being used and is causing the DependencyResolutionException. Any further pointers would really be appreciated!
@theself Edited my answer. Go through the wiki section of Autofac.
Thanks! Edited original question and added code snippet associated with this issue. The code snippet is along the lines of what was suggested in an earlier comment. This is existing code that I am new to and am told that this was working properly earlier and just recently started generating the DependencyResolutionException. This might be something straightforward that we are not able to pinpoint, so any help on this would be highly appreciated! Thanks much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.