Пользовательский обработчик раздела конфигурации не может найти обработчик

Я создаю configSections в app.config с моим пользовательским обработчиком AbraMain.MyConfigHandler

Ошибка:


Не удалось загрузить тип 'AbraMain.MyConfigHandler.ApplicationListCollection' из сборки 'System.Configuration, Version=4.0.0.0, Culture= нейтральный, PublicKeyToken = b03f5f7f11d50a3a'. ":" 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

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"/>
Другие вопросы по тегам