某些服务无法在 ASP.NET 核心中构建
Some services are not able to be constructed in ASP.NET Core
由于这个异常,在使用 CQRS 时遇到了困难。我有一个 Movie 模型和 MovieDTO 模型
//EDIT 好的,我刚刚意识到在 GetMoviesQuery
中我不使用 IRepository< MovieDTO >
,当我将 MovieRepository
更改为 IRepository<MovieDTO>
时它起作用了
IRepository.cs
public interface IRepository<TEntity>
{
IEnumerable<TEntity> GetAll();
TEntity Get(int id);
TEntity Save(TEntity entity);
void Delete(int entityId);
}
MovieRepository.cs
public class MovieRepository : IRepository<MovieDTO>
{
private MyContext _context;
private IMapper _mapper;
public MovieRepository(MyContext context)
{
_context = context;
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<Movie, MovieDTO>();
});
_mapper = config.CreateMapper();
}
public IEnumerable<MovieDTO> GetAll()
{
return _context.Movies.ToList().Select(movie => _mapper.Map<Movie, MovieDTO>(movie));
}
}
GetMoviesQuery.cs
public class GetMoviesQuery
{
public class Query : IRequest<IEnumerable<MovieDTO>> { }
public class Handler : RequestHandler<Query, IEnumerable<MovieDTO>>
{
private MovieRepository _repository;
public Handler(MovieRepository repository)
{
_repository = repository ?? throw new ArgumentNullException(nameof(_repository));
}
protected override IEnumerable<MovieDTO> Handle(Query request)
{
return _repository.GetAll();
}
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddRazorPages();
services.AddTransient<IRepository<MovieDTO>, MovieRepository>();
services.AddHttpClient();
services.AddDbContext<MyContext>(options => options.UseSqlite("Data Source = blogging.db"));
services.AddMediatR(typeof(Startup));
}
异常:
System.AggregateException: '无法构建某些服务(验证服务描述符 'ServiceType: MediatR.IRequestHandler2[TicketReservationSystem.Server.CQRS.Queries.GetMoviesQuery+Query,System.Collections.Generic.IEnumerable
1[TicketReservationSystem.Server.Models.DTO.MovieDTO]] Lifetime: Transient ImplementationType: TicketReservationSystem.Server.CQRS.Queries.GetMoviesQuery+Handler' 时出错:尝试激活
时无法解析类型 'TicketReservationSystem.Server.Data.Repository.MovieRepository' 的服务
我不知道如何真正找出问题所在。
public Handler(MovieRepository repository)
应该改为
public Handler(IRepository<MovieDTO> repository)
,因为您使用接口而不是实现注册了容器。
services.AddTransient<IRepository<MovieDTO>, MovieRepository>();
如果您想使用您的原始代码,请注册 class 本身
services.AddTransient<MovieRepository>();
如果您有实现接口,请将其添加到 Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IDepartments, Departments>()
}
由于这个异常,在使用 CQRS 时遇到了困难。我有一个 Movie 模型和 MovieDTO 模型
//EDIT 好的,我刚刚意识到在 GetMoviesQuery
中我不使用 IRepository< MovieDTO >
,当我将 MovieRepository
更改为 IRepository<MovieDTO>
时它起作用了
IRepository.cs
public interface IRepository<TEntity>
{
IEnumerable<TEntity> GetAll();
TEntity Get(int id);
TEntity Save(TEntity entity);
void Delete(int entityId);
}
MovieRepository.cs
public class MovieRepository : IRepository<MovieDTO>
{
private MyContext _context;
private IMapper _mapper;
public MovieRepository(MyContext context)
{
_context = context;
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<Movie, MovieDTO>();
});
_mapper = config.CreateMapper();
}
public IEnumerable<MovieDTO> GetAll()
{
return _context.Movies.ToList().Select(movie => _mapper.Map<Movie, MovieDTO>(movie));
}
}
GetMoviesQuery.cs
public class GetMoviesQuery
{
public class Query : IRequest<IEnumerable<MovieDTO>> { }
public class Handler : RequestHandler<Query, IEnumerable<MovieDTO>>
{
private MovieRepository _repository;
public Handler(MovieRepository repository)
{
_repository = repository ?? throw new ArgumentNullException(nameof(_repository));
}
protected override IEnumerable<MovieDTO> Handle(Query request)
{
return _repository.GetAll();
}
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddRazorPages();
services.AddTransient<IRepository<MovieDTO>, MovieRepository>();
services.AddHttpClient();
services.AddDbContext<MyContext>(options => options.UseSqlite("Data Source = blogging.db"));
services.AddMediatR(typeof(Startup));
}
异常:
System.AggregateException: '无法构建某些服务(验证服务描述符 'ServiceType: MediatR.IRequestHandler2[TicketReservationSystem.Server.CQRS.Queries.GetMoviesQuery+Query,System.Collections.Generic.IEnumerable
1[TicketReservationSystem.Server.Models.DTO.MovieDTO]] Lifetime: Transient ImplementationType: TicketReservationSystem.Server.CQRS.Queries.GetMoviesQuery+Handler' 时出错:尝试激活
我不知道如何真正找出问题所在。
public Handler(MovieRepository repository)
应该改为
public Handler(IRepository<MovieDTO> repository)
,因为您使用接口而不是实现注册了容器。
services.AddTransient<IRepository<MovieDTO>, MovieRepository>();
如果您想使用您的原始代码,请注册 class 本身
services.AddTransient<MovieRepository>();
如果您有实现接口,请将其添加到 Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IDepartments, Departments>()
}