none
MessageContract vs DataContract

    Question

  • I need know what is the difference between DataContract and MessageContract. and when I need to use messageContract or DataContract.

     

    Can you help please?

    Thanks,

    Wednesday, July 11, 2007 9:00 PM

Answers

All replies

  • DataContracts are used to descibe the data types used by a service.  This enables the types to be described in metadata to enable clients to interop with the service.  DataContracts can be used to describe either parameters or return values.  DataContracts are unnecessary if the service only uses simple types.

     

    MessageContracts are used to explicitly describe the soap message format.  It can be used to declare which headers various message elements go.

     

    Whether you need to use either of these depends on your scenario.  Could you briefly describe your scenario?  Maybe you don't need either of these, and can get by simply with a ServiceContract and OperationContract.

     

    -James

    Wednesday, July 11, 2007 9:11 PM
    Moderator
  • I have this scenario

     

    I have the folloing two classes

     

    class Response

    {

        byte[] buffer;

        int  offset;

    }

     

    class Request

    {

        byte[] buffer;

       MyFormat Format;

    }

     

    and I have an operatin contract as below

    [OperationContract]

    Response Convert(Request);

     

    so in this case these two classes shoud be DataContracts or MessageContracts, if I can use both (Message and Data) then what is the diff?

     

    Thanks

     

     

    Wednesday, July 11, 2007 9:27 PM
  • I think, in this scenario, you want to use DataContract, since these classes describe the types being used, and not the format of the message being sent.

     

    -James

    Wednesday, July 11, 2007 9:59 PM
    Moderator
  • well can you gibe me a scenario for MessageContract nad what I get if i fromat a meassage that i need to send what is the advantage for MessageContract. and if i used it in my scenario what differs
    Wednesday, July 11, 2007 10:15 PM
  • Info about using MessageContract, including examples:

    http://msdn2.microsoft.com/en-us/library/ms730255.aspx

     

    Info about using DataContract, including examples:

    http://msdn2.microsoft.com/en-us/library/ms733127.aspx

     

    -James

    Wednesday, July 11, 2007 10:41 PM
    Moderator
  • Why, when using streaming, must the MessageContract have only one MessageBodyMember which is a stream: "In order to use Streams with the MessageContract programming model, the type UploadStreamMessage must have a single member with MessageBodyMember attribute and the member type must be Stream."(Error thrown if more than one stream is passed)

     

    And why does passing a stream in the DataContract like this :

     

    [DataContract]

    public class UploadStreamMessage

    {

    [DataMember]

    public Int64 streamLength;

    [DataMember]

    public string streamHash;

    [DataMember]

    public Stream upload;

    }

     

    Throw this exception?

     

    "The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue."

     

    However passing it in a MessageContract is fine. I see that a stream comes out the other side as a MessageBodyStream.

     

     I would expect that this is a serialization issue, however I'm trying to understand fully what's going on behind the scenes.

     

     

     

     

     

     

    Wednesday, August 01, 2007 5:27 PM
  • The data contract is not really how you do streaming i think have a look at the samples...All you are doing is sending a copy of the stream which will fail as it will not be able to read from its source ( or will read the entire stream into memory  , serialize it all and send it)

     

    Also sending byte arrays is really bad pratice especially for http bindings.. You will send 13-16 bytes for each byte .. eg

    <byte>111</byte>  =16  UTF8 chracters  for sending the value 111.  You really need to uuencode it to a string - binary tcp transports will probably be ok.

     

    Why do you want to send a byte[] or a stream ?

     

    How to stream

    http://kennyw.com/indigo/91

    http://blogs.msdn.com/drnick/archive/2006/03/31/565558.aspx

     

     

     

     

     

    Regards,

     

    Ben

     

     

    Thursday, August 02, 2007 10:28 AM
  • BenK,

     

    Thank you for your input, but I don't think you understood my question. It was not HOW to to implement streaming in WCF but WHY it works the way it does.

     

    Did you read all of my post? Or just the parts with the pretty colors?

    Friday, August 03, 2007 2:27 AM
  • If i did not answer it i will try better - your data contract sample was just at the wrong level ( ie it will just serialise the stream) which i was trying to get accross.  The byte array comment was on the original post where the sample was a byte array. Appologies but a lot of people who ask about streams do not really understand streaming over the network and when to do it and why .. ie not performance but to manage memory better.  .

     

    eg http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1008778&SiteID=1

     

     

    basically WCF is a message based system it does not understand streams.  To implement streaming when expecting individual messages you need some sort of alternative or break out strategy . Basically a WCF Message ( ie message contract NOT data contract )  will call the serializer once for each member in the body and add the result to the output message . 

    Now if you send a DataContract like you mentioned

     

    DataMember]

    public Int64 streamLength;

    [DataMember]

    public string streamHash;

    [DataMember]

    public Stream upload;

    }

     

    it will as i mentioned serialize the entire stream .. Ok say you build the streaming break out logic into the serializer.

     

    You send say 1000 1K  messages for a Meg of data Each message will have the structure you mentioned - as it is the contract.

     

    note you have just send 1000 stream hashes and streams length.  Now you pass each message to the serializer

    and you need to deserialize the message pull out the stream and then add the stream to the ongoing stream which is being build up . ( Note you are also serializsing things twice.)

     

    Note the contract specifies the message not the conversation. ( coloured stuff ;-) ) How do you specify the contents of a part of a stream in a message ?

     

    By limiting it to just the Message body you dont need to look for the stream in each message and you can break out before calling the serializer on the body and pass the body to the ongoing stream ( which is beign read by the deserialiser) .  

     

    I could be wrong of course ...but these are the things i would do if i were to implement streaming on a message based system.

     

    Regards,

     

    Ben

     

    PS:: if specifying the result as a stream this is the same as a message contract with a single stream member.

    Saturday, August 04, 2007 5:37 AM
  • mark
    Friday, August 06, 2010 10:31 AM
  • Mark2

    hifan

    Tuesday, June 05, 2012 7:17 AM