![](/img/trans.png)
[英]The type '***' is not assignable to service '***' in Autofac
[英]Autofac Generic interface is not assignable to service
我在應用程序中有20個像這樣的注冊:
builder.RegisterType(typeof(FilterParser<>)).As(typeof(IFilterParser<>)).InstancePerDependency();
當我嘗試構建我的容器時,我得到一個錯誤:
類型'My.Namespace.FilterParser`1 [TEntity]'不能分配給服務'My.Namespace.IFilterParser`1'。
注意,差異是實現中的[TEntity]
和接口中缺少的。
我的服務確實實現了接口:
public class FilterParser<TEntity> : IFilterParser<TEntity> where TEntity : new ()
public interface IFilterParser<TEntity> where TEntity : new ()
我也使用.net核心實現了這個注冊,它工作正常,但在Autofac中它只是拒絕工作。
我甚至嘗試過AsImplementedInterfaces
,我得到了同樣的錯誤
builder.RegisterType(typeof(FilterParser<>)).AsImplementedInterfaces()
.InstancePerDependency();
使用RegisterGeneric()
構建器方法:
builder.RegisterGeneric(typeof(FilterParser<>))
.As(typeof(IFilterParser<>))
.InstancePerDependency();
當從容器請求匹配的服務類型時,Autofac會將其映射到實現類型的等效封閉版本
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.