2012-04-27 3 views
2

いくつかのサーバーで複数のテスターに​​よって実行される複数のc#セレンテストスイートがあります。複数のapp.configをよりグローバルに移動する

できるだけ設定を簡素化したいと思います。現在、それぞれ独自のapp.configを持つ複数のc#seleniumプロジェクトがあります。サーバーを変更したいときは、それぞれの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); 

を、我々は「Firefoxの」または「クロム」または「に設定することができ、「CompanyTestBrowser」という変数を持っていますIE "。

私はすべてのセレンテストを実行するpowershellスクリプトが必要なときにいつでも変数を簡単に変更できるため、環境変数が好きです。

しかし、私はこのapp.configからこれらのDB文字列を引き出すことはできません。どうすればそれらを少しでもグローバルにすることができます。&動的です。理想的には、1つの場所に設定するだけです。理想的には、他の環境変数や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");