AutoFac / .NET Core - 注册DBcontext

时间:2018-05-01 23:21:57

标签: c# .net-core autofac ef-core-2.0

我有一个新的.NET Core Web API项目,它具有以下项目结构:

API - >业务/域名 - >基础设施

只有API方法,API非常薄。业务/域层具有我的所有业务逻辑。最后,我的Infrastructure层使用EF Core 2.0创建了我的DB类。

我知道使用.NET Core内置依赖注入我可以将API项目的引用添加到Infrastructure项目中,然后在StartUp.cs文件中添加以下代码:

services.AddDbContext<MyContext>(options => options.UseSqlServer(connectionString));

但是,我希望保持更传统的关注点分离。到目前为止,我已经在我的基础架构层中添加了一个模块,尝试进行如下注册:

builder.Register(c =>
        {
            var config = c.Resolve<IConfiguration>();

            var opt = new DbContextOptionsBuilder<MyContext>();
            opt.UseSqlServer(config.GetSection("ConnectionStrings:MyConnection:ConnectionString").Value);

            return new MyContext(opt.Options);
        }).AsImplementedInterfaces().InstancePerLifetimeScope();

然而,DBContext没有注册。尝试访问注入的DBContext的任何类都无法解析该参数。

有没有办法在.NET Core Web API项目中使用AuftoFac在单独的项目中注册DBContext?

5 个答案:

答案 0 :(得分:9)

我使用Autofac来注册HttpContextAccessorDbContext

builder.RegisterType<HttpContextAccessor>().As<IHttpContextAccessor>().SingleInstance();

builder
    .RegisterType<AppDbContext>()
    .WithParameter("options", DbContextOptionsFactory.Get())
    .InstancePerLifetimeScope();

DbContextOptionsFactory

public class DbContextOptionsFactory
{
    public static DbContextOptions<AppDbContext> Get()
    {
        var configuration = AppConfigurations.Get(WebContentDirectoryFinder.CalculateContentRootFolder());

        var builder = new DbContextOptionsBuilder<AppDbContext>();
        DbContextConfigurer.Configure(builder, configuration.GetConnectionString(AppConsts.ConnectionStringName));

        return builder.Options;
    }
}

DbContextConfigurer

public class DbContextConfigurer
{
    public static void Configure(DbContextOptionsBuilder<AppDbContext> builder, string connectionString)
    {
        builder.UseNpgsql(connectionString).UseLazyLoadingProxies();
    }
}

答案 1 :(得分:6)

我认为问题在于您尝试使用MyContext()注册AsImplementedInterfaces()。这不是DbContext通常如何注册的方式。你应该注册并自己解决课程。

答案 2 :(得分:2)

Autofac版本4.8.1的另一个简单解决方案

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().AddControllersAsServices();

        services.AddDbContext<MyContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ConnectionStrings:MyConnection:ConnectionString")));

        var builder = new ContainerBuilder();

        builder.Populate(services);

        //...
        // Your interface registration
        //...

        builder.Build(Autofac.Builder.ContainerBuildOptions.None);
    }

答案 3 :(得分:1)

在所需的项目中,您可以创建一个扩展方法,将上下文添加到集合

public static class MyDataExtensions {
    public static IServiceCollection AddMyData(this IServiceCollection services) {
        //...

        services.AddDbContext<MyContext>(options => options.UseSqlServer(connectionString));

        //...
    }
}

然后在启动时,只需要调用从其他项目公开的扩展

services.AddMyData();

//...other settings

API项目是组合根,因此它需要知道所有相关的依赖项。至少使用此扩展,您不必直接引用已使用的数据库上下文

答案 4 :(得分:0)

这是我使用的实现-它在Autofac 4.9.4中模仿了EF Core 3.1 registration。请确保根据您的要求调整范围。

public void RegisterContext<TContext>(ContainerBuilder builder)
    where TContext : DbContext
{
    builder.Register(componentContext =>
        {
            var serviceProvider = componentContext.Resolve<IServiceProvider>();
            var configuration = componentContext.Resolve<IConfiguration>();
            var dbContextOptions = new DbContextOptions<TContext>(new Dictionary<Type, IDbContextOptionsExtension>());
            var optionsBuilder = new DbContextOptionsBuilder<TContext>(dbContextOptions)
                .UseApplicationServiceProvider(serviceProvider)
                .UseSqlServer(configuration.GetConnectionString("MyConnectionString"),
                    serverOptions => serverOptions.EnableRetryOnFailure(5, TimeSpan.FromSeconds(30), null));

            return optionsBuilder.Options;
        }).As<DbContextOptions<TContext>>()
        .InstancePerLifetimeScope();

    builder.Register(context => context.Resolve<DbContextOptions<TContext>>())
        .As<DbContextOptions>()
        .InstancePerLifetimeScope();

    builder.RegisterType<TContext>()
        .AsSelf()
        .InstancePerLifetimeScope();
}