@@ -27,10 +27,67 @@ public class DbService : IDbService
2727 public DbService ( ILogger < DbService > logger )
2828 {
2929 _logger = logger ;
30- client = new MongoClient ( GeneralConfigStatic . tlconfig ? . mongo_connection_string ?? Environment . GetEnvironmentVariable ( "connectionString" ) ) ;
31- currentDatabase = getDatabase ( "default" ) ;
32- this . dbName = "default" ;
33- _logger . LogInformation ( "DbService initialized - Connected to MongoDB" ) ;
30+ var connectionString = GeneralConfigStatic . tlconfig ? . mongo_connection_string
31+ ?? Environment . GetEnvironmentVariable ( "connectionString" ) ;
32+
33+ if ( string . IsNullOrWhiteSpace ( connectionString ) )
34+ {
35+ _logger . LogWarning ( "DbService: MongoDB connection string not configured - setup required" ) ;
36+ // Use a default that will fail gracefully when actually used
37+ connectionString = "mongodb://localhost:27017" ;
38+ }
39+
40+ try
41+ {
42+ var settings = MongoClientSettings . FromConnectionString ( connectionString ) ;
43+ settings . ServerSelectionTimeout = TimeSpan . FromSeconds ( 5 ) ;
44+ settings . ConnectTimeout = TimeSpan . FromSeconds ( 5 ) ;
45+ client = new MongoClient ( settings ) ;
46+ currentDatabase = getDatabase ( "default" ) ;
47+ this . dbName = "default" ;
48+ _logger . LogInformation ( "DbService initialized - Connected to MongoDB" ) ;
49+ }
50+ catch ( Exception ex )
51+ {
52+ _logger . LogWarning ( ex , "DbService: Could not connect to MongoDB - setup may be required" ) ;
53+ // Create client anyway for later use after setup
54+ client = new MongoClient ( connectionString ) ;
55+ currentDatabase = client . GetDatabase ( "default" ) ;
56+ this . dbName = "default" ;
57+ }
58+ }
59+
60+ /// <summary>
61+ /// Reinitializes the MongoDB connection with a new connection string.
62+ /// Call this after setup when the connection string has been configured.
63+ /// </summary>
64+ public void ReinitializeConnection ( string connectionString )
65+ {
66+ if ( string . IsNullOrWhiteSpace ( connectionString ) )
67+ {
68+ _logger . LogWarning ( "ReinitializeConnection: Connection string is empty" ) ;
69+ return ;
70+ }
71+
72+ try
73+ {
74+ _logger . LogInformation ( "Reinitializing MongoDB connection..." ) ;
75+ var settings = MongoClientSettings . FromConnectionString ( connectionString ) ;
76+ settings . ServerSelectionTimeout = TimeSpan . FromSeconds ( 5 ) ;
77+ settings . ConnectTimeout = TimeSpan . FromSeconds ( 5 ) ;
78+
79+ // Create new client with updated connection string
80+ client = new MongoClient ( settings ) ;
81+ currentDatabase = getDatabase ( "default" ) ;
82+ this . dbName = "default" ;
83+
84+ _logger . LogInformation ( "DbService reinitialized - Connected to MongoDB with new connection string" ) ;
85+ }
86+ catch ( Exception ex )
87+ {
88+ _logger . LogError ( ex , "Failed to reinitialize MongoDB connection: {Message}" , ex . Message ) ;
89+ throw ;
90+ }
3491 }
3592
3693 public async Task < IClientSessionHandle > getSession ( )
0 commit comments