Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications

You're reading from   Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications Over 85 easy recipes for managing communication between applications

Arrow left icon
Product type Paperback
Published in Oct 2010
Publisher Packt
ISBN-13 9781849680769
Length 316 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Juntao Cheng Juntao Cheng
Author Profile Icon Juntao Cheng
Juntao Cheng
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications
Credits
Foreword
About the Author
About the Reviewers
Preface
1. Working with Contracts 2. Endpoint, Binding, and Behavior FREE CHAPTER 3. Hosting and Configuration 4. Service Discovery and Proxy Generation 5. Channel and Messaging 6. Dealing with Data in Service 7. Security 8. Concurrency 9. Extending WCF Runtime 10. RESTful and AJAX-enabled WCF Services 11. Interoperability 12. Diagnostics 13. Miscellaneous WCF Development Tips Index

Using MessageContract to control the SOAP message


DataContract can help us design the data types used in a WCF service. However, this only covers the data members (variables and parameters used in operation) serialized in the underlying SOAP message. Sometimes we also need to control the structure and format of the entire SOAP message.

WCF introduces a MessageContract concept, which helps service developers to model the structure and format of the entire message of a given service operation. Actually, we can take MessageContract type as a special DataContract type, which is marked by the MessageContractAttribute. This recipe will show you how we can define a typical MessageContract for our WCF service operation to control the format of the underlying SOAP XML message.

How to do it...

  1. Define a custom data type that represents the entire SOAP message body content. Use MessageContractAttribute to decorate the type and MessageBodyMemberAttribute to mark class members that will be embedded in the SOAP message body. The following code demonstrates a sample MessageContract pair—one is for operation request and the other for operation response.

        [MessageContract(WrapperName="Hello",WrapperNamespace="http:// wcftest.org/messagecontract")]
        public class HelloRequest
        {
           [MessageBodyMember(Name="Who")]
           public string User { get; set; }
        }
     [MessageContract(WrapperName="HelloResponse",WrapperNamespace="http://wcftest.org/messagecontract")]
        public class HelloResponse
        {
            [MessageBodyMember(Name="Reply")]
            public string ReplyContent { get; set; }
        }
  2. After defining the MessageContract types for request/response operation, we need to use them as the input parameter (the only input parameter) and return value of the operation's implementation (see the following SayHello operation):

    [OperationContract]
    HelloResponse SayHello(HelloRequest req);

    In the SayHello operation, HelloRequest is the only input parameter and HelloResponse represents the return value.

How it works...

Types marked with MessageContractAttribute can be used to represent the entire SOAP envelope body. The serialization of such types still follows the rules for normal DataContract types.

Also, it is important that operations which use MessageContract to control the SOAP envelope only have a single input parameter and return value. This is because only that input parameter will be serialized as the entire SOAP request body, and the return value will be serialized as the entire SOAP response body.

By capturing the SOAP request/response on wire, we can find that the serialized SOAP message content conforms to the MessageContract definition (refer to the next two screenshots):

See also

  • Creating a service via ChannelListener in Chapter 5

  • Complete source code for this recipe can be found in the \Chapter 1\recipe5\ folder

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime