开发者

.NET Console Application configSections

开发者 https://www.devze.com 2023-03-31 14:02 出处:网络
I have a shared class library which is being used by an asp.net web application and a console application.

I have a shared class library which is being used by an asp.net web application and a console application.

In the web.config of my web app, I have a sectionGroup within the configSections declared, and then the matching settings.

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

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

Everything works when I am reading these settings in the web application. Howe开发者_StackOverflow中文版ver, when I add this to the App.config of my console application, it is not able to read these settings. Basically whenever I am trying to read anything from the App.config file I just get an error "Object reference not set to an instance of an object."

Not very helpful.

It seems as though this section is just not being read from the app.config file, which leads me to think that you cannot add the configSections to the app.config file? Or is there another way to debug this to get a better error message?

I am reading from the configSections using the code

<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

Stack Trace:

at StockLocator.Model.StockLocatorService.MatchStock(StockLocator_Store store) in C:\projects\StockLocator\StockLocator\Model\StockLocator.vb:line 421


If you have other appSettings in your App.config their relative order matters. The configSections section should come before the appSettings. More on this msdn thread

0

精彩评论

暂无评论...
验证码 换一张
取 消