AutoFac / .NET Core - 注册 DBcontext

AutoFac / .NET Core - Register DBcontext

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

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

API 非常精简,只有 API 方法。业务/领域层具有我所有的业务逻辑。最后,我的基础设施层有我的数据库 classes 使用 EF Core 2.0。

我知道使用 .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 的 class 都无法解析该参数。

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

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

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

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

        //...
    }
}

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

services.AddMyData();

//...other settings

API项目是组合根,所以无论如何它都需要知道所有相关的依赖项。至少有了这个扩展,你不必直接引用使用的数据库上下文,

我认为问题在于您正尝试使用 AsImplementedInterfaces() 注册 MyContext()。这不是 DbContext 通常注册的方式。您应该自己注册并解析 class。

我使用 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();
    }
}

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);
    }

这是我使用的一个实现 - 它模仿 EF Core 3.1 registration 和 Autofac 4.9.4。请务必根据您的要求调整范围。

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();
}