【问题标题】:Multiple Elements in Custom ConfigurationSection自定义配置部分中的多个元素
【发布时间】:2014-04-29 00:43:37
【问题描述】:

更新:自定义配置太难了! >.

我一直在关注下面引用的文章中的 MSDN 示例,但我在将这个概念应用于我的案例时遇到了麻烦。我的项目是 VS2010 中的 VB.NET 4.0 Web 应用程序,但 C# 中的答案也可以(只要解决方案在 VB 中有效)。 :) 以下是相关代码:

致电

Dim conf As ApiSection = CType(ConfigurationManager.GetSection("remoteServiceApi/site"), ApiSection)

异常 (ConfigurationErrorsException)

The element <site> may only appear once in this section.

请注意以下相关代码...

架构

<xs:element name="ApiSection">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="site" id="environment" nillable="false" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:attribute name="environment" use="required">
                        <xs:simpleType>
                            <xs:restriction base="xs:string">
                                <xs:enumeration value="Production"></xs:enumeration>
                                <xs:enumeration value="Sandbox"></xs:enumeration>
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:attribute>
                    <xs:attribute name="APIKey" type="xs:string" use="required" />
                    <xs:attribute name="APIUsername" type="xs:string" use="required" />
                    <xs:attribute name="APIPassword" type="xs:string" use="required" />
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

尽管我在 web.config 文件的架构集中添加并激活了上述内容,但它似乎并没有真正起作用。 :(这可能是问题吗?

Web.Config

<configSections>
    <sectionGroup name="remoteServiceApi">
        <section name="site" type="RemoteServiceApi.ApiSection" />
    </sectionGroup>
</configSections>
<remoteServiceApi environment="Sandbox">
    <site environment="Sandbox"
        APIKey="reallyLongHashKeyDev"
        APIUsername="samplejoe"
        APIPassword="joespass" />
    <site environment="Production"
        APIKey="reallyLongHashKeyProd"
        APIUsername="samplejoe"
        APIPassword="joespass" />
</remoteServiceApi>

类文件

Imports System.Configuration

Namespace RemoteServiceApi
    Public Class ApiSection
        Inherits ConfigurationSection

        <ConfigurationProperty("site", IsRequired:=True)> _
        Public Property site() As SiteElement
            Get
                Return CType(Me("site"), SiteElement)
            End Get
            Set(value As SiteElement)
                Me("site") = value
            End Set
        End Property
    End Class

    Public Class SiteElement
        Inherits ConfigurationElement

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

        <ConfigurationProperty("APIKey", IsRequired:=True)> _
        Public ReadOnly Property APIKey() As String
            Get
                Return CStr(Me("APIKey"))
            End Get
        End Property

        <ConfigurationProperty("APIUsername", IsRequired:=True)> _
        Public ReadOnly Property APIUsername() As String
            Get
                Return CStr(Me("APIUsername"))
            End Get
        End Property

        <ConfigurationProperty("APIPassword", IsRequired:=True)> _
        Public ReadOnly Property APIPassword() As String
            Get
                Return CStr(Me("APIPassword"))
            End Get
        End Property
    End Class

    Public Enum Environment
        Production
        Sandbox
    End Enum
End Namespace

我用作指导的参考资料:

【问题讨论】:

    标签: c# vb.net visual-studio-2010 web-config configurationsection


    【解决方案1】:

    您需要将Site 元素设为一个集合(基于您发布的Web.config)。尝试这样的事情(在 C# 中,抱歉):

    [ConfigurationCollection(typeof(SiteElement)[
    public class SiteElementCollection : ConfigurationElementCollection
    
        public SiteElement this[string name]
        {
            get
            {
                return (SiteElement)base.BaseGet(name);
            }
        }
    
        public SiteElement this[int index]
        {
            get
            {
                return (SiteElement)base.BaseGet(index);
            }
        }
    
        public override ConfigurationElement CreateNewElement()
        {
            return new SiteElement();
        }
    
        public override object GetElementKey(ConfigurationElement element)
        {
            return ((SiteElement)element).AddressKey;
        }
    

    此类将包装SiteElement,允许您拥有该元素的多个实例。

    我不能 100% 确定下一部分,因为我从未在该部分的下方直接使用 ConfigurationElementCollection,但您需要引用该集合。

    作为一个例子(它不太适合您尝试做的事情,但希望适合),假设您在部分组下有一个部分名称 Sites。你可以这样做:

    public SiteElementCollection Sites
    {
        get
        {
            return (SiteElementCollection)base["site"];
        }
    }
    

    我希望这至少能给你一个可以遵循的方向。这里的关键是当您需要给定元素的多个实例时使用ConfigurationElementCollection

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-13
      • 2020-08-30
      • 2014-01-19
      • 1970-01-01
      • 2010-10-25
      • 2013-10-31
      相关资源
      最近更新 更多