gpt4 book ai didi

c# - Autofac 的 None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'

转载 作者:行者123 更新时间:2023-11-30 17:44:15 26 4
gpt4 key购买 nike

尽管我以非常相似的方式使用它一年多了,但我还是回到了 Autofac 问题上,我真的需要一些建议。

我选择通过属性选择加入。这次的不同之处在于,其中一个注册使用 IIndex 根据 LoginState 枚举为手头的任务选择最合适的接口(interface)。该值通过属性提供。

您可以认为它是一个非常基本的基于 Autofac 的状态机。

由于选择加入的性质,我有以下扩展方法,它通过属性的存在和属性的值获取类型:

public static class ContainerBuilderEnumerateAttributedTypes
{
#region EnumerateAttributedTypes(builder, action)

[DebuggerStepThrough]
public static void EnumerateAttributedTypes<TAttribute>(this ContainerBuilder builder,
Action<Type, TAttribute> action) where TAttribute : Attribute
{
var typesAndAttributes = Assembly
.GetExecutingAssembly()
.GetTypes()
.Where(type => type.GetCustomAttributes<TAttribute>(false).Any())
.Select(type => new { Type = type, Attribute = type.GetCustomAttributes<TAttribute>(false).First() });

foreach (var typeAndAtttribute in typesAndAttributes)
{
action(typeAndAtttribute.Type, typeAndAtttribute.Attribute);
}
}

#endregion

#region EnumerateAttributedTypes(builder, inherit, action)

[DebuggerStepThrough]
public static void EnumerateAttributedTypes<TAttribute>(this ContainerBuilder builder,
bool inherit, Action<Type, TAttribute> action) where TAttribute : Attribute
{
var typesAndAttributes = Assembly
.GetExecutingAssembly()
.GetTypes()
.Where(type => type.GetCustomAttributes<TAttribute>(inherit).Any())
.Select(type => new { Type = type, Attribute = type.GetCustomAttributes<TAttribute>(inherit).First() });

foreach (var typeAndAtttribute in typesAndAttributes)
{
action(typeAndAtttribute.Type, typeAndAtttribute.Attribute);
}
}

#endregion
}

在 Global.asax.cs 期间,我调用了 builder.RegisterModule,它在术语中调用了 builder.RegisterModule。

在那里我有以下内容:

public class LogicAutofacModule : Module
{
#region Load

protected override void Load(ContainerBuilder builder)
{
builder.EnumerateAttributedTypes<DispatcherAttribute>((type, attribute) =>
{
var @interface = type.GetInterfaces().First();

// ReSharper disable once ConvertToLambdaExpression
builder
.RegisterType(type)
.As(@interface);
});

builder.EnumerateAttributedTypes<LogicAttribute>((type, attribute) =>
{
var @interface = type.GetInterfaces().First();

// ReSharper disable once ConvertToLambdaExpression
builder
.RegisterType(type)
.Keyed(attribute.State, @interface)
.As(@interface);
});
}

#endregion

IDispatcher 有两个实例:

IIndex<LoginState, ILogic<AuthenticateContext, AuthenticateResult>>

IIndex<LoginState, ILogic<AuthenticateIncurringChargeContext, AuthenticateIncurringChargeResult>> _handlers; 

LoginState 如果通过属性指定,适用时通过

[LogicDispatcher(LogicState.InvalidCredentials)]

和类似的。

无论我如何做到这一点,即使回到最基本的注册方式,我也会得到“找不到构造函数”

Autofac.Core.Activators.Reflection.DefaultConstructorFinder error message.

以前从未遇到过...请告知或询问更多信息。

事后思考...这里是 ILogic 实现的示例:

[Logic(LoginState.InvalidAccount)]
public class AuthenticateInvalidAccount : ILogic<AuthenticateContext, AuthenticateResult>
{
#region Execute

public AuthenticateResult Execute(AuthenticateContext context, LoginResponse response)
{
return new AuthenticateResult
{
State = State.InvalidAccount
};
}

#endregion
}

还有一个调度器的实例:

[Dispatcher]
public class AuthenticateDispatcher : IDispatcher<AuthenticateContext, AuthenticateResult>
{
#region Fields

private readonly IIndex<LoginState, ILogic<AuthenticateContext, AuthenticateResult>> _handlers;

#endregion

#region Constructors

public AuthenticateDispatcher(IIndex<LoginState, ILogic<AuthenticateContext, AuthenticateResult>> handlers)
{
_handlers = handlers;
}

#endregion

#region Dispatch

public AuthenticateResult Dispatch(AuthenticateContext context)
{
var service = new AccountServiceClient();

var response = service.Invoke(client => client.Login(context.Username, context.Password));

var logic = _handlers[response.LoginState];

var result = logic.Execute(context, response);

return result;
}

#endregion
}

服务 super 简单:

[ErrorHandled]
public class Service : IService
{
#region Fields

private readonly IDispatcher<AuthenticateContext, AuthenticateResult> _authenticateDispatcher;

private readonly IDispatcher<AuthenticateIncurringChargeContext, AuthenticateIncurringChargeResult> _authenticateIncurringChargeDispatcher;

#endregion

#region Constructor

public Service(
IDispatcher<AuthenticateContext, AuthenticateResult> authenticateDispatcher,
IDispatcher<AuthenticateIncurringChargeContext, AuthenticateIncurringChargeResult> authenticateIncurringChargeDispatcher)
{
_authenticateDispatcher = authenticateDispatcher;
_authenticateIncurringChargeDispatcher = authenticateIncurringChargeDispatcher;
}

#endregion

#region Authenticate

public AuthenticateResponse Authenticate(AuthenticateRequest request)
{
var context = request.Map<AuthenticateContext>();

var result = _authenticateDispatcher.Dispatch(context);

var response = result.Map<AuthenticateResponse>();

return response;
throw new NotImplementedException();
}

#endregion

#region AuthenticateIncurringCharge

public AuthenticateIncurringChargeResponse AuthenticateIncurringCharge(AuthenticateIncurringChargeRequest request)
{
//var context = request.Map<AuthenticateIncurringChargeContext>();

//var result = _authenticateIncurringChargeDispatcher.Dispatch(context);

//var response = result.Map<AuthenticateIncurringChargeResponse>();

//return response;
throw new NotImplementedException();
}

#endregion
}

最佳答案

比较尴尬。注释掉注册逻辑服务的代码。惭愧:(

希望有人发现扩展方法有用!

关于c# - Autofac 的 None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29893462/

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