简体   繁体   English

Autofac通用接口不可分配给服务

[英]Autofac Generic interface is not assignable to service

I have like 20 registrations like these in an app: 我在应用程序中有20个像这样的注册:

builder.RegisterType(typeof(FilterParser<>)).As(typeof(IFilterParser<>)).InstancePerDependency();

When I try to build my container I get an error like so: 当我尝试构建我的容器时,我得到一个错误:

The type 'My.Namespace.FilterParser`1[TEntity]' is not assignable to service 'My.Namespace.IFilterParser`1'.' 类型'My.Namespace.FilterParser`1 [TEntity]'不能分配给服务'My.Namespace.IFilterParser`1'。

Note the difference is the [TEntity] in the implementation and lack thereof in the interface. 注意,差异是实现中的[TEntity]和接口中缺少的。

My services do implement the interfaces: 我的服务确实实现了接口:

public class FilterParser<TEntity> : IFilterParser<TEntity> where TEntity : new () 

public interface IFilterParser<TEntity> where TEntity : new () 

I also implemented this registration using .net core and it works just fine but in Autofac it just refuses to work. 我也使用.net核心实现了这个注册,它工作正常,但在Autofac中它只是拒绝工作。

I even tried AsImplementedInterfaces and I get the same error 我甚至尝试过AsImplementedInterfaces ,我得到了同样的错误

builder.RegisterType(typeof(FilterParser<>)).AsImplementedInterfaces()
                        .InstancePerDependency();

Use the RegisterGeneric() builder method: 使用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 当从容器请求匹配的服务类型时,Autofac会将其映射到实现类型的等效封闭版本

Reference Registration Concepts: Open Generic Components 参考注册概念:打开通用组件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM