diff --git a/Directory.Packages.props b/Directory.Packages.props index a6c3ea028..93fd38dd8 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -48,6 +48,7 @@ + diff --git a/src/Application/Application.csproj b/src/Application/Application.csproj index e5a5c434c..ebb9170ed 100644 --- a/src/Application/Application.csproj +++ b/src/Application/Application.csproj @@ -10,6 +10,7 @@ + diff --git a/src/Application/DependencyInjection.cs b/src/Application/DependencyInjection.cs index d92639203..d29044575 100644 --- a/src/Application/DependencyInjection.cs +++ b/src/Application/DependencyInjection.cs @@ -1,24 +1,23 @@ using System.Reflection; using CleanArchitecture.Application.Common.Behaviours; +using Microsoft.Extensions.Hosting; namespace Microsoft.Extensions.DependencyInjection; public static class DependencyInjection { - public static IServiceCollection AddApplicationServices(this IServiceCollection services) + public static void AddApplicationServices(this IHostApplicationBuilder builder) { - services.AddAutoMapper(Assembly.GetExecutingAssembly()); + builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly()); - services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly()); + builder.Services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly()); - services.AddMediatR(cfg => { + builder.Services.AddMediatR(cfg => { cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()); cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(UnhandledExceptionBehaviour<,>)); cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(AuthorizationBehaviour<,>)); cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>)); cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(PerformanceBehaviour<,>)); }); - - return services; } } diff --git a/src/Infrastructure/DependencyInjection.cs b/src/Infrastructure/DependencyInjection.cs index 3208de031..001a272dd 100644 --- a/src/Infrastructure/DependencyInjection.cs +++ b/src/Infrastructure/DependencyInjection.cs @@ -7,21 +7,22 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; namespace Microsoft.Extensions.DependencyInjection; public static class DependencyInjection { - public static IServiceCollection AddInfrastructureServices(this IServiceCollection services, IConfiguration configuration) + public static void AddInfrastructureServices(this IHostApplicationBuilder builder) { - var connectionString = configuration.GetConnectionString("DefaultConnection"); + var connectionString = builder.Configuration.GetConnectionString("DefaultConnection"); Guard.Against.Null(connectionString, message: "Connection string 'DefaultConnection' not found."); - services.AddScoped(); - services.AddScoped(); + builder.Services.AddScoped(); + builder.Services.AddScoped(); - services.AddDbContext((sp, options) => + builder.Services.AddDbContext((sp, options) => { options.AddInterceptors(sp.GetServices()); @@ -32,34 +33,32 @@ public static IServiceCollection AddInfrastructureServices(this IServiceCollecti #endif }); - services.AddScoped(provider => provider.GetRequiredService()); + builder.Services.AddScoped(provider => provider.GetRequiredService()); - services.AddScoped(); + builder.Services.AddScoped(); #if (UseApiOnly) - services.AddAuthentication() + builder.Services.AddAuthentication() .AddBearerToken(IdentityConstants.BearerScheme); - services.AddAuthorizationBuilder(); + builder.Services.AddAuthorizationBuilder(); - services + builder.Services .AddIdentityCore() .AddRoles() .AddEntityFrameworkStores() .AddApiEndpoints(); #else - services + builder.Services .AddDefaultIdentity() .AddRoles() .AddEntityFrameworkStores(); #endif - services.AddSingleton(TimeProvider.System); - services.AddTransient(); + builder.Services.AddSingleton(TimeProvider.System); + builder.Services.AddTransient(); - services.AddAuthorization(options => + builder.Services.AddAuthorization(options => options.AddPolicy(Policies.CanPurge, policy => policy.RequireRole(Roles.Administrator))); - - return services; } } diff --git a/src/Web/DependencyInjection.cs b/src/Web/DependencyInjection.cs index e57a69c97..b7613fc45 100644 --- a/src/Web/DependencyInjection.cs +++ b/src/Web/DependencyInjection.cs @@ -13,28 +13,28 @@ namespace Microsoft.Extensions.DependencyInjection; public static class DependencyInjection { - public static IServiceCollection AddWebServices(this IServiceCollection services) + public static void AddWebServices(this IHostApplicationBuilder builder) { - services.AddDatabaseDeveloperPageExceptionFilter(); + builder.Services.AddDatabaseDeveloperPageExceptionFilter(); - services.AddScoped(); + builder.Services.AddScoped(); - services.AddHttpContextAccessor(); + builder.Services.AddHttpContextAccessor(); - services.AddHealthChecks() + builder.Services.AddHealthChecks() .AddDbContextCheck(); - services.AddExceptionHandler(); + builder.Services.AddExceptionHandler(); - services.AddRazorPages(); + builder.Services.AddRazorPages(); // Customise default API behaviour - services.Configure(options => + builder.Services.Configure(options => options.SuppressModelStateInvalidFilter = true); - services.AddEndpointsApiExplorer(); + builder.Services.AddEndpointsApiExplorer(); - services.AddOpenApiDocument((configure, sp) => + builder.Services.AddOpenApiDocument((configure, sp) => { configure.Title = "CleanArchitecture API"; @@ -51,20 +51,16 @@ public static IServiceCollection AddWebServices(this IServiceCollection services configure.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("JWT")); #endif }); - - return services; } - public static IServiceCollection AddKeyVaultIfConfigured(this IServiceCollection services, ConfigurationManager configuration) + public static void AddKeyVaultIfConfigured(this IHostApplicationBuilder builder) { - var keyVaultUri = configuration["AZURE_KEY_VAULT_ENDPOINT"]; + var keyVaultUri = builder.Configuration["AZURE_KEY_VAULT_ENDPOINT"]; if (!string.IsNullOrWhiteSpace(keyVaultUri)) { - configuration.AddAzureKeyVault( + builder.Configuration.AddAzureKeyVault( new Uri(keyVaultUri), new DefaultAzureCredential()); } - - return services; } } diff --git a/src/Web/Program.cs b/src/Web/Program.cs index 35acd8d10..4579ab16f 100644 --- a/src/Web/Program.cs +++ b/src/Web/Program.cs @@ -1,17 +1,15 @@ using CleanArchitecture.Infrastructure.Data; var builder = WebApplication.CreateBuilder(args); - + +// Add services to the container. #if (UseAspire) builder.AddServiceDefaults(); #endif - -// Add services to the container. -builder.Services.AddKeyVaultIfConfigured(builder.Configuration); - -builder.Services.AddApplicationServices(); -builder.Services.AddInfrastructureServices(builder.Configuration); -builder.Services.AddWebServices(); +builder.AddKeyVaultIfConfigured(); +builder.AddApplicationServices(); +builder.AddInfrastructureServices(); +builder.AddWebServices(); var app = builder.Build();