2011-06-25 4 views
7

Assembly1とAssembly2があるとします。参照アセンブリ間の構成の繰り返し

Assembly2はAssembly1で使用されるC#クラスライブラリです。

Webおよびサービス参照は、Asembly2/app.Configで設定および格納されます。

さらに、EF接続文字列はAssembly2/app.Configにあります。

Assembly1でAssembly2を使用すると、Assembly2構成ファイルは使用されません。実際、このシナリオでは、Assembly1構成だけが既定の手段でアクセス可能に見えます。

その結果、Assembly1設定にAssembly2設定内容をコピーする必要があります。

これは多くのプロジェクトで役に立ちました。

別の方法がありますか?より良い方法ですか?

設定データが繰り返し表示されているようです。

あなたには、推奨または技術がありますか?

ありがとうございます。

答えて

4

エントリポイントのexeアセンブリの設定ファイルに変更を加える必要があります。クラスライブラリアセンブリ(dll)設定ファイルは使用されません。 Visual Studioで作成されているので、必要に応じて設定をexe設定ファイルに簡単にコピーできます。

ベローは、クラスライブラリClassLibrary1の設定とexeアセンブリの設定MainAssemblyの両方を持つexeアセンブリの設定ファイルの例です。両方の接続文字列が1つのconnectionStrings設定に含まれていることがわかります。ただし、接続文字列の横に他の設定をする必要がある場合は、余分なセクションを追加する必要があります。

既にこの手法を使用している場合は、これは正しい方法です。この手法は柔軟性があります。たとえば、1つのボックスに同じ接続文字列を持つ複数のプロジェクトがある場合は、machine.configファイルで接続文字列を指定できます。また、必要に応じていくつかのプロジェクトの設定を上書きすることもできます。

<?xml version="1.0"?> 
<configuration> 
    <configSections> 
    <sectionGroup name="applicationSettings" 
        type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > 

     <!--This section declaratrion pasted here from dll conifg file --> 
     <section name="ClassLibrary1.Properties.Settings" 
       type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
       requirePermission="false" /> 

     <!--This section declaratrion was here in the first place --> 
     <section name="MainAssembly.Properties.Settings" 
       type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
       requirePermission="false" /> 
    </sectionGroup> 
    </configSections> 
    <connectionStrings> 

    <!--This connection string was here in the first place --> 
    <add name="MainAssembly.Properties.Settings.MainAssemblyConnectionString" 
     connectionString="MainConnectionStringValue" /> 

    <!--This connection string pasted here from dll config file --> 
    <add name="ClassLibrary1.Properties.Settings.LibraryConnectionString" 
     connectionString="LibraryConnectionStringValue" 
     providerName="" /> 
    </connectionStrings> 
    <applicationSettings> 

    <!--This settings section pasted here from dll config file --> 
    <ClassLibrary1.Properties.Settings> 
     <setting name="LibrarySetting" 
       serializeAs="String"> 
     <value>LibrarySettingValue</value> 
     </setting> 
    </ClassLibrary1.Properties.Settings> 

    <!--This strings section was here in the first place --> 
    <MainAssembly.Properties.Settings> 
     <setting name="MainAssemblySetting" 
       serializeAs="String"> 
     <value>MainSettingValue</value> 
     </setting> 
    </MainAssembly.Properties.Settings> 
    </applicationSettings> 
</configuration> 
2

DLL(または別の参照アセンブリ)は、独自のapp.configを持っているのではなく、呼び出し元によって設定されたすべてのものを持っています。だから、すべてのexeのapp.configに行く必要があります。

たとえば、データベースへの接続文字列を必要とする共有データアクセスライブラリを考えてみましょう。ライブラリは、異なる接続要件を持つさまざまなアプリケーションから使用することが可能でなければなりません。共有ライブラリに厳密に結び付けられた接続文字列を使用できない場合は、ライブラリを使用してクライアントで実行する必要があります。

machine.configファイル内のマシン上で実行されているすべてのアプリケーションに影響を与えるシステム全体の設定を行うことはできますが、マシン上のすべてのアプリケーションに影響するので注意して使用してください。

関連する問題