using Azure.Identity; using Core.Blueprint.DAL.Infrastructure.Context; using Core.Blueprint.DAL.Infrastructure.Contracts; using Core.Blueprint.DAL.Infrastructure.Proxies; using Core.Blueprint.DAL.Infrastructure.Repository; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Azure; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using System.Reflection; using Serilog; using Serilog.Events; using Serilog.Exceptions; namespace Core.Blueprint.DAL.Infrastructure.Configure { public static class DependencyInjection { public static IServiceCollection RegisterInfrastructure(this IServiceCollection services, IConfiguration configuration) { var chainedCredentials = new ChainedTokenCredential( new ManagedIdentityCredential(), new SharedTokenCacheCredential(), new VisualStudioCredential(), new VisualStudioCodeCredential() ); services.AddAzureClients(cfg => { cfg.AddBlobServiceClient(configuration.GetRequiredSection("Azure:BlobStorage")).WithCredential(chainedCredentials); cfg.AddSecretClient(configuration.GetRequiredSection("Azure:KeyVault")).WithCredential(chainedCredentials); }); services.AddDbContext(options => options.UseSqlServer(configuration.GetConnectionString("SQLServerConnString"))); // LogFactory services.ConfigureLogging(); //MongoDB services.Configure(configuration.GetRequiredSection("MongoDbSettings")); services.AddScoped(serviceProvider => serviceProvider.GetRequiredService>().Value); services.AddScoped(); services.AddScoped(serviceProvider => { var mongoContext = serviceProvider.GetRequiredService(); return mongoContext.Database; }); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); return services; } public static void ConfigureLogging(this IServiceCollection services) { Log.Logger = new LoggerConfiguration() .Enrich.FromLogContext() .Enrich.WithMachineName() .Enrich.WithProcessId() .Enrich.WithThreadId() .Enrich.WithProperty("Application", Assembly.GetExecutingAssembly().GetName().Name) .Enrich.WithExceptionDetails() .MinimumLevel.Is(LogEventLevel.Information) .WriteTo.Console() .CreateLogger(); services.AddSingleton(Log.Logger); } } }