Message Contracts ? Why ? Data Contract ?

Topics: July and December Releases Forum
Aug 7, 2007 at 3:49 AM
Hi,

I have seen in webcasts as well as Hands On Lab, there is always a Message Contract created, especially a Request and Response. And when one creates ServiceContract, they associate this message contract. But my question is, how do we associate data contracts if we like with a ServiceOperation when creating a ServiceContract using the Wizard. For example , consider the following,

[OperationContract]
String StoreDetails(Details1 details1,Details2 details2);

The Details1 and Details2 are datacontracts

How do I implement this using the Wizard for Creating Service Contract ?

Thanks

Regards,
Chaks
Aug 7, 2007 at 3:08 PM
Are you trying to create a service request with multiple data contracts WITHOUT a message contract?

If that's the case, why?

What you'd normally do is this:

OperationContract
StoreDetailsResponse response StoreDetails(StoreDetailsRequest request)
{}

Where response is a message contract called 'StoreDetailsResponse' and has one member (which in your example would be a string)

AND

Where Details1 and Details2 are separate DataContracts. The StoreDetailsRequest object would then contain 2 members (of type Details1 and Details2 respectively).

HTH
Aug 7, 2007 at 3:52 PM
Thanks Carphill. I thought it would be that way :)

Why I had a doubt was, my friend told this from one of the books,

Windows Communication Foundation Unleashed – Carig McMurtry
CHAPTER 21 - Guidance
AVOID using message contracts.
Message contracts are for differentiating the data that should be among the headers of a message from the data that should be in the body. However, it has become customary to use headers only for information pertaining to communication protocols, rather than to use them for conveying substantive information. Therefore, unless one is defining a communication protocol or using one that defines message headers, one should have no use for message contracts.

Can anyone explain the above ?

Thanks
Aug 8, 2007 at 11:00 AM
I have heard that you only need DataContracts OR MessageContracts and not necessarily both, but I think I've seen an instance where a developer here mistakenly created a DataContract object instead of a MessageContract object and the service threw a runtime exception as a result...

If I get time, I'll try to prove this. I have definitely created a service that ONLY uses a message contract (uber simplistic service) but reading your excerpt this probably wasn't best practice!
Aug 8, 2007 at 4:50 PM
DataContracts essentially control how complex types are serialized across the wire. MessageContracts give you control over the entire SOAP Message Structure, allowing you to specify what goes in the SOAP Header and SOAP Body. There are attributes that you can use with MessageContracts, MessageHeader and MessageBodyMember, that give you the ability to specify how to structure the SOAP Message.

Unless you need finer control over what gets placed in the header or body of the SOAP Message, you won't be needing to use a MessageContract. I would say the default is to always use DataContracts unless you have special needs.

Regards,

Dave

_____________________________

David Hayden
Microsoft MVP C#
Aug 8, 2007 at 5:00 PM
Edited Aug 8, 2007 at 5:02 PM


DavidHayden wrote:
DataContracts essentially control how complex types are serialized across the wire. MessageContracts give you control over the entire SOAP Message Structure, allowing you to specify what goes in the SOAP Header and SOAP Body. There are attributes that you can use with MessageContracts, MessageHeader and MessageBodyMember, that give you the ability to specify how to structure the SOAP Message.

Unless you need finer control over what gets placed in the header or body of the SOAP Message, you won't be needing to use a MessageContract. I would say the default is to always use DataContracts unless you have special needs.


Thanks Dave :)

So I think WCF Service Factory Wizard allows only to have Message Contracts right ? I could not find any way that I could specify more than one Data contract for a Service operation through the Wizard :(
Aug 9, 2007 at 4:11 PM
You can specify DataContracts with the Service Contract Wizard.

The wizard only allows you to specify one input parameter to a service operation, but that is just a limitation of the wizard. If you want to add more, you can do it manually by just adding additional parameters.

Regards,

Dave

___________________________

David Hayden
Microsoft MVP C#
Aug 9, 2007 at 10:36 PM

If you want to add more, you can do it manually by just adding additional parameters.


Yep, that is what I am doing now, but I am glad that the Wizard is able to generate the necessary code of service operation with one parameter..... :)

----
Regards,
Chaks
Aug 10, 2007 at 10:36 AM
I've had more of a think about this, and although I take the points very well, I think I'll continue to use message contracts in most cases. Here's why:

I have a service with one data contract as the request and one as the response. I dont use message contracts.

As part of an iterative development process, the requirements change and now two data contracts are needed (either as request or response) because the two lots of information are discreet.

If I havent used a message contract, I need to amend the service interface AND the implementation, whereas if I'd used a message contract, that's all I'd need to change.

Thoughts?
Aug 10, 2007 at 12:14 PM

If I havent used a message contract, I need to amend the service interface AND the implementation, whereas if I'd used a message contract, that's all I'd need to change.

I agree with you Carphill.

We have to decide on the needs of our application. At places where you feel like it may get updated later (from your design), its better to have Message Contracts. Like for an example, I would put all my responses as Message Contracts because I may very well add Error Details and other stuffs that I am interested to :)

------
Regards,
Chaks