实现自定义配置节处理程序

2023-12-11

从各种来源(包括 stackOverlflow)收集的信息,但是当我开始使用它时,我收到以下错误消息

“配置属性‘deviceconfig’可能不是从 ConfigurationSection 派生的。”

我现在已经在这个问题上挣扎了一整天,而且还没有找到解决方案,所以在这件事上的任何帮助将不胜感激。我是实现自定义配置部分的新手,所以请温柔地对待我:-)

app.config 文件如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="deviceconfig"  type="CNIMonitor.Core.CustomConfig.DeviceConfig,Core"/>
  </configSections>
<system.diagnostics>
    <sources>
        <!-- This section defines the logging configuration for My.Application.Log -->
        <source name="DefaultSource" switchName="DefaultSwitch">
            <listeners>
                <add name="FileLog"/>
                <!-- Uncomment the below section to write to the Application Event Log -->
                <!--<add name="EventLog"/>-->
            </listeners>
        </source>
    </sources>
    <switches>
        <add name="DefaultSwitch" value="Information" />
    </switches>
    <sharedListeners>
        <add name="FileLog"
             type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" 
             initializeData="FileLogWriter"/>
        <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
        <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
    </sharedListeners>
</system.diagnostics>
<deviceconfig>
  <devices>          
      <device deviceid = "1" 
              name = "localhost" 
              ipaddress="127.0.0.1" 
              port="10000"
              status="not listening"
              message="no message"
      />
      <device deviceid ="2" 
              name ="2nd localhost" 
              ipaddress="127.0.0.1" 
              port="20000" 
              status="not listening"
              message="no message"
      />
  </devices>  
</deviceconfig>

自定义部分处理程序代码如下:

Imports System.Configuration

命名空间 CNIMonitor.Core.CustomConfig

Public Class DeviceConfig
    Inherits ConfigurationSection
    <ConfigurationProperty("deviceconfig")> _
    Public ReadOnly Property DeviceConfig() As DeviceConfig
        Get
            Return CType(MyBase.Item("deviceconfig"), DeviceConfig)
        End Get
    End Property
End Class

<ConfigurationCollectionAttribute(GetType(Device))> _
Public Class Devices
    Inherits ConfigurationElementCollection
    Protected Overrides Function CreateNewElement() As ConfigurationElement
        Return New Device
    End Function
    Protected Overrides Function GetElementKey _
    (ByVal element As ConfigurationElement) As Object
        Return CType(element, Device).DeviceID
    End Function
    Public Sub Add(ByVal element As Device)
        Me.BaseAdd(element)
    End Sub
End Class
Public Class Device
    Inherits ConfigurationElement
    <ConfigurationProperty("deviceid", DefaultValue:="", IsKey:=True, IsRequired:=True)> _
    Public Property DeviceID() As String
        Get
            Return CType(MyBase.Item("deviceid"), String)
        End Get
        Set(ByVal value As String)
            MyBase.Item("deviceid") = value
        End Set
    End Property
    <ConfigurationProperty("hostname", DefaultValue:="", IsKey:=True, IsRequired:=False)> _
    Public Property HostName() As String
        Get
            Return CType(MyBase.Item("RegisteredDate"), String)
        End Get
        Set(ByVal value As String)
            MyBase.Item("RegisteredDate") = value
        End Set
    End Property
    <ConfigurationProperty("ipaddress", DefaultValue:="", IsKey:=True, IsRequired:=False)> _
    Public Property IpAddress() As String
        Get
            Return CType(MyBase.Item("ipaddress"), String)
        End Get
        Set(ByVal value As String)
            MyBase.Item("ipaddress") = value
        End Set
    End Property
    <ConfigurationProperty("port", DefaultValue:="", IsKey:=True, IsRequired:=False)> _
    Public Property Port() As String
        Get
            Return CType(MyBase.Item("port"), String)
        End Get
        Set(ByVal value As String)
            MyBase.Item("port") = value
        End Set
    End Property
    <ConfigurationProperty("status", DefaultValue:="", IsKey:=False, IsRequired:=False)> _
    Public Property Status() As String
        Get
            Return CType(MyBase.Item("status"), String)
        End Get
        Set(ByVal value As String)
            MyBase.Item("status") = value
        End Set
    End Property
    <ConfigurationProperty("message", DefaultValue:="", IsKey:=False, IsRequired:=False)> _
    Public Property Message() As String
        Get
            Return CType(MyBase.Item("message"), String)
        End Get
        Set(ByVal value As String)
            MyBase.Item("message") = value
        End Set
    End Property
End Class

End Namespace

顶层不需要 DeviceConfig 属性。你什么do需要做的是为您添加一个属性ConfigurationElementCollection设备。我也会重命名你的Devices类一些不太模糊的东西,比如说,DeviceElementCollection。尝试这个:

Public Class DeviceConfig    
    Inherits ConfigurationSection    

    <ConfigurationProperty("Devices")> _    
    Public ReadOnly Property Devices() As DeviceElementCollection    
        Get    
            Return CType(Me("Devices"), DeviceElementCollection)    
        End Get    
    End Property    
End Class    

然后,在你的定义中DeviceElementCollection,如果不添加以下内容,可能会出现问题:

Public Overrides ReadOnly Property CollectionType() As System.Configuration.ConfigurationElementCollectionType
    Get
        Return ConfigurationElementCollectionType.BasicMap
    End Get
End Property

Protected Overrides ReadOnly Property ElementName() As String
    Get
        Return "Device"
    End Get
End Property

