【问题标题】:Moving multiple app.config's to something more global将多个 app.config 移动到更全球化的地方
【发布时间】:2012-05-14 20:31:08
【问题描述】:

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

如果可能,我想简化设置。目前我们有多个 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”。

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

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

谢谢!

【问题讨论】:

    标签: c# configuration selenium app-config


    【解决方案1】:

    我的建议是,将连接字符串保存在网络位置前的 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 文件并使用以下代码 sn-p 更新应用程序配置文件,

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

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

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

      类似这样的:

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

      【讨论】:

        【解决方案3】:

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

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

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

        【讨论】:

          猜你喜欢
          • 2010-11-18
          • 2015-01-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多