【问题标题】:Need maxOccurs="unbounded" in WCF Data Contract Needs在 WCF 数据合同需求中需要 maxOccurs="unbounded"
【发布时间】:2012-06-14 21:23:26
【问题描述】:

我试图在另一个环境中模拟现有的 Web 服务以进行测试,但遇到了障碍。

我正在尝试在 WCF 中创建此模拟服务

我正在模拟的服务(不是在 WCF 中开发的)有一个具有这种类型定义的实体:

<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="dog" nillable="true" type="xs:string"/>

</xs:sequence>

注意 maxOccurs="unbounded" 消息本身看起来像这样:

<dog>1</dog>
<dog>2</dog>

但我不知道如何定义我的 DataContract 来处理这个问题。我假设只使用如下数组类型:

    [DataContract]
    public class P56040Input
    {
        [DataMember]
        public string[] dog { get; set; }


    }

会对应,但它使用新类型(arrayofstring)做了一些意想不到的事情:

<xs:sequence>
<xs:element xmlns:q1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" minOccurs="0" name="dog" nillable="true" type="q1:ArrayOfstring"/>
</xs:sequence>

这甚至可以在 WCF 中模拟吗?

【问题讨论】:

    标签: .net xml wcf xsd wsdl


    【解决方案1】:

    如果您转到 ArrayOfString 定义,您会看到它有 maxOccurs=“unbounded”。所以你在这里看到的只是 wcf 生成的包装器类型。为了不生成这种类型,您需要使用 xml 序列化程序而不是数据协定。定义如下所示:

    [System.Xml.Serialization.XmlElementAttribute("dog")]
    public string[] dog  {get; set;}
    

    我怎么知道的?您需要做的就是向 wsdl“添加 Web 引用”(以便生成客户端代理)。然后查看相关的代理部分以查看类型顶部的属性。您可以对默认使用数据协定的“添加服务参考”执行相同操作,并查看它生成了您使用的内容,这与您需要的内容不完全相同。

    【讨论】:

    • 非常有帮助。谢谢。
    猜你喜欢
    • 2010-12-23
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    相关资源
    最近更新 更多