Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# InvalidOperationException:无法解析泛型存储库类型的服务_C# - Fatal编程技术网

C# InvalidOperationException:无法解析泛型存储库类型的服务

C# InvalidOperationException:无法解析泛型存储库类型的服务,c#,C#,通用存储库抛出以下错误。 nvalidOperationException:在尝试激活“Controllers.HomeController”时,无法解析类型“Core.Repository.MongoDb.MongoRepository”“1[Controllers.HomeController+TestUser]”的服务 下面是在尝试进行依赖项注入时抛出错误的通用存储库 public interface IEntity { string

通用存储库抛出以下错误。 nvalidOperationException:在尝试激活“Controllers.HomeController”时,无法解析类型“Core.Repository.MongoDb.MongoRepository”“1[Controllers.HomeController+TestUser]”的服务

下面是在尝试进行依赖项注入时抛出错误的通用存储库

      public interface IEntity
        {

            string Id { get; set; }

            DateTime CreatedDate { get; set; }

        }

   public interface IRepository<T> where T: IEntity
    {
        /// <summary>
        /// Inserts the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        Task<T> Insert(T entity, InsertOneOptions options, CancellationToken cancellationToken = default(CancellationToken));
}

 public class MongoRepository<T> : IRepository<T> where T : IEntity
 {
          ....
  }


public class Entity: IEntity
{
    public string Id { get; set; }

    public DateTime CreatedDate { get; set; }
    ....
}


public class HomeController : Controller
{
    private IRepository<TestUser> _repository;

    public class TestUser : Entity
    {
        public string Name { get; set; }
    }
    public HomeController(MongoRepository<TestUser> repository)
    {
              this._repository = repository;

    }

    public IActionResult Index()
    {
        var result=_repository.GetAll();

        return View(result);
    }
}
公共接口的可扩展性
{
字符串Id{get;set;}
DateTime CreatedDate{get;set;}
}
公共接口i假设,其中T:i
{
/// 
///插入指定的实体。
/// 
///实体。
///取消令牌。
/// 
任务插入(T实体,InsertOnOptions,CancellationToken CancellationToken=default(CancellationToken));
}
公共类MongoRepository:IRepository其中T:Entity
{
....
}
公共类实体:通用性
{
公共字符串Id{get;set;}
公共日期时间CreatedDate{get;set;}
....
}
公共类HomeController:控制器
{
私人IRepository_存储库;
公共类TestUser:实体
{
公共字符串名称{get;set;}
}
公共HomeController(MongoRepository存储库)
{
这个。_repository=repository;
}
公共IActionResult索引()
{
var result=_repository.GetAll();
返回视图(结果);
}
}
Startup.cs类注册服务如下

 services.AddScoped(typeof(IRepository<>), typeof(MongoRepository<>));
services.addScope(typeof(IRepository)、typeof(MongoRepository));
基本上,在抛出错误之前,它甚至没有回到主控制器。当我尝试在没有DI的情况下实例化存储库时,我不确定它为什么会在DI中失败。

您正在注入

MongoRepository<TestUser> repository
MongoRepository存储库
或者你必须使用接口,这是DI的原理

在控制器的构造函数中替换为以下内容

IRepository<TestUser> repository
i存储库

它会起作用。

您的IRepository需要entity,而您的实际实体TestUser(在构造函数中使用)只从entity继承,而不实现entity接口。这是一个问题吗?请花时间回答您的问题,以便代码格式正确。@Jure Entity类实现了Entity接口,我正在从Entity派生我的其他类,但不确定为什么它不能在DI中解决?请找出基本原因,默认构造函数被意外删除,导致MongoRepository对象不是在DI进程中创建的我不明白您的问题是什么。。很高兴听到它正在工作。