我为类似的问题写了一个很长的答案(抱歉,用 C#)here.

更新 - 如何在代码中使用

Dim deviceConfiguration as DeviceConfig = ConfigurationManager.GetSection("deviceconfigs")
For Each device As Device in deviceConfiguration.Devices
    '...whatever you need to do
Next
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

实现自定义配置节处理程序 的相关文章

  • Access / Word 2010 VBA 邮件合并尝试打开 [文件夹名称].mdb 而不是 ACCDB 源

    我们正在尝试从 Access 中自动执行邮件合并过程 单击按钮后 VBA 将运行指定当前数据库 accdb 作为数据源并运行 SQL 具体代码如下 Set up Word Dim objWord As Object Set objWord
  • 在哪里存储 Java 的 .properties 文件?

    The Java教程 http download oracle com javase tutorial essential environment properties htmlon using Properties 讨论如何使用 Prop
  • 没有 OAuth 的 Spring Security JWT

    最近我开始学习如何使用oauth 2 0 jwt配置spring boot 我有一个问题 是否可以使用spring boot security jwt避免oauth 2 0 是的 可以使用JWT无需使用标准化的功能OAuth 2 0 flo
  • 迭代 pandas 数据框的最快方法?

    如何运行数据框并仅返回满足特定条件的行 必须在之前的行和列上测试此条件 例如 1 2 3 4 1 1 1999 4 2 4 5 1 2 1999 5 2 3 3 1 3 1999 5 2 3 8 1 4 1999 6 4 2 6 1 5 1
  • 如何为 Windows toast 注册协议?

    如何注册 Windows toast 协议 样本中来自https blogs msdn microsoft com tiles and toasts 2015 07 02 adaptive and interactive toast not
  • 使用 crypt() 加密

    我目前正在做一个非常安全的登录系统 但我是 crypt 函数的新手 需要一些快速帮助 我在注册过程中使用 crypt 加密密码字符串并将其保存到数据库中 但是 我如何在登录过程中解密密钥 或者我应该怎么做 或者是否可以对提交的密码字符串进行
  • 带重定向标准流的 C# + telnet 进程立即退出

    我正在尝试用 C 做一个 脚本化 telnet 项目 有点类似于Tcl期望 http expect nist gov 我需要为其启动 telnet 进程并重定向 和处理 其 stdin stdout 流 问题是 生成的 telnet 进程在
  • 您可以使用关键字参数而不提供默认值吗?

    我习惯于在 Python 中使用这样的函数 方法定义 def my function arg1 None arg2 default do stuff here 如果我不供应arg1 or arg2 那么默认值None or default
  • Android ScrollView fillViewport 不工作

    我有一个简单的布局 名称位于顶部 按钮位于屏幕底部 或者超出该按钮 以防我添加更多项目 所以我使用带有 LinearLayout 的 ScrollView 如下所示
  • Googletest:如何异步运行测试?

    考虑到一个包含数千个测试的大型项目 其中一些测试需要几分钟才能完成 如果按顺序执行 整套测试需要一个多小时才能完成 通过并行执行测试可以减少测试时间 据我所知 没有办法直接从 googletest mock 做到这一点 就像 async选项
  • NGinx $proxy_add_x_forwarded_for 和 real_ip_header

    我在 NGinx 下有一个 web 应用程序和另一个前端负载均衡器 如下所示 x x x x IP 地址 客户端 a a a a gt LB b b b b gt NGX c c c c gt WEBAPP d d d d 这是我的 NGi
  • Typescript 函数接口重载

    我有以下代码 interface MySecondInterface a type A interface MyInterface val1 string val2 string MySecondInterface a
  • 实例化 Microsoft.Office.Interop.Excel.Application 对象时出现错误:800700c1

    实例化 Microsoft Office Interop Excel Application 以从 winforms 应用程序生成 Excel 时 出现以下错误 这之前是有效的 但突然间它停止工作了 尽管代码和 Excel 版本没有变化 我
  • 使用 Crypto++ 获取 ECDSA 签名

    我必须使用 Crypto 在变量中获取 ECDSA 签名 我在启动 SignMessage 后尝试获取它 但签名为空 我怎样才能得到它 你看过 Crypto wiki 吗 上面有很多东西椭圆曲线数字签名算法 http www cryptop
  • 从 Azure 应用服务连接到 MongoDB Atlas 集群

    我在 Azure 上有一个 Web 应用程序 它连接到 Atlas cloud mongodb com 上托管的 MongoDB 集群 我想使用 Atlas 这样我就不必关心 MongoDb 配置 问题是我的集群连接超时 我必须在我的 mo
  • 匿名结构体作为返回类型

    下面的代码编译得很好VC 19 00 23506 http rextester com GMUP11493 标志 Wall WX Za 与VC 19 10 25109 0 标志 Wall WX Za permissive 这可以在以下位置检
  • 保存符号方程以供以后使用?

    From here http www mathworks com help releases R2011a toolbox symbolic brvfu8o 1 html brvfxem 1 我正在尝试求解这样的符号方程组 syms x y
  • 当ScrollView滚动到底部时加载更多数据

    我有一个带有动态加载内容的滚动视图 有时可能会有很多内容 所以我想在用户滚动到底部时加载更多内容 我搜索了合适的方法 发现了两种 onScrollChanged and getScrollY 但我不知道如何将它用于我的目的 请给我一些建议
  • 如果产品重量超过1000克,如何以公斤为单位显示

    在 Storefront 主题中 我使用下面的代码将格式化重量从 1000g 更改为 1kg add action woocommerce after shop loop item title show weight 10 function
  • CUDA 中指令重放的其他原因

    这是我从 nvprof CUDA 5 5 获得的输出 Invocations Metric Name Metric Description Min Max Avg Device Tesla K40c 0 Kernel MyKernel do

随机推荐