Service Fabric Core3.1中MediatR依赖注入注册异常问题
问题描述
在基于.NET Core 3.1的Service Fabric应用中集成MediatR时,QueryHandler不依赖仓储可正常注册;但添加IRepository<myDto>依赖后,无法构造请求处理器,报错无法解析该仓储服务。
DI配置代码
var assembly1 = Assembly.GetExecutingAssembly(); var assembly2 = typeof(GetSomeThingQuery).Assembly; var assembly3 = typeof(IRepository<myDto>).Assembly; var assembly4 = typeof(myDto).Assembly; var provider = new ServiceCollection() .AddLogging() .AddMediatR(assembly1, assembly2, assembly3, assembly4) .AddSingleton<IApiExceptionService, ApiExceptionService>() .BuildServiceProvider(); ServiceRuntime.RegisterServiceAsync("ApiServiceType", context => new ApiService(context, provider.GetService<IApiExceptionService>(), provider.GetService<IMediator>())).GetAwaiter().GetResult();
处理器代码
namespace Query { public class GetSomeThingQuery: IRequest<List<myDto>> { public string para1{ get; set; } public string para2 { get; set; } } public class GetSomeThingQueryHandler : IRequestHandler<GetSomeThingQuery, List<myDto>> { private readonly IRepository<myDto> _repository; public GetSomeThingQueryHandler (IRepository<myDto> repository) { _repository = repository; } public async Task<List<myDto>> Handle(GetSomeThingQueryHandler Query request, CancellationToken cancellationToken) { var test = await _repository.CallTheDatabaseAndGetTheResult(request.para1); return null; //this is just here as a place holder. } } }
错误信息
System.InvalidOperationException: Error constructing handler for request of type MediatR.IRequestHandler`2[Query.GetSomeThingQuery,System.Collections.Generic.List`1[Sql.myDto]]. Register your handlers with the container. See the samples in GitHub for examples. ---> System.InvalidOperationException: Unable to resolve service for type 'IRepository`1[myDto]' while attempting to activate 'Query.GetSomeThingQuery'. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)
解决方案
1. 修复处理器Handle方法的参数类型错误
处理器的Handle方法参数类型写错了,应该是GetSomeThingQuery而非GetSomeThingQueryHandler,修正后代码如下:
public async Task<List<myDto>> Handle(GetSomeThingQuery request, CancellationToken cancellationToken) { var test = await _repository.CallTheDatabaseAndGetTheResult(request.para1); return null; // 占位用,实际返回对应结果 }
这个笔误会导致MediatR无法正确匹配请求与处理器,甚至触发错误的类型解析逻辑。
2. 在DI容器中注册仓储的实现类
当前DI配置中仅注册了MediatR和IApiExceptionService,但未注册IRepository<myDto>的具体实现。需要添加仓储的注册代码,根据实际业务需求选择合适的生命周期(单例、作用域或瞬态):
var provider = new ServiceCollection() .AddLogging() .AddMediatR(assembly1, assembly2, assembly3, assembly4) .AddSingleton<IApiExceptionService, ApiExceptionService>() // 添加仓储注册,替换为你的实际实现类 .AddSingleton<IRepository<myDto>, MyRepositoryImplementation<myDto>>() .BuildServiceProvider();
内容的提问来源于stack exchange,提问作者krock28
相关产品推荐
相关产品推荐

