DI Setup using HostApplicationBuilder
This commit is contained in:
@@ -1,32 +1,27 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace EF_Demo;
|
||||
|
||||
internal class DISetup
|
||||
internal static class DISetup
|
||||
{
|
||||
public static ServiceProvider BuildDI()
|
||||
public static IHost BuildHost(string[] args)
|
||||
{
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.AddJsonFile("appsettings.json")
|
||||
.AddUserSecrets<Program>()
|
||||
.Build();
|
||||
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
|
||||
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
builder.Services.AddDbContextFactory<Data.Postgres.EventsContext>(options => {
|
||||
options.UseNpgsql(builder.Configuration.GetConnectionString("EventsPostgres"));
|
||||
});
|
||||
builder.Services.AddDbContextFactory<Data.MSSQL.EventsContext>(options =>
|
||||
{
|
||||
options.UseSqlServer(builder.Configuration.GetConnectionString("EventsMssql"));
|
||||
});
|
||||
|
||||
builder.Services.AddTransient<Demo>();
|
||||
|
||||
var provider = services.AddLogging(configure => {
|
||||
configure.AddConfiguration(configuration.GetSection("Logging"));
|
||||
configure.AddConsole();
|
||||
})
|
||||
.AddDbContext<Data.MSSQL.EventsContext>(options => {
|
||||
options.UseSqlServer(configuration.GetConnectionString("EventsMssql"));
|
||||
}, contextLifetime: ServiceLifetime.Transient)
|
||||
.AddDbContext<Data.Postgres.EventsContext>(options => {
|
||||
options.UseNpgsql(configuration.GetConnectionString("EventsPostgres"));
|
||||
}, contextLifetime: ServiceLifetime.Transient)
|
||||
.BuildServiceProvider();
|
||||
return provider;
|
||||
return builder.Build();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user