AUTOFAC-解决开放的通用类型以解决派生类型
我正在尝试使用AUTOFAC注册打开的通用类型来解决
public interface IBackGroundJobHandler<T> where T: INotification
{
public Task Handle(T notification, CancellationToken cancellationToken);
}
public abstract class EventHandler<T> : IBackGroundJobHandler<T> where T : INotification
{
public abstract Task Handle(T notification, CancellationToken cancellationToken);
}
public class TestEventHandler : EventHandler<TestEvent>
{
public async override Task Handle(TestEvent notification, CancellationToken
cancellationToken)
{
await Task.Delay(20000);
System.Diagnostics.Debug.WriteLine("Test Event Finished");
}
}
public class SomeService<T> where T:INotfication
{
public SomeService(IBackGroundJobHandler<T> handler)
{
//sometask
}
}
我尝试注册的派生类型:
builder.RegisterGeneric(typeof(EventHandler<>)).As(typeof(IBackGroundJobHandler<>))
.InstancePerLifetimeScope();
ibackgroundJobhandler在服务构建器中无法解决。
我还尝试了:
builder.RegisterAssemblyTypes(typeof(EventHandler).GetTypeInfo().Assembly)
.AsClosedTypesOf(typeof(IBackGroundJobHandler<>)).InstancePerLifetimeScope();
我是AutoFac的新手,该如何解决?
I am trying to use autofac to register open generic type to resolve to derived type
public interface IBackGroundJobHandler<T> where T: INotification
{
public Task Handle(T notification, CancellationToken cancellationToken);
}
public abstract class EventHandler<T> : IBackGroundJobHandler<T> where T : INotification
{
public abstract Task Handle(T notification, CancellationToken cancellationToken);
}
public class TestEventHandler : EventHandler<TestEvent>
{
public async override Task Handle(TestEvent notification, CancellationToken
cancellationToken)
{
await Task.Delay(20000);
System.Diagnostics.Debug.WriteLine("Test Event Finished");
}
}
public class SomeService<T> where T:INotfication
{
public SomeService(IBackGroundJobHandler<T> handler)
{
//sometask
}
}
I tried registering it by :
builder.RegisterGeneric(typeof(EventHandler<>)).As(typeof(IBackGroundJobHandler<>))
.InstancePerLifetimeScope();
IBackgroundJobHandler is not resolving in service constructor.
I also tried:
builder.RegisterAssemblyTypes(typeof(EventHandler).GetTypeInfo().Assembly)
.AsClosedTypesOf(typeof(IBackGroundJobHandler<>)).InstancePerLifetimeScope();
I am new to autofac, how can I resolve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我避免了抽象类,并简单地使用了通用接口ibackgroundJobhandler&lt;&gt;
在AUTOFAC容器中注册为:
I avoided abstract class and simply used generic interface IBackGroundJobHandler<>
Registered in Autofac container as:
EventHandler&lt; t&gt;是一个抽象类,因此AUTOFAC解析器无论如何都无法创建。
您尚未注册testeventhandler(例如),因此AutoFac不了解它。
当您要求ibackgroundJobhandler&lt; testevent&gt;我假设您想解决一个testeventhandler,但是尚未注册该课程。
因此,鉴于您需要注册testeventhandler,您可能不需要eventhandler&lt; t&gt;无论如何,抽象类。 (我猜您认为这将是注册从EventHandler&lt; t&gt; in Go中派生的所有类的一种方式)。 TestEventHandler可以实现IbackgroundJobhandler&lt; testevent&gt;。
然后使用:
如果您有很多事件处理程序,则可能会很痛苦,因此我会使用反射来找到确实实现界面的类:
EventHandler<T> is an abstract class, so the Autofac resolver will not be able to create one anyway.
You've not registered TestEventHandler (for example), so Autofac is unaware of it.
When you request a IBackGroundJobHandler<TestEvent>, I presume that you want to get a TestEventHandler resolved, but that class has not been registered.
So, given that you need to register TestEventHandler, you probably don't need that EventHandler<T> abstract class anyway. (I'm guessing that you thought that would be a way of registering all classes that derived from EventHandler<T> in on go). The TestEventHandler can just implement IBackGroundJobHandler<TestEvent>.
Then use:
If you have many event handlers, it can be a pain, so I'd use reflection to find the classes that do implement the interface: