2011-06-25 50 views
7

假設我們有Assembly1和Assembly2。跨引用程序集重複配置

Assembly2是Assembly1使用的C#類庫。

Web和服務引用已配置並存儲在Asembly2/app.Config中。

此外,EF連接字符串位於Assembly2/app.Config中。

當我在Assembly1中使用Assembly2時,不使用Assembly2配置文件。實際上,在這種情況下,只有Assembly1配置通過默認方式顯示。因此,我不得不將Assembly2配置內容複製到Assembly1配置中。

這對很多項目都適用。

還有別的辦法嗎?更好的方法?

重複配置數據似乎是錯誤的。

你有推薦或技術可行嗎?

謝謝。

回答

4

您需要將更改應用於入口點exe程序集的配置文件。類庫程序集(dll)配置文件從不使用。它們由Visual Studio製作,因此如果需要,您可以輕鬆將設置複製到exe配置文件。

波紋管是EXE程序集配置文件的示例,它具有來自類庫ClassLibrary1的設置和來自EXE程序集MainAssembly的設置。您可以看到兩個連接字符串都在一個connectionStrings設置中。但是,如果您需要設置其他設置,請在連接字符串旁邊添加額外的部分。

如果您已經在使用這種技術,這是正確的方法。這種技術是靈活的。例如,如果在一個框中有多個項目具有相同的連接字符串,則可以在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文件中機器上運行的所有應用程序的系統範圍設置,但請謹慎使用該方法,因爲這會影響機器上的所有應用程序。

相關問題