gpt4 book ai didi

dependency-injection - Autofac.Core.DependencyResolutionException

转载 作者:行者123 更新时间:2023-12-01 16:17:36 25 4
gpt4 key购买 nike

我是 Autofac 的新手,我想更好地理解以下异常(exception)情况。任何有关此问题的帮助/指示将不胜感激!

获取以下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>));
}
...
...

最佳答案

您的LoginService类有一个类似于 LoginService(Func<System.IO.Stream> stream) 的构造函数。 Autofac 无法创建 Func<System.IO.Stream>争论。因此,适本地注册它,以便 Autofac 可以创建 LoginService .

例如:

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

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

关于dependency-injection - Autofac.Core.DependencyResolutionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10693711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com