Autofac.Core.DependencyResolutionException

时间:2012-05-21 22:57:24

标签: dependency-injection autofac

Autofac新手在这里想要更好地理解以下异常,任何帮助/指针都将受到高度赞赏!

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
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 个答案:

答案 0 :(得分:6)

您的LoginService类的构造函数与LoginService(Func<System.IO.Stream> stream)类似。 Autofac无法创建Func<System.IO.Stream>参数。因此,请适当注册,以便Autofac可以创建LoginService

例如:

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

浏览Wiki的Component Registration部分,找出适合您的方法。