[英]Register a generic type inside an assembly that implements an interface with autofac
在項目Providers
我已經定義了這個 class
public class MyLogProvider<T> : IMyProvider<T>
where T : ILoggable
ILoggable
是一個簡單的接口,沒有要實現的方法。
然后,我有一個 web 項目,其中包含一些實現ILoggable
接口的 MVC 控制器。 例如
public class MyController : Controller, ILoggable
{
...
}
最后,我有一個項目IoC
,它被 web 項目引用,並且引用了項目Providers
。 所以在IoC
內部我不能使用MyController
class。
我需要進行這種注冊:
builder.RegisterType<MyLogProvider<MyController>>()
.As<IMyProvider<MyController>>()
.InstancePerRequest();
但顯然我不能那樣做......
我也試過
builder.RegisterGeneric(typeof(Log4NetProvider<>)).As(typeof(Log4NetProvider<>));
我一直在尋找使用
builder.RegisterAssemblyTypes(assemblies)
但沒有成功。
您單獨注冊 class 而不是接口。
builder.RegisterGeneric(typeof(MyProvider<>)).As(typeof(IMyProvider<>));
我假設接口是注入 controller 的接口
public class MyController : Controller, ILoggable {
public MyController (IMyProvider<MyController> provider) {
//...
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.