Core.Blueprint.DAL/Core.Blueprint.DAL.Infrastrcture/Configure/DependencyInjection.cs
Sergio Matias Urquin 6358f5f199 Add project files.
2025-04-29 18:39:57 -06:00

76 lines
3.2 KiB
C#

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<SqlServerContext>(options => options.UseSqlServer(configuration.GetConnectionString("SQLServerConnString")));
// LogFactory
services.ConfigureLogging();
//MongoDB
services.Configure<MongoDbSettings>(configuration.GetRequiredSection("MongoDbSettings"));
services.AddScoped<IMongoDbSettings>(serviceProvider =>
serviceProvider.GetRequiredService<IOptions<MongoDbSettings>>().Value);
services.AddScoped<IMongoContext, MongoContext>();
services.AddScoped(serviceProvider =>
{
var mongoContext = serviceProvider.GetRequiredService<IMongoContext>();
return mongoContext.Database;
});
services.AddScoped<IProxyBlobStorage, ProxyBlobStorage>();
services.AddScoped<ISampleImageRepository, SampleImageRepository>();
services.AddScoped<ISampleItemRepository, SampleItemRepository>();
services.AddScoped<IBlueprintRepository, BlueprintRepository>();
services.AddScoped<ISecretRepository, SecretRepository>();
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);
}
}
}