2011-08-30 218 views
0

我有一个共享类库正在由一个asp.net Web应用程序和控制台应用程序使用。.NET控制台应用程序configSections

在我的web应用程序的web.config中,我在声明的configSections内部有一个sectionGroup,然后是匹配设置。

<configSections> 
    <sectionGroup name="StockLocator"> 
      <section name="AppSettings" type="StockLocator.ConfigSettings.AppConfig, StockLocator"/> 
    </sectionGroup> 
</configSections> 

<StockLocator> 
    <AppSettings> 
     <Settings...... /> 
    </AppSettings> 
</StockLocator> 

当我在Web应用程序中阅读这些设置时,一切正常。但是,当我将其添加到我的控制台应用程序的App.config时,它无法读取这些设置。基本上每当我试图从App.config文件读取任何东西,我只是得到一个错误"Object reference not set to an instance of an object."

不是很有帮助。

看起来好像这一节只是没有从app.config文件读取,这导致我认为你不能将configSections添加到app.config文件?还是有另一种方式来调试,以获得更好的错误信息?

我从configSections使用的代码

<Serializable()> _ 
Public Class AppConfig 
    Inherits ConfigurationSection 

    ''' <summary> 
    ''' Initialises and gets AppConfig SiteSettings 
    ''' </summary> 
    ''' <returns></returns> 
    ''' <remarks></remarks> 
    Public Shared Function getConfig() As AppConfig 
     Return CType(ConfigurationManager.GetSection("StockLocator/AppSettings"), AppConfig) 
    End Function 

    <ConfigurationProperty("Settings")> _ 
    Public Property Settings() As SettingsElement 
     Get 
      Return CType(Me("Settings"), SettingsElement) 
     End Get 
     Set(ByVal value As SettingsElement) 
      Me("Settings") = value 
     End Set 
    End Property 

    Public Class SettingsElement 
     Inherits ConfigurationElement 

     <ConfigurationProperty("SqlConnName")> _ 
     Public Property SqlConnName() As String 
      Get 
       Return CType(Me("SqlConnName"), String) 
      End Get 
      Set(ByVal value As String) 
       Me("SqlConnName") = value 
      End Set 
     End Property 

    End Class 

End Class 

堆栈跟踪阅读:

在StockLocator.Model.StockLocatorService.MatchStock(StockLocator_Store店)在C:\项目\ StockLocator \ StockLocator \ Model \ StockLocator.vb:line 421

+0

您能否发布完整的异常,请包括堆栈跟踪? –

+0

你还可以发布你正在使用的代码来尝试从app.config中读取吗? – PaulStock

+0

已更新。堆栈跟踪无用:P –

回答

2

如果您的App.config中有其他appSetting,则它们的相对顺序很重要。 configSections部分应该在appSettings之前。更多关于this msdn thread

相关问题