这是我的问题:InvalidOperationException: Unable to resolve service for type 'WebApi.Test.Repository.TestRepository' while attempting to activate 'WebApi.Test.Services.TestService'.
当然,问题是,当服务需要时,我似乎无法构造TestRepository
。
这是回购:
public class TestRepository : GenericRepository<TestEntity>
{
public TestRepository(TestContext dbContext
, ILoggerFactory loggerFactory) : base(dbContext, loggerFactory)
{
}
}
该服务的构造函数中基本上有一个 TestRepository
作为参数(但问题似乎在此之前)。
这是我的自定义 WebApplicationFactory
public class ApiWebApplicationFactory : WebApplicationFactory<Startup>
{
public IConfiguration Configuration { get; private set; }
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureAppConfiguration(config =>
{
Configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
config.AddConfiguration(Configuration);
});
//Add Services
builder.ConfigureTestServices(services =>
{
// Add framework services.
services.AddMvc();
//Add ApplicationDbContext using an in-memory database for testing.
services.AddDbContext<TestContext>(options =>
options.UseInMemoryDatabase("InMemoryDbForTesting"));
services.AddAutoMapper(typeof(Startup));
// Build the service provider.
var serviceProvider = services.BuildServiceProvider();
// Create a scope to obtain a reference to the database
// context (ApplicationDbContext).
using var scope = serviceProvider.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<TestContext>();
// Ensure the database is created.
db.Database.EnsureCreated();
// Seed the database with test data.
InitializeTestDatabase(db);
services.AddScoped<GenericRepository<TestEntity>, TestRepository>();
services.AddScoped<GenericService<TestEntity, TestRequest, TestResponse>, TestService>();
services.AddScoped<GenericController<TestEntity, TestRequest, TestResponse>, TestController>();
});
}
private void InitializeTestDatabase(TestContext textContext)
{
textContext.TestEntities.Add(new TestEntity { Id = 1 });
}
}
所有的泛型类都是抽象类。这是GenericRepository
:
public abstract class GenericRepository<T> : IGenericRepository<T>
where T : BaseEntity
{
protected DbContext _context;
private DbSet<T> _table;
private ILogger _logger;
public GenericRepository(DbContext context
, ILoggerFactory loggerFactory)
{
_context = context;
_table = context.Set<T>();
_logger = loggerFactory.CreateLogger<GenericRepository<T>>();
}
//CRUD Methods
}
我在想是不是因为我没有在我的自定义 WebAppFactory 上创建 ILoggerFactory,如果是这样,有人怎么能创建 ILoggerFactory?。
我尝试过的: 我尝试添加以下行:
services.AddScoped<DbContext, TestContext>();
但我一直收到同样的错误。
这是服务:
public abstract class GenericService<T, Request, Response>
where T : BaseEntity
where Response : class, IResponseDto
where Request : class, IRequestDto
{
protected readonly GenericRepository<T> _genericRepository;
protected readonly IMapper _mapper;
protected readonly ILogger _logger;
public GenericService(GenericRepository<T> genericRepository
, IMapper mapper
, ILoggerFactory loggerFactory)
{
_genericRepository = genericRepository;
_mapper = mapper;
_logger = loggerFactory.CreateLogger<GenericService<T, Request, Response>>();
}
//Methods
}