wcfmessagecontractmessagecontract

How to define message contract to get this schema?


How can i define the Message contract to get this XML SOAP format?

Schema:

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

I created the class file as mention below but it gives the response different

<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

My response is like this:

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

How to achive the proper message using Message contract?


Solution

  • VB.net is not installed in my VS. C# code below for your reference.

    //class definition

    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");
    }