c# - 将多个 app.config 移动到更全局化的地方

标签 c# configuration selenium app-config

我们有几个由多个测试人员在多个服务器上运行的 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 中提取那些数据库字符串。我怎样才能将它们转移到更具全局性和动态性的东西中。理想情况下,我只需要在一个地方设置?理想情况下,我可以像另一个一样将它们移动到环境变量或位于 c# 项目外部的配置文件中。

谢谢!

最佳答案

我的建议是,将连接字符串保存在网络位置 ex 的 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);

希望这对您有所帮助。

关于c# - 将多个 app.config 移动到更全局化的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10351193/

相关文章:

c# - Visual Studio 2010 程序集引用问题

C# 字典 ValueOrNull/ValueorDefault

c# - 用户偏好匹配推荐系统( PIL 逊相关)

hadoop - 亚马逊电子病历 : Set unique number of mappers and reducers per EMR instance

scala-加载 jar 内打包的属性文件时出错

java - 获取 Groovy 项目中使用的 Selenium 版本

c# - 将最小起订量与 Linq Any() 结合使用

configuration - 如何隐藏 tmux 状态栏中的窗口列表?

selenium - docker run -v bindmount 失败

python - 如何在登录的 selenium 中执行类而不是打开新的 chrome 实例?