user952342
user952342

Reputation: 2752

Moving multiple app.config's to something more global

We have several c# selenium test suites that are run on several servers by several testers.

I want to simplify the settings if possible. Currently we have multiple c# selenium projects each with their own app.config. When I want to change the server, I need to change each and every app.config. My app.config currently looks something like this:

<connectionStrings>
    <add name="Company"
         connectionString="Data Source=func3_db1;Initial Catalog=CompanyProduction;Integrated Security=SSPI;"/>
    <add name="CompanyProductionEntities"
         connectionString="metadata=res://*/DataAccess.CompanyEntities.csdl|res://*/DataAccess.CompanyEntities.ssdl|res://*/DataAccess.CompanyEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=func3_db1;initial catalog=CompanyProduction;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;"
         providerName="System.Data.EntityClient" />
</connectionStrings>

We also have some settings in Windows Environment variables. It's kind of an obscure way of doing it but it works pretty well. To access these settings, we just do something like this:

var value = Environment.GetEnvironmentVariable(varName, EnvironmentVariableTarget.User);

So, we have a variable called "CompanyTestBrowser" which can be set to "Firefox" or "Chrome" or "IE".

I like environment variables because the powershell scripts that run all of our selenium tests can easily alter the variables whenever they need to.

However, I can't seem to pull those DB strings out of this app.config. How can I move them into something that can be a bit more global & dynamic. Ideally I only have to set in 1 single place? Ideally, I could move them into environment variables like the other or a config file that sits outside the c# project.

Thanks!

Upvotes: 2

Views: 1923

Answers (3)

user952342
user952342

Reputation: 2752

I used the accepted solution above. This is my exact code (which is only slightly different than above).

This solutions effectively keeps my current app.config file. All other settings in that app.config file are kept, except I "override" the "connectionString" part with my custom one.

 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    System.Console.WriteLine("Starting to write app.config stuff");

    //Change the Admin's app.config where name=companyProductionEntities
    config.ConnectionStrings.ConnectionStrings["companyProductionEntities"].ConnectionString =
        string.Format(@"metadata=res://*/DataAccess.companyEntities.csdl|res://*/DataAccess.companyEntities.ssdl|res://*/DataAccess.companyEntities.msl;provider=System.Data.SqlClient;provider connection string=';data source={0};initial catalog=companyProduction;integrated security=True;multipleactiveresultsets=True;App=EntityFramework';", SeleniumConfiguration.SimpleDatabaseConnectionString);
    config.Save(ConfigurationSaveMode.Modified, true);
    ConfigurationManager.RefreshSection("connectionStrings");

Upvotes: 0

Cinchoo
Cinchoo

Reputation: 6332

My advise would be, have the connection strings maintained in a xml file in a networklocation ex. \machine-name\DBConnectionString.config as below format

<connectionStrings>
    <add name="Company"
         connectionString="Data Source=func3_db1;Initial Catalog=CompanyProduction;Integrated Security=SSPI;"/>
    <add name="CompanyProductionEntities"
         connectionString="metadata=res://*/DataAccess.CompanyEntities.csdl|res://*/DataAccess.CompanyEntities.ssdl|res://*/DataAccess.CompanyEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=func3_db1;initial catalog=CompanyProduction;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;"
         providerName="System.Data.EntityClient" />
</connectionStrings>

At your application startup routine, read \machine-name\DBConnectionString.config file and update application config file using the below code snippet,

// Get the application configuration file.
System.Configuration.Configuration config =
        ConfigurationManager.OpenExeConfiguration(
        ConfigurationUserLevel.None);

// Create a connection string element and
// save it to the configuration file.

//Read the \\machine-name\DBConnectionString.config file

// Create a connection string element.
ConnectionStringSettings csSettings =
        new ConnectionStringSettings("My Connection",
        "LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;" +
        "Initial Catalog=aspnetdb", "System.Data.SqlClient");

// Get the connection strings section.
ConnectionStringsSection csSection =
    config.ConnectionStrings;

// Add the new element.
csSection.ConnectionStrings.Add(csSettings);

// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);

Hope this helps.

Upvotes: 1

Eric
Eric

Reputation: 401

You could add a key to app.config which has a path to an XML file which has all the common settings in it and then write a new ConfigurationManager class to first try to pull out a value from app.config and if not found, open the XML file and look for it in there

Something like this:

  <appSettings>
    <add key="ConfigFileSettings" value="\\MyServer\CommonSetting\settings.xml"/>

Upvotes: 0

Related Questions