将多个 app.config 移动到更全球化的东西

本文关键字:全球化 移动 app config | 更新日期: 2023-09-27 18:32:30

我们有几个 c# 硒测试套件,由多个测试人员在多个服务器上运行。

如果可能的话,我想简化设置。目前,我们有多个 c# Selenium 项目,每个项目都有自己的 app.config。 当我想更改服务器时,我需要更改每个app.config。 我的 app.config 目前看起来像这样:

<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>

我们在 Windows 环境变量中也有一些设置。 这是一种晦涩难懂的方式,但它效果很好。 要访问这些设置,我们只需执行以下操作:

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

因此,我们有一个名为">CompanyTestBrowser"的变量,可以设置为">Firefox"或">Chrome"或">IE"。

我喜欢环境变量,因为运行我们所有硒测试的powershell脚本可以在需要时轻松更改变量。

但是,我似乎无法从此 app.config 中提取这些数据库字符串。 我怎样才能把它们变成一个更加全球化和动态的东西。 理想情况下,我只需要设置在 1 个地方? 理想情况下,我可以将它们移动到环境变量中,例如其他变量或位于 c# 项目外部的配置文件。

谢谢!

将多个 app.config 移动到更全球化的东西

我的建议是,将连接字符串维护在网络位置的 xml 文件中,例如 ''machine-name''DBConnectionString.config 如下格式

<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>

在应用程序启动例程中,读取 ''machine-name''DBConnectionString.config 文件并使用以下代码片段更新应用程序配置文件,

// 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);

希望这有帮助。

你可以向 app.config 添加一个键,该键具有指向包含所有常见设置的 XML 文件的路径,然后编写一个新的 ConfigurationManager 类以首先尝试从 app.config 中提取值,如果未找到,请打开 XML 文件并在其中查找它

像这样:

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

我使用了上面接受的解决方案。 这是我的确切代码(仅与上面略有不同(。

此解决方案有效地保留了我当前的 app.config 文件。 该 app.config 文件中的所有其他设置都被保留,除了我用我的自定义设置"覆盖"连接字符串"部分。

 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");