Autofac 通用多接口
Autofac Generic Multiple Interface
我正在尝试解析一个通用接口,如下所示,但在尝试 运行 应用程序时出现异常。
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>>>();
所以当我尝试 运行 这段代码时,它给我以下错误。
The type 'Service.ActionService.DomainEventHandler.ApplicationUserCreatedEventHandler'
is not assignable to service 'Domain.Core.DomainEvent.IHandler`1'.
我不知道如何解决这个错误。
您尝试将 ApplicationUserCreatedEventHandler
注册为 IHandler<>
的开放类型,但此类型不是 IHandler<>
,而是 IHandler<ApplicationUserCreatedEvent>
,因此您必须将其注册为它。
builder.RegisterType<ApplicationUserCreatedEventHandler>()
.As(typeof(IHandler<ApplicationUserCreatedEvent>));
你可以这样解决:
container.Resolve<IEnumerable<IHandler<ApplicationUserCreatedEvent>>>();
顺便说一句,如果你想注册一个开放类型,你可以使用这样的东西:
builder.RegisterGeneric(typeof(ApplicationUserCreatedEventHandler<TUserCreatedEvent>))
.As(typeof(IHandler<>));
和 ApplicationUserCreatedEventHandler<T>
像这样:
public class ApplicationUserCreatedEventHandler<TUserCreatedEvent>
: IHandler<TUserCreatedEvent>
where TUserCreatedEvent : ApplicationUserCreatedEvent
{
public void Handle(TUserCreatedEvent args)
{
if (args == null) throw new ArgumentNullException("args");
// Code
}
}
您仍然可以通过这种方式解决问题:
container.Resolve<IEnumerable<IHandler<ApplicationUserCreatedEvent>>>();
我正在尝试解析一个通用接口,如下所示,但在尝试 运行 应用程序时出现异常。
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>>>();
所以当我尝试 运行 这段代码时,它给我以下错误。
The type 'Service.ActionService.DomainEventHandler.ApplicationUserCreatedEventHandler' is not assignable to service 'Domain.Core.DomainEvent.IHandler`1'.
我不知道如何解决这个错误。
您尝试将 ApplicationUserCreatedEventHandler
注册为 IHandler<>
的开放类型,但此类型不是 IHandler<>
,而是 IHandler<ApplicationUserCreatedEvent>
,因此您必须将其注册为它。
builder.RegisterType<ApplicationUserCreatedEventHandler>()
.As(typeof(IHandler<ApplicationUserCreatedEvent>));
你可以这样解决:
container.Resolve<IEnumerable<IHandler<ApplicationUserCreatedEvent>>>();
顺便说一句,如果你想注册一个开放类型,你可以使用这样的东西:
builder.RegisterGeneric(typeof(ApplicationUserCreatedEventHandler<TUserCreatedEvent>))
.As(typeof(IHandler<>));
和 ApplicationUserCreatedEventHandler<T>
像这样:
public class ApplicationUserCreatedEventHandler<TUserCreatedEvent>
: IHandler<TUserCreatedEvent>
where TUserCreatedEvent : ApplicationUserCreatedEvent
{
public void Handle(TUserCreatedEvent args)
{
if (args == null) throw new ArgumentNullException("args");
// Code
}
}
您仍然可以通过这种方式解决问题:
container.Resolve<IEnumerable<IHandler<ApplicationUserCreatedEvent>>>();