2012-08-08 39 views
11

這裏是我的app.configConfigurationManager.GetSection返回null

<configuration> 
    <configSections> 
     <section name="procedureList" type="System.Configuration.NameValueSectionHandler, System, Version=4.0.30319, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> 
    </configSections> 

    <procedureList> 
    <add key="NAS.spBusObjGetLineProd" value="@area='Melt Shop';@endDt=?date?;@dayonly=1;@obj='Melt Shop Business Objective" /> 
    <add key="NAS.spBusObjGetLineProd" value="@area='Cold Mill';@endDt=?date?;@dayonly=1;@obj='Cold Mill Business Objective" /> 
    </procedureList> 
    <appSettings> 
    <add key="Connstr" value=""/> 
    <add key="Userid" value=""/> 
    <add key="Timeout" value=""/> 
    </appSettings> 

</configuration> 

但是,當我把它的代碼,我得到一個空回

public void samplemethod() 
{ 
    NameValueCollection nvc = ConfigurationManager.GetSection("procedureList") as NameValueCollection; 
    string[] keys = nvc.AllKeys; 
} 

我將不勝感激指出任何幫助我做了什麼錯

+0

您對此投下了錯誤信息。我會在下面發佈一個示例以及可用作參考的鏈接 – MethodMan 2012-08-08 21:06:34

+0

如果'as'投射操作失敗,則返回空值。但是,配置文件注意到節類型是'NameValueSectionHandler' - 您應該在調用GetSection()時轉換爲此類型。 – 2012-08-08 21:14:58

+0

即使當我將類型更改爲NameValueSectionHandler時,我仍然得到空回 – edepperson 2012-08-08 21:17:22

回答

5

Using section handlers to group settings in the configuration file

例如,你可以遵循一些如T他以下

private void ReadSettings() 
{ 
    NameValueCollection loc = 
    (NameValueCollection)ConfigurationSettings.GetConfig("procedureList"); 
} 

MSDN ConfigurationManager.GetConfig Method

+0

仍然變回原點 – edepperson 2012-08-08 21:12:55

+5

想通了。我在dll中配置了應用程序配置,而不是調用形式。因爲最終dll將被服務調用,所以我需要使用ConfigurationManager.OpenExeConfiguration來修復這個問題。感謝刺激。 – edepperson 2012-08-08 21:25:48

+1

沒有問題..這就是爲什麼我總是問開發人員他們是否調試他們的代碼..這是一個真棒工具調試器...很高興你得到它的工作 – MethodMan 2012-08-08 21:28:58

1

如果您測試您的類必須複製在您的測試項目中配置的app.config