简体   繁体   English

注册开放泛型类型 - 类型参数不匹配

[英]Registration of open generic types - type parameter mismatch

I'm trying to register all implementations of my generic repository as follows:我正在尝试按如下方式注册我的通用存储库的所有实现:

container.Register(typeof(IRepository<>), new[] { typeof(Repository<>).Assembly });

However, the container can't verify my configuration:但是,容器无法验证我的配置:

Additional information: The configuration is invalid.附加信息:配置无效。 Creating the instance for type IErrorLogService failed.为类型 IErrorLogService 创建实例失败。 The constructor of type ErrorLogService contains the parameter with name 'errorLogRepository' and type IRepository<ErrorLog> that is not registered. ErrorLogService 类型的构造函数包含名称为“errorLogRepository”且类型为 IRepository<ErrorLog> 的未注册参数。 Please ensure IRepository<ErrorLog> is registered, or change the constructor of ErrorLogService.请确保 IRepository<ErrorLog> 已注册,或更改 ErrorLogService 的构造函数。 Note that there exists a registration for a different type Persistence.Interfaces.Repository.Generic.IRepository<T> while the requested type is Persistence.Interfaces.Repository.Generic.IRepository<Persistence.DataModel.ErrorLog>.请注意,存在不同类型 Persistence.Interfaces.Repository.Generic.IRepository<T> 的注册,而请求的类型为 Persistence.Interfaces.Repository.Generic.IRepository<Persistence.DataModel.ErrorLog>。

Based on various SO threads , the snippet above should be the way to go.基于各种 SO 线程,上面的代码片段应该是要走的路。 What did I miss?我错过了什么?

My repository class:我的存储库类:

public class Repository<T> : IRepository<T> where T : Entity { }

IRepository and Repository exists in the same assembly. IRepositoryRepository存在于同一个程序集中。

An explicit registration of each type works:每种类型的显式注册都有效:

container.Register<IRepository<ErrorLog>, Repository<ErrorLog>>();

The Register(Type openGenericServiceType, IEnumerable<Assembly> assemblies) overload you are using states in its documentation:您在其文档中使用状态的Register(Type openGenericServiceType, IEnumerable<Assembly> assemblies)重载:

Registers all concrete, non-generic , public and internal types in the given set of assemblies that implement the given openGenericServiceType with container's default lifestyle (which is transient by default).在给定的assemblies集中注册所有具体的、非泛型的、公共的和内部类型,这些assemblies使用容器的默认生活方式(默认情况下是瞬态的)实现给定的openGenericServiceType

Note the word "non-generic" here.请注意此处的“非通用”一词。 This register overload is meant to batch-register all non-generic implementations of an open-generic service type.此注册重载旨在批量注册开放通用服务类型的所有非通用实现。 Since you have one open-generic implementation, this Register method will not find it.由于您有一个开放的通用实现,因此该Register方法将找不到它。

Instead, you should use the Register(Type serviceType, Type implementationType) overload, that states:相反,您应该使用Register(Type serviceType, Type implementationType)重载,它指出:

Registers that a new instance of will be returned every time a serviceType is requested.注册每次请求serviceType时将返回的新实例。 If serviceType and implementationType represent the same type, the type is registered by itself.如果serviceTypeimplementationType表示相同的类型,则该类型自行注册。 Open and closed generic types are supported.支持开放和封闭的泛型类型。

TLDR; TLDR;

Change your registration to the following:将您的注册更改为以下内容:

container.Register(typeof(IRepository<>), typeof(Repository<>));

You can find more information about this in Simple Injector's documentation .您可以在Simple Injector 的文档中找到更多相关信息。

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

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