I have a new .NET Core Web API project that has the following projects structure:
API -> Business / Domain -> Infrastructure
The API is very thin with only the API methods. The Business / Domain layer has all my business logic. And finally, my Infrastructure layer has my DB classes using EF Core 2.0.
I know using .NET Core built-in Dependency Injection I can add a reference from the API project to the Infrastructure project, then add the following code in the StartUp.cs file:
services.AddDbContext<MyContext>(options => options.UseSqlServer(connectionString));
However, I would like to maintain a more traditional separation of concerns. So far I have added a module in my Infrastructure layer that attempts to make the registration like so:
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();
The DBContext, however, is not getting registered. Any class that attempts to access the injected DBContext cannot resolve the parameter.
Is there a way to register the DBContext in a separate project using AuftoFac in a .NET Core Web API Project?
EF Core can be added to this configuration using AddDbContext in the ConfigureServices method of Startup.cs. For example: This example registers a DbContext subclass called ApplicationDbContext as a scoped service in the ASP.NET Core application service provider (a.k.a. the dependency injection container).
Especially if you use async code, you'll discover situations where the DbContext has been disposed, which may take some time to sort out. The ideal way to configure a DbContext in ASP.NET or ASP.NET Core is via a DI container.
The DBContext, however, is not getting registered. Any class that attempts to access the injected DBContext cannot resolve the parameter. Is there a way to register the DBContext in a separate project using AuftoFac in a .NET Core Web API Project? Show activity on this post.
The ideal way to configure a DbContext in ASP.NET or ASP.NET Core is via a DI container. You can avoid literally all of this pain if you just let a DI container (like Autofac) manage your DbContext instances and their lifetime for you. If you also use a repository or similar abstraction, be sure to set its lifetime to match your DbContext lifetime.
I use Autofac to register both HttpContextAccessor
and DbContext
.
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(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With