2012-04-27 23 views
2

我們有幾個c#selenium測試套件,它們由多個測試人員在幾臺服務器上運行。將多個app.config移到更全局的東西

如果可能,我想簡化設置。目前我們有多個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」,它可以被設置爲「火狐」或「」或「變量IE「。

我喜歡環境變量,因爲運行我們所有硒測試的powershell腳本可以在需要時輕鬆更改變量。

但是,我似乎無法從這個app.config中拉出這些數據庫字符串。我如何將它們移動到可以更具全局性的東西&動態。理想情況下,我只需要設置一個地方?理想情況下,我可以將它們移動到環境變量中,比如另一個或者位於c#項目之外的配置文件。

謝謝!

回答

1

我的建議是,必須保持在一個networklocation前一個XML文件的連接字符串。 \計算機名\ 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> 

在你的應用程序啓動程序,讀取\計算機名\ 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); 

希望這有助於。

0

您可以向app.config添加一個密鑰,該密鑰包含一個具有其中所有常用設置的XML文件的路徑,然後編寫一個新的ConfigurationManager類以首先嚐試從app.config中提取一個值,if沒有找到,打開XML文件,並在那裏

像這樣的東西尋找它:

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

我使用上面接受的解決方案。這是我的確切代碼(與上面只有稍微不同)。

這個解決方案有效地保留了我目前的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");