From 7d6cadf8ea3cebeadd8fdef94c195f5bcb004511 Mon Sep 17 00:00:00 2001 From: HammerheadShark666 <103519731+HammerheadShark666@users.noreply.github.com> Date: Sun, 15 Sep 2024 13:25:32 +0100 Subject: [PATCH] Add DatabaseConnectionStringNotFound exception and refactor sql provider function --- .../IServiceCollectionExtensions.cs | 45 +++++++++---------- .../Helpers/Constants.cs | 2 - .../Helpers/EnvironmentVariables.cs | 3 -- .../DatabaseConnectionStringNotFound.cs | 18 ++++++++ 4 files changed, 40 insertions(+), 28 deletions(-) create mode 100644 Microservice.Customer.Api/Helpers/Exceptions/DatabaseConnectionStringNotFound.cs diff --git a/Microservice.Customer.Api/Extensions/IServiceCollectionExtensions.cs b/Microservice.Customer.Api/Extensions/IServiceCollectionExtensions.cs index 1fd3c2c..7328a87 100644 --- a/Microservice.Customer.Api/Extensions/IServiceCollectionExtensions.cs +++ b/Microservice.Customer.Api/Extensions/IServiceCollectionExtensions.cs @@ -5,6 +5,7 @@ using Microservice.Customer.Api.Data.Repository; using Microservice.Customer.Api.Data.Repository.Interfaces; using Microservice.Customer.Api.Helpers; +using Microservice.Customer.Api.Helpers.Exceptions; using Microservice.Customer.Api.Helpers.Interfaces; using Microservice.Customer.Api.Helpers.Providers; using Microservice.Customer.Api.Helpers.Swagger; @@ -46,36 +47,21 @@ public static void ConfigureAutoMapper(this IServiceCollection services) services.AddAutoMapper(Assembly.GetAssembly(typeof(GetCustomerMapper))); } - //public static void ConfigureDatabaseContext(this IServiceCollection services, ConfigurationManager configuration) - //{ - // services.AddDbContextFactory(options => - // options.UseSqlServer(configuration.GetConnectionString(Helpers.Constants.DatabaseConnectionString), - // options => options.EnableRetryOnFailure())); - //} - public static void ConfigureSqlServer(this IServiceCollection services, IConfiguration configuration, IWebHostEnvironment environment) { if (environment.IsProduction()) { - services.AddDbContextFactory(options => - { - SqlAuthenticationProvider.SetProvider( - SqlAuthenticationMethod.ActiveDirectoryManagedIdentity, - new ProductionAzureSQLProvider()); - var sqlConnection = new SqlConnection(configuration.GetConnectionString(Constants.AzureDatabaseConnectionString)); - options.UseSqlServer(sqlConnection); - }); + var connectionString = configuration.GetConnectionString(Constants.AzureDatabaseConnectionString) + ?? throw new DatabaseConnectionStringNotFound("Production database connection string not found."); + + AddDbContextFactory(services, SqlAuthenticationMethod.ActiveDirectoryManagedIdentity, new ProductionAzureSQLProvider(), connectionString); } else if (environment.IsDevelopment()) { - services.AddDbContextFactory(options => - { - SqlAuthenticationProvider.SetProvider( - SqlAuthenticationMethod.ActiveDirectoryServicePrincipal, - new DevelopmentAzureSQLProvider()); - var sqlConnection = new SqlConnection(configuration.GetConnectionString(Constants.LocalDatabaseConnectionString)); - options.UseSqlServer(sqlConnection); - }); + var connectionString = configuration.GetConnectionString(Constants.LocalDatabaseConnectionString) + ?? throw new DatabaseConnectionStringNotFound("Development database connection string not found."); + + AddDbContextFactory(services, SqlAuthenticationMethod.ActiveDirectoryServicePrincipal, new DevelopmentAzureSQLProvider(), connectionString); } } @@ -112,4 +98,17 @@ public static void ConfigureSwagger(this IServiceCollection services) options.SupportNonNullableReferenceTypes(); }); } + + private static void AddDbContextFactory(IServiceCollection services, SqlAuthenticationMethod sqlAuthenticationMethod, SqlAuthenticationProvider sqlAuthenticationProvider, string connectionString) + { + services.AddDbContextFactory(options => + { + SqlAuthenticationProvider.SetProvider( + sqlAuthenticationMethod, + sqlAuthenticationProvider); + var sqlConnection = new SqlConnection(connectionString); + options.UseSqlServer(sqlConnection); + }); + } + } \ No newline at end of file diff --git a/Microservice.Customer.Api/Helpers/Constants.cs b/Microservice.Customer.Api/Helpers/Constants.cs index 77b7949..846126a 100644 --- a/Microservice.Customer.Api/Helpers/Constants.cs +++ b/Microservice.Customer.Api/Helpers/Constants.cs @@ -6,8 +6,6 @@ public class Constants public const string JwtAudience = "JWT_AUDIENCE"; public const string JwtSymmetricSecurityKey = "JWT_SYMMETRIC_SECURITY_KEY"; - //public const string DatabaseConnectionString = "SQLAZURECONNSTR_CUSTOMER"; - public const string AzureUserAssignedManagedIdentityClientId = "AZURE_USER_ASSIGNED_MANAGED_IDENTITY_CLIENT_ID"; public const string AzureDatabaseConnectionString = "AZURE_MANAGED_IDENTITY_SQL_CONNECTION"; diff --git a/Microservice.Customer.Api/Helpers/EnvironmentVariables.cs b/Microservice.Customer.Api/Helpers/EnvironmentVariables.cs index 1403d87..5bfe1c0 100644 --- a/Microservice.Customer.Api/Helpers/EnvironmentVariables.cs +++ b/Microservice.Customer.Api/Helpers/EnvironmentVariables.cs @@ -9,14 +9,11 @@ public class EnvironmentVariables public static string JwtIssuer => GetEnvironmentVariable(Constants.JwtIssuer); public static string JwtAudience => GetEnvironmentVariable(Constants.JwtAudience); public static string JwtSymmetricSecurityKey => GetEnvironmentVariable(Constants.JwtSymmetricSecurityKey); - // public static string AzureDatabaseConnectionString => GetEnvironmentVariable(Constants.AzureDatabaseConnectionString); public static string AzureUserAssignedManagedIdentityClientId => GetEnvironmentVariable(Constants.AzureUserAssignedManagedIdentityClientId); - //public static string LocalDatabaseConnectionString => GetEnvironmentVariable(Constants.LocalDatabaseConnectionString); public static string LocalDevelopmentClientId => GetEnvironmentVariable(Constants.AzureLocalDevelopmentClientId); public static string LocalDevelopmentClientSecret => GetEnvironmentVariable(Constants.AzureLocalDevelopmentClientSecret); public static string LocalDevelopmentTenantId => GetEnvironmentVariable(Constants.AzureLocalDevelopmentTenantId); - public static string GetEnvironmentVariable(string name) { var variable = Environment.GetEnvironmentVariable(name); diff --git a/Microservice.Customer.Api/Helpers/Exceptions/DatabaseConnectionStringNotFound.cs b/Microservice.Customer.Api/Helpers/Exceptions/DatabaseConnectionStringNotFound.cs new file mode 100644 index 0000000..b71df30 --- /dev/null +++ b/Microservice.Customer.Api/Helpers/Exceptions/DatabaseConnectionStringNotFound.cs @@ -0,0 +1,18 @@ +namespace Microservice.Customer.Api.Helpers.Exceptions; + +public class DatabaseConnectionStringNotFound : Exception +{ + public DatabaseConnectionStringNotFound() + { + } + + public DatabaseConnectionStringNotFound(string message) + : base(message) + { + } + + public DatabaseConnectionStringNotFound(string message, Exception inner) + : base(message, inner) + { + } +} \ No newline at end of file