[英]Register IPipelineBehavior in ASP.NET Core 3 with autofac
[英]AutoFac / .NET Core - Register DBcontext
我有一个新的 .NET Core Web API 项目,它具有以下项目结构:
API -> 业务/领域 -> 基础设施
API 非常精简,只有 API 方法。 业务/领域层包含我所有的业务逻辑。 最后,我的基础设施层拥有使用 EF Core 2.0 的数据库类。
我知道使用 .NET Core 内置依赖注入,我可以将 API 项目的引用添加到基础结构项目,然后在 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?
我使用 Autofac 注册HttpContextAccessor
和DbContext
。
builder
.RegisterType<HttpContextAccessor>()
.As<IHttpContextAccessor>()
.SingleInstance();
builder
.RegisterType<AppDbContext>()
.WithParameter("options", DbContextOptionsFactory.Get())
.InstancePerLifetimeScope();
数据库上下文选项工厂
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;
}
}
数据库上下文配置器
public class DbContextConfigurer
{
public static void Configure(
DbContextOptionsBuilder<AppDbContext> builder,
string connectionString)
{
builder.UseNpgsql(connectionString).UseLazyLoadingProxies();
}
}
我认为问题在于您正在尝试使用MyContext()
注册MyContext()
AsImplementedInterfaces()
。 这不是 DbContext 通常注册的方式。 您应该注册并解析类本身。
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 注册到 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();
}
在所需的项目中,您可以创建一个扩展方法,将上下文添加到集合中
public static class MyDataExtensions {
public static IServiceCollection AddMyData(this IServiceCollection services) {
//...
services.AddDbContext<MyContext>(options => options.UseSqlServer(connectionString));
//...
}
}
有了它,然后在您启动时,只需调用从其他项目公开的扩展即可
services.AddMyData();
//...other settings
API 项目是组合根,因此无论如何它都需要知道所有相关的依赖项。 至少使用此扩展,您不必直接引用使用的 db 上下文,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.