VB.NET/ASP.NET 4.0自定义配置部分:“创建配置节处理程序时出错”

时间:2012-05-02 12:52:26

标签: vb.net .net-4.0 web-config app-config asp.net-4.0

我正在创建一个自定义配置部分,它允许我从我的VB.NET/ASP.NET应用程序中管理我想忽略的ELMAH异常。这是我的代码。如果有人能够接受诊断问题的挑战,我可以轻松粘贴到空白代码文件中。

Imports System.Configuration
Namespace ElmahExceptionHandling
    Public Class IgnoredExceptionSection : Inherits ConfigurationSection
        <ConfigurationProperty("IgnoredExceptions")>
        ReadOnly Property IgnoredExceptions As IgnoredExceptions
            Get
                Return TryCast(Me("IgnoredExceptions"), IgnoredExceptions)
            End Get
        End Property
        Shared Function GetConfig() As IgnoredExceptionSection

            Return TryCast(System.Configuration.ConfigurationManager.GetSection("IgnoredExceptionSection"), IgnoredExceptionSection)

        End Function
    End Class

    <ConfigurationCollection(GetType(IgnoredException))>
    Public Class IgnoredExceptions : Inherits ConfigurationElementCollection
        Protected Overloads Overrides Function CreateNewElement() As System.Configuration.ConfigurationElement

            Return New IgnoredException

        End Function
        Protected Overrides Function GetElementKey(element As System.Configuration.ConfigurationElement) As Object

            Return TryCast(element, IgnoredException).Message

        End Function
    End Class

    Public Class IgnoredException : Inherits ConfigurationElement
        <ConfigurationProperty("Message")>
        ReadOnly Property Message As String
            Get
                Return Me("Message")
            End Get
        End Property
    End Class
End Namespace

这是配置:

<configSections>
    <section name="IgnoredExceptionSection" type="ElmahExceptionHandling.IgnoredExceptionSection, WEB" />
</configSections>
<IgnoredExceptionSection>
    <IgnoredExceptions>
        <add Message="test exception" />
    </IgnoredExceptions>
</IgnoredExceptionSection>

执行此代码时:

Dim section As ElmahExceptionHandling.IgnoredExceptionSection = ConfigurationManager.GetSection("IgnoredExceptionSection")

我收到错误An error occurred creating the configuration section handler for IgnoredExceptionSection: Could not load file or assembly 'WEB' or one of its dependencies.

令我难以置信的是,在使用Web实用程序从VB.NET转换代码后,这一切在我的C#控制台测试应用程序中运行良好。但是,当我将Web代码中的VB代码粘贴到我的VB.NET控制台测试应用程序中时,它也无法在那里工作,因此它似乎是一个C#/ VB问题。我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

在您的配置文件中,类型声明的末尾有一个不需要的条目。尝试更改此行:

<configSections>
    <section name="IgnoredExceptionSection"  type="ElmahExceptionHandling.IgnoredExceptionSection, WEB" />
</configSections>

到此

<configSections>
    <section name="IgnoredExceptionSection" type="ElmahExceptionHandling.IgnoredExceptionSection" />
</configSections>