如何定义消息契约以获取此架构?

时间:2014-04-04 06:18:58

标签: wcf message contract messagecontract

如何定义Message合约以获取此XML SOAP格式?

架构:

    <List>
      <Exclusions>
        <ExclusionID>123</ExclusionID>
        <ExclusionID>656</ExclusionID>
      </Exclusions>
      </List>

我创建了类文件,如下所述,但它给出了不同的响应

<DataContract> _
Public Class List


    ''' <remarks/>
    Private _exclusions As Exclusions

    <DataMember()> _
    Public Property Exclusions() As Exclusions
        Get
            Return _exclusions
        End Get
        Set(ByVal value As Exclusions)
            _exclusions = value
        End Set
    End Property

End Class

<DataContract>
Public Class Exclusions

    <DataMember>
    Public ExclusionID As ExclusionID()

End Class

<MessageContract>
Public Class ExclusionID

    <MessageBodyMember(Name:="")>
   Public Value As String

End Class

我的回答是这样的:

    <List >
      <Exclusions >
        <ExclusionID>
          <ExclusionID>
            <Value>123</Value>
          </ExclusionID>
          <ExclusionID>
            <Value>145</Value>
          </ExclusionID>
        </ExclusionID>
      </Exclusions>
    </List>

如何使用消息合约获取正确的消息?

1 个答案:

答案 0 :(得分:0)

我的VS中没有安装VB.net。下面的C#代码供您参考。

//类定义

public class List {

    [MessageBodyMember]
    public Exclusions Exclusions { get; set; }
}

[CollectionDataContract(Name="Exclusions", ItemName="Exclusion")]
public class Exclusions : List<String> {

}

//help method
static void WriteMessage(Message message, String filePath) {
    using (var writer = new XmlTextWriter(filePath, Encoding.UTF8)) {
        message.WriteMessage(writer);
    }
}

//code
var myList = new List();
myList.Exclusions = new Exclusions();
myList.Exclusions.Add("123");
myList.Exclusions.Add("656");

using (var message = Message.CreateMessage(MessageVersion.Soap12WSAddressing10,
    "http://127.0.0.1:3333/someaction", myList)) {

    WriteMessage(message, "a.xml");
}