尝试激活控制器时未解析接口 (IOwnerRepository) class
Interface (IOwnerRepository) is not resolved while attempting to activate controller class
我有一个接口如下:这个接口是我的 GetAllOwners 方法签名。
namespace Pms.Core
{
public interface IOwnerRepository
{
Task<IEnumerable<Owner>> GetAllOwners();
}
}
驱动此接口的我的存储库class如下所示。这 class 应该 return 数据库中的所有所有者。
namespace Pms.Persistence
{
public class OwnerRepository : IOwnerRepository
{
private readonly PmsDbContext context;
public OwnerRepository(PmsDbContext context)
{
this.context = context;
}
public async Task<IEnumerable<Owner>> GetAllOwners()
{
return await context.Owners.ToListAsync();
}
}
}
我有一个控制器 class,它使用 Repository class 来显示所有所有者。
namespace Pms.Controllers
{
[Route("/api/owners")]
public class OwnersController : Controller
{
private readonly PmsDbContext context;
private readonly IMapper mapper;
private readonly IOwnerRepository repository;
public OwnersController(PmsDbContext context, IMapper mapper,
IOwnerRepository repository)
{
this.repository = repository;
this.context = context;
this.mapper = mapper;
}
[HttpGet]
public async Task<IEnumerable<OwnerResource>> GetOwners()
{
var owners = await repository.GetAllOwners();
return mapper.Map<IEnumerable<Owner>,
IEnumerable<OwnerResource>>(owners);
}
}
}
最后,这是我注册服务的代码片段。
services.AddScoped<IOwnerRepository, OwnerRepository>();
当我测试此 API 时,我得到“处理请求时发生未处理的异常。InvalidOperationException:尝试激活时无法解析类型 'Pms.Core.IOwnerRepository' 的服务 'Pms.Controllers.OwnersController'."
我所做的是确保 API 在没有界面的情况下也能正常工作。这个应用程序在不使用接口的情况下工作得很好,但不知何故我无法实现接口来使我的应用程序更健壮和面向对象。
我找到了罪魁祸首。依赖注入的版本不兼容。我使用的是 2.0,所以我降级到 1.2.0,一切正常。
<PackageReference
Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="1.2.0" />
我有一个接口如下:这个接口是我的 GetAllOwners 方法签名。
namespace Pms.Core
{
public interface IOwnerRepository
{
Task<IEnumerable<Owner>> GetAllOwners();
}
}
驱动此接口的我的存储库class如下所示。这 class 应该 return 数据库中的所有所有者。
namespace Pms.Persistence
{
public class OwnerRepository : IOwnerRepository
{
private readonly PmsDbContext context;
public OwnerRepository(PmsDbContext context)
{
this.context = context;
}
public async Task<IEnumerable<Owner>> GetAllOwners()
{
return await context.Owners.ToListAsync();
}
}
}
我有一个控制器 class,它使用 Repository class 来显示所有所有者。
namespace Pms.Controllers
{
[Route("/api/owners")]
public class OwnersController : Controller
{
private readonly PmsDbContext context;
private readonly IMapper mapper;
private readonly IOwnerRepository repository;
public OwnersController(PmsDbContext context, IMapper mapper,
IOwnerRepository repository)
{
this.repository = repository;
this.context = context;
this.mapper = mapper;
}
[HttpGet]
public async Task<IEnumerable<OwnerResource>> GetOwners()
{
var owners = await repository.GetAllOwners();
return mapper.Map<IEnumerable<Owner>,
IEnumerable<OwnerResource>>(owners);
}
}
}
最后,这是我注册服务的代码片段。
services.AddScoped<IOwnerRepository, OwnerRepository>();
当我测试此 API 时,我得到“处理请求时发生未处理的异常。InvalidOperationException:尝试激活时无法解析类型 'Pms.Core.IOwnerRepository' 的服务 'Pms.Controllers.OwnersController'."
我所做的是确保 API 在没有界面的情况下也能正常工作。这个应用程序在不使用接口的情况下工作得很好,但不知何故我无法实现接口来使我的应用程序更健壮和面向对象。
我找到了罪魁祸首。依赖注入的版本不兼容。我使用的是 2.0,所以我降级到 1.2.0,一切正常。
<PackageReference
Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="1.2.0" />