@@ -111,20 +111,24 @@ private static void LoadConfiguration(string[] args)
111111
112112 private static void SetConnectionString ( )
113113 {
114- var config = ConfigurationManager . OpenExeConfiguration ( ConfigurationUserLevel . None ) ;
115- var connectionStringsSection = ( ConnectionStringsSection ) config . GetSection ( "connectionStrings" ) ;
114+ ConfigProcessor . LoadAndProcessEnvVariables ( Configuration . AsEnumerable ( ) ) ;
116115
117- //var test = Configuration["ConnectionStrings:ResgridContext"];
116+ // Inject the connection string into the in-memory ConfigurationManager.ConnectionStrings
117+ // collection for legacy consumers. We intentionally do NOT persist the .dll.config to disk
118+ // via config.Save(): that fails on read-only / non-root hardened (DHI) containers. This
119+ // mirrors the web apps' Startup, which only inject in-memory and never write to disk.
120+ var settings = ConfigurationManager . ConnectionStrings ;
121+ var element = typeof ( ConfigurationElement ) . GetField ( "_readOnly" , System . Reflection . BindingFlags . Instance | System . Reflection . BindingFlags . NonPublic ) ;
122+ var collection = typeof ( ConfigurationElementCollection ) . GetField ( "_readOnly" , System . Reflection . BindingFlags . Instance | System . Reflection . BindingFlags . NonPublic ) ;
118123
119- ConfigProcessor . LoadAndProcessEnvVariables ( Configuration . AsEnumerable ( ) ) ;
124+ element . SetValue ( settings , false ) ;
125+ collection . SetValue ( settings , false ) ;
120126
121- if ( connectionStringsSection . ConnectionStrings [ "ResgridContext" ] != null )
122- connectionStringsSection . ConnectionStrings [ "ResgridContext" ] . ConnectionString = DataConfig . ConnectionString ;
123- else
124- connectionStringsSection . ConnectionStrings . Add ( new ConnectionStringSettings ( "ResgridContext" , DataConfig . ConnectionString ) ) ;
127+ if ( settings [ "ResgridContext" ] == null )
128+ settings . Add ( new ConnectionStringSettings ( "ResgridContext" , DataConfig . ConnectionString ) ) ;
125129
126- config . Save ( ) ;
127- ConfigurationManager . RefreshSection ( "connectionStrings" ) ;
130+ collection . SetValue ( settings , true ) ;
131+ element . SetValue ( settings , true ) ;
128132 }
129133 }
130134}
0 commit comments