none
Message Contract and Data Contract RRS feed

  • Question

  • I have seen an example (below) which contains both Message Contract and DataContracts. Why is like that?

    MESSAGE CONTRACT.vbproj
    
    <WCF.MessageContract(WrapperName := "OrderInsertOrUpdate_Input", WrapperNamespace := "http://www.Test.ME")> _ 
    Public Partial Class OrderInsertOrUpdate_Input 
    
        Private m_Order As PROjects.DataContracts.Orders
        Private m_statusObject As String 
        
        <WCF.MessageBodyMember([Namespace] := "http://www.Test.ME", Name := "Order")> _ 
        Public Property Order() As PROjects.DataContracts.Orders 
            Get 
                Return m_Order
            End Get 
            Set(ByVal value As PROjects.DataContracts.Orders) 
                m_Order = value 
            End Set 
        End Property 
        
        <WCF.MessageBodyMember(Name := "StatusObject")> _ 
        Public Property StatusObject() As String 
            Get 
                Return m_statusObject 
            End Get 
            Set(ByVal value As String) 
                m_statusObject = value 
            End Set 
        End Property 
    End Class 
    
    
    DATA CONTRACT.vbproj
    
    Orders.vb
    
    <WcfSerialization.DataContract([Namespace] := "http://www.Test.ME", Name := "Orders")> _ 
    Public Partial Class Orders 
        Private m_ID As String 
        Private m_Name As String 
        
        <WcfSerialization.DataMember(Name := "ID", IsRequired := False, Order := 0)> _ 
        Public Property ID() As String 
            Get 
                Return m_ID 
            End Get 
            Set(ByVal value As String) 
                m_ID = value 
            End Set 
        End Property 
        
        <WcfSerialization.DataMember(Name := "Name", IsRequired := False, Order := 1)> _ 
        Public Property Name() As String 
            Get 
                Return m_Name 
            End Get 
            Set(ByVal value As String) 
                m_Name = value 
            End Set 
        End Property 
    End Class 
    
    
    Orders_O.vb
    
    <WcfSerialization.DataContract([Namespace] := "http://www.Test.ME", Name := "Orders_O")> _ 
    Public Partial Class Orders_O 
        Private m_returnID As String 
        
        <WcfSerialization.DataMember(Name := "ReturnID", IsRequired := False, Order := 0)> _ 
        Public Property ReturnID() As String 
            Get 
                Return m_returnID 
            End Get 
            Set(ByVal value As String) 
                m_returnID = value 
            End Set 
        End Property 
    End Class 

    Seshu
    Wednesday, June 10, 2009 2:13 AM

Answers

  • It really depends on the service design and requirements.
    The fundamental different between the two contracts is:
    A [DC] maps .NET types to the body of a message. 

    A Message Contract offers more control over the content of the seriaized data than a DC, because a message contract defines message headers and the message body. 
    Message contract also provides the means to express the security requirements of a member during serialization. 
    All message contracts must implement a public parameterless ctor.
    Amit Sharma
    Wednesday, June 10, 2009 3:21 AM
    Moderator
  • WCF was designed from ground up to interoperate with other systems (.net and non-.net systems)
    You can customize the SOAP message header or body based on your communication requirements.

    I will try to explain with a very simple data contract and a message contract.

    Consider the following message contract:

    [MessageContract]
    public class MyMC
    {
       [MessageHeader]
       public string Header = "Header1";

       [MessageBodyMember]
       public string Body = "Body1";
    }

    when this message contract is transmitted, the soap representation of the type will look like:

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
      <s:Header>
        <h:Header xmlns:h="http://tempuri.org/">Header1</h:Header>
        <To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addr
    essing/none">http://amitsharma:5000/Service</To>
        <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/
    addressing/none">http://tempuri.org/IService/EchoMC</Action>
      </s:Header>
      <s:Body>
        <MyMC xmlns="http://tempuri.org/">
          <Body>Body1</Body>
        </MyMC>
      </s:Body>
    </s:Envelope>

    Note that the [MessageHeader] field is actually present in the soap header section and   [MessageBodyMember] is present in the soap body section.  This header customization is useful when you communicate with systems that require such extensions.


    On the other hand, take a look at the following data contract

    [DataContract]
        public class MyDC
        {
            [DataMember]
            public string Member="Member1";
        }

    The soap message of this type will be:

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
      <s:Header>
        <To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addr
    essing/none">http://amitsharma:5000/Service</To>
        <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/
    addressing/none">http://tempuri.org/IService/EchoDC</Action>
      </s:Header>
      <s:Body>
        <EchoDC xmlns="http://tempuri.org/">
          <dc xmlns:a="
    http://schemas.datacontract.org/2004/07/MCvsDC" xmlns:i="http
    ://www.w3.org/2001/XMLSchema-instance">
            <a:Member>Member1</a:Member>
          </dc>
        </EchoDC>
      </s:Body>
    </s:Envelope>

    You can see that the whole type is actually serialized in the soap body section.  Not much soap envelope customization is possible with a data contract.


    Amit Sharma
    Wednesday, June 10, 2009 4:55 AM
    Moderator

