gpt4 book ai didi

c# - Autofac.Core.Registration.ComponentNotRegisteredException

转载 作者:行者123 更新时间:2023-12-04 08:47:56 27 4
gpt4 key购买 nike

我试图在我的应用程序中实现 CQRS 模式。所以我在这里找到了如何从程序集中注册所有命令处理程序:Autofac resolve dependency in CQRS CommandDispatcher

但这对我来说效果不佳。这是代码:

        containerBuilder.RegisterAssemblyTypes(assembly)
.AsClosedTypesOf(typeof(ICommandHandler<>));

containerBuilder.RegisterAssemblyTypes(assembly)
.AsClosedTypesOf(typeof(IQueryHandler<,>));

搬运工工厂
 public class CqrsHandlerFactory : ICqrsHandlerFactory
{
private readonly IContainer container;

public CqrsHandlerFactory(IContainer container)
{
this.container = container;
}

public ICommandHandler<TCommand> GetCommandHandler<TCommand>(TCommand command) where TCommand : class, ICommand
{
return container.Resolve<ICommandHandler<TCommand>>();
}

public IQueryHandler<TQuery, object> GetQueryHandler<TQuery>(TQuery query) where TQuery : class, IQuery
{
return container.Resolve<IQueryHandler<TQuery, object>>();
}
}

公共(public)汽车
 public class CqrsBus : ICqrsBus
{
private readonly ICqrsHandlerFactory cqrsHandlerFactory;

public CqrsBus(ICqrsHandlerFactory cqrsHandlerFactory)
{
this.cqrsHandlerFactory = cqrsHandlerFactory;
}

public void ExecuteCommand(ICommand command)
{
var handler = cqrsHandlerFactory.GetCommandHandler(command);
if (handler == null)
throw new NotImplementedHandlerException(string.Format("Cannot find handler for {0}", command.GetType()));
handler.Handle(command);
}

public TResult RunQuery<TResult>(IQuery query)
{
var handler = cqrsHandlerFactory.GetQueryHandler(query);
if (handler == null)
throw new NotImplementedHandlerException(string.Format("Cannot find handler for {0}", query.GetType()));
return (TResult)handler.Handle(query);
}
}

异常(exception)

An exception of type 'Autofac.Core.Registration.ComponentNotRegisteredException' occurred in Autofac.dll but was not handled in user code Additional information: The requested service 'PromocjeWsieciowkach.Messaging.Core.ICommandHandler`1[[PromocjeWsieciowkach.Messaging.Core.ICommand, PromocjeWsieciowkach.Messaging.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.



堆栈跟踪

at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable1 parameters)
at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context, IEnumerable
1 parameters) at PromocjeWsieciowkach.Messaging.Factories.CqrsHandlerFactory.GetCommandHandler[TCommand](TCommand command) in C:\Users\Daniel\Desktop\PromocjeWsieciowkach\src\PromocjeWsieciowkach.Messaging\Factories\CqrsHandlersFactory.cs:line 17 at PromocjeWsieciowkach.Messaging.Bus.CqrsBus.ExecuteCommand(ICommand command) in C:\Users\Daniel\Desktop\PromocjeWsieciowkach\src\PromocjeWsieciowkach.Messaging\Bus\CqrsBus.cs:line 17 at PromocjeWsieciowkach.Controllers.PostController.Index() in C:\Users\Daniel\Desktop\PromocjeWsieciowkach\src\PromocjeWsieciowkach\Controllers\PostController.cs:line 20 at lambda_method(Closure , Object , Object[] ) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__28.MoveNext()



那么我做错了什么?

最佳答案

您的代码和异常消息清楚地显示了问题。总之,您的异常消息解释说:

The requested service 'ICommandHandler<ICommand>' has not been registered.



换句话说,您请求的是 ICommandHandler<ICommand>而不是 ICommandHandler<TestCommand> .这可以在这里看到:
public void ExecuteCommand(ICommand command)
{
var handler = cqrsHandlerFactory.GetCommandHandler(command);
// ...
}

C# 编译器将类型推断应用于 GetCommandHandler<T>称呼。所以下面的代码是实际的调用:
var handler = cqrsHandlerFactory.GetCommandHandler<ICommand>(command);

您应该更改 ICrqsBus.ExecuteCommand方法如下:
public void ExecuteCommand<TCommand>(TCommand command)
{
// You can merge the factory and your CqrsBus. Splitting them is useless.
var handler = cqrsHandlerFactory.GetCommandHandler<TCommand>();
// You don't need then null check; Autofac never returns null.
handler.Handle(command);
}

如果您无法制作 ExecuteCommand方法泛型(例如,因为您在编译时不知道命令类型),您应该使用反射 API 构建泛型类型,如下所示:
public class CqrsBus : ICqrsBus
{
private readonly IComponentContext context;

public CqrsBus(IComponentContext context)
{
this.context = context;
}

public void ExecuteCommand(ICommand command)
{
Type handlerType = typeof(ICommandHandler<>).MakeGenericType(command.GetType());
dynamic handler = this.context.Resolve(handlerType);
void handler.Execute((dynamic)command);
}
}

关于c# - Autofac.Core.Registration.ComponentNotRegisteredException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40358433/

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