【发布时间】:2015-07-07 18:05:07
【问题描述】:
我正在尝试解决一个类似下面的通用接口,但在尝试运行应用程序时遇到异常。
public interface IHandler<in T> where T : IDomainEvent
{
void Handle(T args);
}
public class ApplicationUserCreatedEventHandler : IHandler<ApplicationUserCreatedEvent>
{
public void Handle(ApplicationUserCreatedEvent args)
{
if (args == null) throw new ArgumentNullException("args");
// Code
}
}
我正在 global.asax 中注册,如下所示
var builder = new ContainerBuilder();
builder.RegisterType<ApplicationUserCreatedEventHandler>().As(typeof (IHandler<>));
return builder.Build();
}
这就是我使用 IComponentContext 解决依赖关系的方式。
var handlers = _componentContext.Resolve<IEnumerable<IHandler<TEvent>>>();
所以当我尝试运行此代码时,它给了我以下错误。
类型“Service.ActionService.DomainEventHandler.ApplicationUserCreatedEventHandler” 不可分配给服务“Domain.Core.DomainEvent.IHandler`1”。
我不确定如何修复此错误。
【问题讨论】:
-
您不应该在没有 IEnumerable 的情况下解析
_componentContext.Resolve<IHandler<TEvent>>();吗?
标签: c# dependency-injection domain-driven-design inversion-of-control autofac