All replies

  • It really depends on the service design and requirements.
    The fundamental different between the two contracts is:
    A [DC] maps .NET types to the body of a message. 

    A Message Contract offers more control over the content of the seriaized data than a DC, because a message contract defines message headers and the message body. 
    Message contract also provides the means to express the security requirements of a member during serialization. 
    All message contracts must implement a public parameterless ctor.
    Amit Sharma
    Wednesday, June 10, 2009 3:21 AM
    Moderator
  • Thanks Amit,

    That helped me to understand what it is. one more question, I was reading some thread in this forum and I found this, (http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/3e7a93d7-cf77-4698-b74f-1ee37e791e32)

    Message Contact use:

    Interoperability. For example: If you need to add custom SOAP headers needed to interop with other existing systems.

    Can you please explain above line? In what cases we use SOAP header?

    thanks
    Seshu
    Wednesday, June 10, 2009 3:45 AM
  • WCF was designed from ground up to interoperate with other systems (.net and non-.net systems)
    You can customize the SOAP message header or body based on your communication requirements.

    I will try to explain with a very simple data contract and a message contract.

    Consider the following message contract:

    [MessageContract]
    public class MyMC
    {
       [MessageHeader]
       public string Header = "Header1";

       [MessageBodyMember]
       public string Body = "Body1";
    }

    when this message contract is transmitted, the soap representation of the type will look like:

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
      <s:Header>
        <h:Header xmlns:h="http://tempuri.org/">Header1</h:Header>
        <To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addr
    essing/none">http://amitsharma:5000/Service</To>
        <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/
    addressing/none">http://tempuri.org/IService/EchoMC</Action>
      </s:Header>
      <s:Body>
        <MyMC xmlns="http://tempuri.org/">
          <Body>Body1</Body>
        </MyMC>
      </s:Body>
    </s:Envelope>

    Note that the [MessageHeader] field is actually present in the soap header section and   [MessageBodyMember] is present in the soap body section.  This header customization is useful when you communicate with systems that require such extensions.


    On the other hand, take a look at the following data contract

    [DataContract]
        public class MyDC
        {
            [DataMember]
            public string Member="Member1";
        }

    The soap message of this type will be:

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
      <s:Header>
        <To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addr
    essing/none">http://amitsharma:5000/Service</To>
        <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/
    addressing/none">http://tempuri.org/IService/EchoDC</Action>
      </s:Header>
      <s:Body>
        <EchoDC xmlns="http://tempuri.org/">
          <dc xmlns:a="
    http://schemas.datacontract.org/2004/07/MCvsDC" xmlns:i="http
    ://www.w3.org/2001/XMLSchema-instance">
            <a:Member>Member1</a:Member>
          </dc>
        </EchoDC>
      </s:Body>
    </s:Envelope>

    You can see that the whole type is actually serialized in the soap body section.  Not much soap envelope customization is possible with a data contract.


    Amit Sharma
    Wednesday, June 10, 2009 4:55 AM
    Moderator