【问题标题】:Custom config section handler can't find handler自定义配置部分处理程序找不到处理程序
【发布时间】:2015-06-23 03:32:12
【问题描述】:

我正在使用我的自定义处理程序 AbraMain.MyConfigHandler 在 app.config 中创建 configSections

错误:


无法从程序集 'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 加载类型 'AbraMain.MyConfigHandler.ApplicationListCollection'。":"AbraMain.MyConfigHandler.ApplicationListCollection"

app.config

<configuration>
  <configSections>
     <section name="applicationList" type ="AbraMain.MyConfigHandler.ApplicationListCollection"/>
  </configSections>
  <applicationList>
     <add name="Abra Backup" index="0" iconIndex="0" desc="AbraBackup"/>
     <add name="Abra Backup" index="0" iconIndex="0" desc="AbraBackup"/>
  </applicationList>
</configuration>

MyConfigHandler.vb

命名空间 MyConfigHandler

'Desc : Individual Application configuration Class
'Handle Tag : App.config -> <applicationList> -> <add>
Public Class ApplInfoConfig
Inherits ConfigurationElement

<ConfigurationProperty("name", IsRequired:=True)> _
Public Property name() As String
  Get
    Return CStr(Me("name"))
  End Get
  Set(ByVal value As String)
    Me("name") = value
  End Set
End Property

<ConfigurationProperty("desc", DefaultValue:="", IsRequired:=False)> _
Public Property desc() As String
  Get
    Return CStr(Me("desc"))
  End Get
  Set(ByVal value As String)
    Me("desc") = value
  End Set
End Property

<ConfigurationProperty("subPath", DefaultValue:="", IsRequired:=False)> _
Public Property subPath() As String
  Get
    Return CStr(Me("subPath"))
  End Get
  Set(ByVal value As String)
    Me("subPath") = value
  End Set
End Property

<ConfigurationProperty("index", IsRequired:=True)> _
Public Property index() As Integer
  Get
    Return Me("index")
  End Get
  Set(ByVal value As Integer)
    Me("index") = value
  End Set
End Property

<ConfigurationProperty("iconIndex", DefaultValue:="0", IsRequired:=False)> _
Public Property iconIndex() As Integer
  Get
    Return Me("iconIndex")
  End Get
  Set(ByVal value As Integer)
    Me("iconIndex") = value
  End Set
End Property
End Class

'Desc : Collection of Individual Application configuration Class
'Handle Tag : App.config -> <applicationList>
Public Class ApplicationListCollection
Inherits ConfigurationElementCollection

Protected Overloads Overrides Function CreateNewElement() As System.Configuration.ConfigurationElement
  Return New ApplInfoConfig()
End Function

Protected Overrides Function GetElementKey(ByVal element As System.Configuration.ConfigurationElement) As Object
  Return CType(element, ApplInfoConfig).name()
End Function

End Class
End Namespace

【问题讨论】:

  • 请使用edit 编辑您的问题并格式化您的代码和错误消息,使其看起来不错。另外,请通过单击评论末尾显示的 (x) 删除仅包含代码转储的评论。最后请阅读how to ask a question
  • 感谢您改进您的问题。这让我可以阅读它并提出解决方案。

标签: vb.net configuration configsection


【解决方案1】:

似乎问题出在您为自定义部分指定处理程序的行上的app.config

<section name="applicationList" type ="AbraMain.MyConfigHandler.ApplicationListCollection"/>

自定义处理程序类型的类型声明还需要包含可以在其中找到它的程序集。否则,它将尝试在默认的System.Configuration 程序集中找到它。这也是您收到的错误消息。

因此,您也可以通过包含程序集的名称来解决它。我假设您的程序集名为AbraMain。然后您需要将该行更改为:

<section name="applicationList" type ="AbraMain.MyConfigHandler.ApplicationListCollection, AbraMain"/>

但是,您似乎还有第二个问题。对于配置部分,您需要有一个实现ConfigurationSection 的处理程序。

所以你需要添加一个新类来做到这一点:

Public Class ApplicationList  
Inherits ConfigurationSection

' You need to do the implementation. There are plenty of
' examples available on-line.

End Class

然后指向您的app.config 使用它:

<section name="applicationList" type ="AbraMain.MyConfigHandler.ApplicationList, AbraMain"/>

【讨论】:

    猜你喜欢
    • 2010-10-20
    • 1970-01-01
    • 1970-01-01
    • 2014-02-08
    • 2013-01-21
    • 2011-05-04
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多