Autofac 通用接口不可分配给服务
Autofac Generic interface is not assignable to service
我在一个应用程序中有大约 20 个这样的注册:
builder.RegisterType(typeof(FilterParser<>)).As(typeof(IFilterParser<>)).InstancePerDependency();
当我尝试构建我的容器时,出现如下错误:
The type 'My.Namespace.FilterParser`1[TEntity]' is not assignable to service '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();
When a matching service type is requested from the container, Autofac will map this to an equivalent closed version of the implementation type
我在一个应用程序中有大约 20 个这样的注册:
builder.RegisterType(typeof(FilterParser<>)).As(typeof(IFilterParser<>)).InstancePerDependency();
当我尝试构建我的容器时,出现如下错误:
The type 'My.Namespace.FilterParser`1[TEntity]' is not assignable to service '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();
When a matching service type is requested from the container, Autofac will map this to an equivalent closed version of the implementation type