Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.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# 通过autofac类寄存器将DbContext对象作为参数传递_C#_Asp.net Mvc_Entity Framework_Autofac - Fatal编程技术网

C# 通过autofac类寄存器将DbContext对象作为参数传递

C# 通过autofac类寄存器将DbContext对象作为参数传递,c#,asp.net-mvc,entity-framework,autofac,C#,Asp.net Mvc,Entity Framework,Autofac,我在MVC中有一个两层架构的应用程序(Web和服务)。我已经在web project中的startup方法中注册了我的服务类,如下所示 protected void Application_Start() { var containerBuilder = new ContainerBuilder(); containerBuilder.RegisterControllers(typeof(MvcApplication).Assembly); containerBuild

我在MVC中有一个两层架构的应用程序(Web和服务)。我已经在web project中的startup方法中注册了我的服务类,如下所示

protected void Application_Start()
{
    var containerBuilder = new ContainerBuilder();
    containerBuilder.RegisterControllers(typeof(MvcApplication).Assembly);

    containerBuilder.RegisterModelBinders(Assembly.GetExecutingAssembly());
    containerBuilder.RegisterModelBinderProvider();

    containerBuilder.RegisterType<SearchService>().As<ISearchService>();


    var container = containerBuilder.Build();
    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}

我认为您在MVC控制器中使用SearchService,因此必须在那里创建ISearchService实例。在这种情况下,Autofac可以在控制器中进行构造函数注入

public class ExampleController : Controller
{
    ISearchService _svc;

    public B2BHealthApiController(ISearchService s)
    {
        _svc = s;
    }
}
当Autofac创建ISearchService的实例时,引擎定义ISearchService需要IApplicationDbContext的实例并自动创建它(相同的构造函数注入)

因此,您只需说出Autofac,其中包含IApplicationDbContext和ISearchService实例。添加到您的应用程序\u启动

builder.RegisterType<ApplicationDbContext>()                
            .As<IApplicationDbContext>()
            .InstancePerDependency();

builder.RegisterType<SearchService>()               
            .As<ISearchService>()
            .InstancePerRequest();
builder.RegisterType()
.As()
.InstancePerDependence();
builder.RegisterType()
.As()
.InstancePerRequest();

您只需为DbContext设置一个绑定,我不知道如何使用Autofac,但我已经为您添加了Autofac标记,希望有人会回答。看起来您已将容器设置为扫描当前程序集,但您希望它稍后扫描服务层/数据以查找DbContext,以便可以解析它。或者定义一个
IDbContextFactory
,您可以在
SearchService
构造函数中使用它来创建所需上下文的实例。无论哪种方式,您都需要在容器中注册一些内容,以便容器可以连接所有必要的接口及其实现。您解决了这个问题吗?
public class SearchService : ISearchService
{
    IApplicationDbContext _dbContext;

    public QueueService(IApplicationDbContext context)
    {
       _dbContext = context;
    }
}
public class ExampleController : Controller
{
    ISearchService _svc;

    public B2BHealthApiController(ISearchService s)
    {
        _svc = s;
    }
}
builder.RegisterType<ApplicationDbContext>()                
            .As<IApplicationDbContext>()
            .InstancePerDependency();

builder.RegisterType<SearchService>()               
            .As<ISearchService>()
            .InstancePerRequest();