我正在创建一个自定义配置部分,该部分将允许我管理我想从 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# 控制台测试应用程序中运行良好。 但是,当我将 VB 代码从我的 Web 应用程序粘贴到我的 VB.NET 控制台测试应用程序中时,它在那里也不起作用,因此它似乎是一个 C#/VB 问题。 我在这里做错了什么?
在配置文件中,类型声明的末尾有一个不需要的条目。 尝试更改此行:
<configSections>
<section name="IgnoredExceptionSection" type="ElmahExceptionHandling.IgnoredExceptionSection, WEB" />
</configSections>
对此
<configSections>
<section name="IgnoredExceptionSection" type="ElmahExceptionHandling.IgnoredExceptionSection" />
</configSections>