Files
social-media/src/Infrastructure/DependencyInjection.cs
2024-10-20 15:48:46 -04:00

54 lines
1.9 KiB
C#

using Hutopy.Infrastructure.BlobStorage;
using Hutopy.Infrastructure.Data;
using Hutopy.Infrastructure.Identity;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Hutopy.Infrastructure;
public static class DependencyInjection
{
public static IServiceCollection AddInfrastructureServices(this IServiceCollection services,
IConfiguration configuration)
{
// Replace password in the connection string with env var in local environment.
// Prod will use the connectionString stored in the vault with password in it directly.
var connectionString = configuration.GetConnectionString("MssqlConnection")
?? throw new InvalidOperationException("Missing ConnectionStrings:MssqlConnection");
services.AddDbContext<ApplicationDbContext>((sp, options) =>
{
options.AddInterceptors(sp.GetServices<ISaveChangesInterceptor>());
options.UseSqlServer(connectionString);
});
services.AddScoped<ApplicationDbContextInitializer>();
services.AddAuthentication()
.AddBearerToken(IdentityConstants.BearerScheme);
services.AddAuthorizationBuilder();
services
.AddIdentityCore<ApplicationUser>()
.AddUserManager<ApplicationUserManager>()
.AddRoles<ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddApiEndpoints()
.AddSignInManager<SignInManager<ApplicationUser>>()
.AddDefaultTokenProviders();
// Singleton services
services.AddSingleton(TimeProvider.System);
services.AddSingleton<AzureBlobStorage>();
// Scoped services
services.AddScoped<IdentityService>();
return services;
}
}