Last active
May 2, 2025 20:08
-
-
Save tracker1/503b1584dec0fa432e31495b97edbf0c to your computer and use it in GitHub Desktop.
Revisions
-
tracker1 revised this gist
May 2, 2025 . 1 changed file with 2 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,11 +3,8 @@ using Microsoft.Data.SqlClient; public class DatabaseConnections : IDisposable { public IDbConnection? FirstConnection { get { return getSqlConnection("First"); } } public IDbConnection? SecondConnection { get { return getSqlConnection("Second"); } } private IConfiguration config {get;set;} private ConcurrentDictionary<string, IDbConnection> Connections = new ConcurrentDictionary<string, IDbConnection>(); -
tracker1 created this gist
May 2, 2025 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,69 @@ using System.Collections.Concurrent; using System.Data; using Microsoft.Data.SqlClient; public class DatabaseConnections : IDisposable { public IDbConnection? CarrierProvisioningConnection { get { return getSqlConnection("Billing"); } } public IDbConnection? CarrierSubscriberConnection { get { return getSqlConnection("Billing"); } } public IDbConnection? SystemObservationConnection { get { return getSqlConnection("Billing"); } } public IDbConnection? BillingConnection { get { return getSqlConnection("Billing"); } } private IConfiguration config {get;set;} private ConcurrentDictionary<string, IDbConnection> Connections = new ConcurrentDictionary<string, IDbConnection>(); public DatabaseConnections(IConfiguration config) { this.config = config; } ~DatabaseConnections() { this.Dispose(); } private IDbConnection? getSqlConnection(string name) { IDbConnection? cn; if (this.Connections.TryGetValue(name, out cn)) { return cn; } var cs = this.config.GetConnectionString(name); if (string.IsNullOrWhiteSpace(cs)) { return null; } try { var csb = new SqlConnectionStringBuilder(cs); // TODO: configure for allowed cert chain csb.TrustServerCertificate = true; cn = new SqlConnection(csb.ConnectionString); this.Connections.TryAdd(name, cn); return cn; } catch(Exception) { return null; } } public void Dispose() { var keys = this.Connections.Keys; foreach (var key in keys) { IDbConnection? cn; if (Connections.Remove(key, out cn)) { cn.Dispose(); } } } } public static class DatabaseConnectionsExtensions { public static void AddDatabaseConnections(this IServiceCollection services) { services.AddScoped(o => { var config = o.GetService<IConfiguration>(); if (config is null) throw new ApplicationException("Missing configuration."); return new DatabaseConnections(config); }); } }