无法解析类型 'MediatR.IMediator' 的服务
Unable to resolve service for type 'MediatR.IMediator'
我尝试使用 CQRS 构建 .NET Core API,但由于 MediatR 错误我无法构建它:
System.AggregateException: '无法构建某些服务(验证服务描述符时出错 'ServiceType: Core.Infrastructure.Domain.Queries.IQueryBus Lifetime: Scoped ImplementationType: Core.Infrastructure.Bus.QueryBus':尝试激活时无法解析类型 'MediatR.IMediator' 的服务 'Core.Infrastructure.Bus.QueryBus'.)'
我已经为我的 QueryBus 等添加了 'AddScope'。这是我的代码(适用于 AWS 的应用程序):
public class Startup
{
public const string AppS3BucketKey = "AppS3Bucket";
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public static IConfiguration Configuration { get; private set; }
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddAWSService<Amazon.S3.IAmazonS3>();
services.AddScoped<IQueryBus, QueryBus>();
services.AddScoped<IWarehouseRepository, WarehouseRepository>();
services.AddScoped<IRequestHandler<GetAllWarehouseDepartmentsQuery, IEnumerable<WarehouseDepartmentDto>>, WarehouseQueryHandler>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
查询总线:
using System.Threading.Tasks;
using Core.Infrastructure.Domain.Queries;
using MediatR;
namespace Core.Infrastructure.Bus
{
public class QueryBus : IQueryBus
{
private readonly IMediator _mediator;
public QueryBus(IMediator mediator)
{
_mediator = mediator;
}
public Task<TResponse> Send<TQuery, TResponse>(TQuery query) where TQuery : IQuery<TResponse>
{
return _mediator.Send(query);
}
}
}
IQueryBus:
using System.Threading.Tasks;
namespace Core.Infrastructure.Domain.Queries
{
public interface IQueryBus
{
Task<TResponse> Send<TQuery, TResponse>(TQuery query) where TQuery : IQuery<TResponse>;
}
}
感谢帮助
您没有在启动时注册 Mediatr 本身,因此 DI 容器无法解析它,正如错误提示的那样。
您可以从 NuGet 添加 MediatR DI 扩展,然后在启动时注册 MediatR:
To use, with an IServiceCollection instance:
services.AddMediatR(typeof(MyHandler));
or with an assembly:
services.AddMediatR(typeof(Startup).GetTypeInfo().Assembly);
https://github.com/jbogard/MediatR.Extensions.Microsoft.DependencyInjection
https://www.nuget.org/packages/MediatR.Extensions.Microsoft.DependencyInjection/
我尝试使用 CQRS 构建 .NET Core API,但由于 MediatR 错误我无法构建它:
System.AggregateException: '无法构建某些服务(验证服务描述符时出错 'ServiceType: Core.Infrastructure.Domain.Queries.IQueryBus Lifetime: Scoped ImplementationType: Core.Infrastructure.Bus.QueryBus':尝试激活时无法解析类型 'MediatR.IMediator' 的服务 'Core.Infrastructure.Bus.QueryBus'.)'
我已经为我的 QueryBus 等添加了 'AddScope'。这是我的代码(适用于 AWS 的应用程序):
public class Startup
{
public const string AppS3BucketKey = "AppS3Bucket";
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public static IConfiguration Configuration { get; private set; }
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddAWSService<Amazon.S3.IAmazonS3>();
services.AddScoped<IQueryBus, QueryBus>();
services.AddScoped<IWarehouseRepository, WarehouseRepository>();
services.AddScoped<IRequestHandler<GetAllWarehouseDepartmentsQuery, IEnumerable<WarehouseDepartmentDto>>, WarehouseQueryHandler>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
查询总线:
using System.Threading.Tasks;
using Core.Infrastructure.Domain.Queries;
using MediatR;
namespace Core.Infrastructure.Bus
{
public class QueryBus : IQueryBus
{
private readonly IMediator _mediator;
public QueryBus(IMediator mediator)
{
_mediator = mediator;
}
public Task<TResponse> Send<TQuery, TResponse>(TQuery query) where TQuery : IQuery<TResponse>
{
return _mediator.Send(query);
}
}
}
IQueryBus:
using System.Threading.Tasks;
namespace Core.Infrastructure.Domain.Queries
{
public interface IQueryBus
{
Task<TResponse> Send<TQuery, TResponse>(TQuery query) where TQuery : IQuery<TResponse>;
}
}
感谢帮助
您没有在启动时注册 Mediatr 本身,因此 DI 容器无法解析它,正如错误提示的那样。
您可以从 NuGet 添加 MediatR DI 扩展,然后在启动时注册 MediatR:
To use, with an IServiceCollection instance:
services.AddMediatR(typeof(MyHandler));
or with an assembly:
services.AddMediatR(typeof(Startup).GetTypeInfo().Assembly);
https://github.com/jbogard/MediatR.Extensions.Microsoft.DependencyInjection
https://www.nuget.org/packages/MediatR.Extensions.Microsoft.DependencyInjection/