2011-04-21 23 views
3

私はpage_loadイベント中に非同期に呼び出すいくつかのメソッドを持つWCFデータサービスを持っています。configSource絶対パスの回避策

必要に応じて、スクリプトまたはコンソールアプリケーションからこれらのメソッドを呼び出すソリューションが必要です。

私は、WCF .dllライブラリを参照するコンソールアプリケーションを作成しました。 WCFサービスのweb.configにある設定変数のいくつかをコンソールアプリケーションのapp.configにコピーする必要があります。

私はapp.configを自動的にweb.configをミラーリングするか、何らかの方法でWCFサービスのweb.configでコンソールアプリケーションを参照します。

私のコンソールアプリケーションとwcfプロジェクトは、同じソリューション内で互いに隣接しているため、 'configSource'属性は機能しません。親ディレクトリまたは絶対パスは使用できません。

これを回避する方法はありますか?

+0

何のconfig変数を複製する必要がありますか? – BrandonZeider

+0

configSections要素で定義したカスタム設定セクションと接続文字列。ちょうどそれらの2つ。 –

答えて

3

あなたのカスタム設定セクションは何か分かりませんが、このセクションでは設定セクション(この例ではsystem.serviceModel/client)、アプリケーション設定、接続文字列をプログラムで取得する方法を説明します。あなたのニーズに合わせて変更することができるはずです。

public class ConfigManager 
{ 
    private static readonly ClientSection _clientSection = null; 
    private static readonly AppSettingsSection _appSettingSection = null; 
    private static readonly ConnectionStringsSection _connectionStringSection = null; 
    private const string CONFIG_PATH = @"..\..\..\The rest of your path\web.config"; 

    static ConfigManager() 
    { 
     ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap() 
     { 
      ExeConfigFilename = CONFIG_PATH 
     }; 

     Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None); 

     _clientSection = config.GetSection("system.serviceModel/client") as ClientSection; 
     _appSettingSection = config.AppSettings; 
     _connectionStringSection = config.ConnectionStrings; 
    } 

    public string GetClientEndpointConfigurationName(Type t) 
    { 
     string contractName = t.FullName; 
     string name = null; 

     foreach (ChannelEndpointElement c in _clientSection.Endpoints) 
     { 
      if (string.Compare(c.Contract, contractName, true) == 0) 
      { 
       name = c.Name; 
       break; 
      } 
     } 

     return name; 
    } 

    public string GetAppSetting(string key) 
    { 
     return _appSettingSection.Settings[key].Value; 
    } 

    public string GetConnectionString(string name) 
    { 
     return _connectionStringSection.ConnectionStrings[name].ConnectionString; 
    } 
} 

使用法:

ConfigManager mgr = new ConfigManager(); 

string setting = mgr.GetAppSetting("AppSettingKey"); 
string connectionString = mgr.GetConnectionString("ConnectionStringName"); 
string endpointConfigName = mgr.GetClientEndpointConfigurationName(typeof(IServiceContract)); 
+0

病気を試してみてください。これは本質的に両方のアプリケーションに絶対パスを介して同じ設定ファイルを与えるために使用できますか? –

+0

私はそれを試みたことはありませんが、どうしてそうは見えませんか?興味深いアイデア! – BrandonZeider