C# 如何解决依赖注入无法解析 IEntityRepository 的错误?
结论
异常表明,容器创建 ExtendedService<ExtendedCreateInput, ExtendedComponent, ExtendedUpdateInput, Guid> 时,无法解析其构造函数所需的 IEntityRepository<ComponentBase, Guid>。这并不表示 IExtendedService 的映射本身无法构造。
Unable to resolve service for type
'IEntityRepository<ComponentBase, System.Guid>'
while attempting to activate 'ExtendedService<..., ExtendedComponent, ..., System.Guid>'
ExtendedService 请求的仓储接口需要与已注册的接口完全匹配,并且该接口需要有对应的实现注册。
优先方案:使用具体实体类型的仓储
ExtendedService 已经有泛型参数 TEntity。如果仓储是按具体实体类型注册的,例如 IEntityRepository<ExtendedComponent, Guid>,可将构造函数参数改为 IEntityRepository<TEntity, TEntityId>:
public class ExtendedService<TCreateInput, TEntity, TUpdateBaseInput, TEntityId> :
IExtendedService<TCreateInput, TUpdateBaseInput, TEntity, TEntityId>
where TCreateInput : CreateBaseInput
where TEntity : ComponentBase
where TUpdateBaseInput : UpdateBaseInput
{
private readonly IEntityRepository<TEntity, TEntityId> _repository;
public ExtendedService(IEntityRepository<TEntity, TEntityId> repository)
{
_repository = repository;
}
// 其余方法保持原有实现
}
然后为对应的闭合泛型类型注册仓储实现。具体实现类型取决于项目中的仓储类,注册形式例如:
builder.Services.AddScoped<
IEntityRepository<ExtendedComponent, Guid>,
ExtendedComponentRepository>();
ExtendedComponentRepository 是示例名称,应替换为实际实现该接口的类。服务注册仍可保持:
builder.Services.AddScoped<
IExtendedService<ExtendedCreateInput, ExtendedUpdateInput, ExtendedComponent, Guid>,
ExtendedService<ExtendedCreateInput, ExtendedComponent, ExtendedUpdateInput, Guid>>();
仓储实现需要确实实现 IEntityRepository<ExtendedComponent, Guid>,其泛型约束也要与项目中的类型定义相容。
另一种方案:注册基类仓储
如果服务设计上需要 IEntityRepository<ComponentBase, Guid>,就需要提供并注册这个精确类型的实现:
builder.Services.AddScoped<
IEntityRepository<ComponentBase, Guid>,
ComponentBaseRepository>();
ComponentBaseRepository 同样只是占位示例,必须替换为实际可用的实现。仅注册 IEntityRepository<ExtendedComponent, Guid> 通常无法满足 IEntityRepository<ComponentBase, Guid>:它们是不同的闭合泛型接口类型,C# 泛型接口默认不会因实体继承关系而自动互换。
检查仓储注册
检查 Program.cs 中是否注册了与服务构造函数参数完全一致的接口,并核对以下内容:
- 仓储接口的实体类型是
ComponentBase还是ExtendedComponent。 - 主键类型是否为
Guid。 - 注册的实现类是否实现了对应的闭合泛型接口。
IEntityRepository<,>是否有开放泛型注册;如果有,确认实现类及其泛型约束支持当前使用的类型。
现有的服务映射注册了 IExtendedService<...>,但仅凭这条注册无法判断仓储是否已注册。应根据实际仓储实现选择匹配的方式。只改服务生命周期无法补上缺失的依赖。
备注:内容仅供参考。
