Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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# 具有嵌套依赖项的类型的依赖项注入_C#_Asp.net Core_Dependency Injection - Fatal编程技术网

C# 具有嵌套依赖项的类型的依赖项注入

C# 具有嵌套依赖项的类型的依赖项注入,c#,asp.net-core,dependency-injection,C#,Asp.net Core,Dependency Injection,我想创建一个包含另一个依赖项的类型 原因是,我可以使控制器依赖于一个具有更抽象的函数的基础结构包装器,这些函数实际上可以访问数据层 这里是具有dbcontext依赖关系的包装器 public Repository(IdbContext context) { _context = context; } 控制器构造函数: private readonly Repository _repo; public TheController(IRepository repo) { _rep

我想创建一个包含另一个依赖项的类型

原因是,我可以使控制器依赖于一个具有更抽象的函数的基础结构包装器,这些函数实际上可以访问数据层

这里是具有dbcontext依赖关系的包装器

public Repository(IdbContext context)
{
    _context = context;
}
控制器构造函数:

private readonly Repository _repo;

public TheController(IRepository repo)
{
    _repo = repo;
}
在Startup.cs中,
ConfigureServices

services.AddDbContext<dbContext>(options =>         
    options.UseSqlServer(Configuration.GetConnectionString("dbContext")));
services.AddTransient<IRepository, Repository>();
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“dbContext”));
services.AddTransient();
在Program.cs中

使用(var scope=host.Services.CreateScope())
{
var services=scope.ServiceProvider;
var context=services.GetRequiredService();
var_repo=services.GetRequiredService();
...
}
GetRequiredService()
失败,出现以下异常:

System.InvalidOperationException:'无法解析类型的服务 试图激活时出现“namespace.Models.IdbContext” “名称空间。存储库。存储库”。”


您需要注入与注册相同的类型

如果注册
dbContext
,则插入
dbContext

services.AddDbContext<dbContext>(options => ...);

public Repository(dbContext context)
{
    _context = context;
}
services.AddDbContext<IdbContext, dbContext>(options => ...);

public Repository(IdbContext context)
{
    _context = context;
}
以下是作为可运行演示的后一个选项:


IdbContext
接口与
dbContext
实现相关联

假定

public class dbContext: DbContext, IdbContext  {
    //...
}
它失败是因为它未注册,并且在将
IdbContext
注入存储库时,提供程序不知道初始化什么

services.AddDbContext<IdbContext, dbContext>(options =>         
    options.UseSqlServer(Configuration.GetConnectionString("dbContext")));
services.AddTransient<IRepository, Repository>();
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“dbContext”));
services.AddTransient();

您也可以像这样一次注册所有内容:
services.AddDbContext(options=>…)@ShaunLuttin是的,这更简单。将更新。谢谢。当我尝试这种更简单的形式时,出现错误“CS1061 C#'IServiceCollection'不包含的定义,并且找不到接受类型为'IServiceCollection'的第一个参数的扩展方法(是否缺少using指令或程序集引用?)@JP_medevice如果你查看来源,你会发现ShaunLuttin的建议是准确的,应该有效
services.AddDbContext<IdbContext, dbContext>(options =>         
    options.UseSqlServer(Configuration.GetConnectionString("dbContext")));
services.AddTransient<IRepository, Repository>();