I'm trying to implement MediatR in a Service Fabric application running on core 3.1. I'm able to wire up the Queryhandler as long as I don't include a repository (which really defeats the purpose).
Thank you for taking a look!
Here is how I am setting up the DI. in Main() I have:
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();
My handler looks like:
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.
}
}
}
The error I get is:
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)
I have tried various ways of setting put the container but nothing has worked. If I remove the following lines, MediatR can resolve the handler. When I include the repo it can't resolve the handler.
private readonly IRepository<myDto> _repository;
public GetSomeThingQueryHandler (IRepository<myDto> repository)
{
_repository = repository;
}
CodePudding user response:
You need to add all the classes that you're going to use in the DI container. You haven't registered the repository.
You need to add:
.AddScoped(typeof(IRepository<>), typeof(Repository<>));
to your ServiceCollection
.
I recommend Scrutor to help with the registration if you don't want to register your classes one by one.