【发布时间】:2011-08-30 13:17:28
【问题描述】:
我有一个共享类库,正被一个 asp.net 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
堆栈跟踪:
在 C:\projects\StockLocator\StockLocator\Model\StockLocator.vb:line 421 中的 StockLocator.Model.StockLocatorService.MatchStock(StockLocator_Store 商店)
【问题讨论】:
-
你能发布完整的异常,请包括堆栈跟踪吗?
-
您能否发布您用来尝试从 app.config 读取的代码?
-
已更新。堆栈跟踪没用:P
-
您的 App.config 中有其他 appSettings 吗?如果是这样watch the order
-
@paolo 谢谢你的工作!没想到这很重要。目前在它之前有标准的
<system.diagnostics>部分,我只是在它下面添加了<configSections>。您能否将您的评论添加为答案,以便我可以标记为答案。
标签: .net vb.net console-application configsection