C# 试图激活控制器类时未解析接口(IOwnerRepository)

C# 试图激活控制器类时未解析接口(IOwnerRepository),c#,asp.net-mvc-4,.net-core,C#,Asp.net Mvc 4,.net Core,我有一个如下的接口:这个接口是我的GetAllOwners方法签名 namespace Pms.Core { public interface IOwnerRepository { Task<IEnumerable<Owner>> GetAllOwners(); } } 名称空间Pms.Core { 公共接口IOwnerRepository { 任务GetAllOwners(); } } 驱动此接口的存储库类如下所示。此

我有一个如下的接口:这个接口是我的GetAllOwners方法签名

namespace Pms.Core
{
   public interface IOwnerRepository
   {
       Task<IEnumerable<Owner>> GetAllOwners();        
   }
}
名称空间Pms.Core
{
公共接口IOwnerRepository
{
任务GetAllOwners();
}
}
驱动此接口的存储库类如下所示。此类应返回数据库中的所有所有者

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();
        }
    }
}
名称空间Pms.Persistence
{
公共类OwnerRepository:IOwnerRepository
{
私有只读PmsDbContext上下文;
public OwnerRepository(PmsDbContext上下文)
{
this.context=上下文;
}
公共异步任务GetAllOwners()
{
返回wait context.Owners.toListSync();
}
}
}
我有一个控制器类,它使用Repository类来显示所有所有者

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);
        }
    }
}
名称空间Pms.Controllers
{
[路线(“/api/所有者”)]
公共类所有者控制器:控制器
{
私有只读PmsDbContext上下文;
专用只读IMapper映射器;
私有只读IOOwnerRepository存储库;
public OwnersController(PmsDbContext上下文、IMapper映射器、,
IOOwnerRepository(存储库)
{
this.repository=存储库;
this.context=上下文;
this.mapper=mapper;
}
[HttpGet]
公共异步任务GetOwners()
{
var owners=await repository.GetAllOwners();
返回mapper.Map(所有者);
}
}
}
最后,这里是我注册服务的代码片段

services.AddScoped<IOwnerRepository, OwnerRepository>();
services.addScope();
当我测试此API时,我得到“处理请求时发生未经处理的异常。InvalidOperationException:在尝试激活'Pms.Controllers.OwnersController'时,无法解析'Pms.Core.IOwnerRepository'类型的服务。”


我所做的是确保API在没有接口的情况下工作。这个应用程序在不使用接口的情况下运行得很好,但不知何故,我无法实现接口以使我的应用程序更加健壮和面向对象。

我找到了罪犯。依赖项注入的版本不兼容。我用的是2.0,所以我降级到1.2.0,一切正常

<PackageReference
      Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="1.2.0" />


您在哪里注册服务?Startup.cs是我注册服务的类。下面是我使用的代码:services.addScope();请将代码添加到问题本身,而不是作为注释。Hmmm。嗯,我觉得没有什么特别突出的。你的问题看起来都是正确的。好吧,我找到了解决方案,我也在下面发布了。有趣。