- Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications
- Steven Cheng
- 395字
- 2021-04-13 17:04:02
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.
- Define a custom data type that represents the entire SOAP message body content. Use
MessageContractAttribute
to decorate the type andMessageBodyMemberAttribute
to mark class members that will be embedded in the SOAP message body. The following code demonstrates a sampleMessageContract
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; } }
- 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 followingSayHello
operation):[OperationContract] HelloResponse SayHello(HelloRequest req);
In the
SayHello
operation,HelloRequest
is the only input parameter andHelloResponse
represents the return value.
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):


- Creating a service via ChannelListener in Chapter 5
- Complete source code for this recipe can be found in the
\Chapter 1\recipe5\
folder
- Vulkan學習指南
- 基于元胞自動機的城市路網交通流建模與仿真
- Protel DXP 2004 SP2原理圖與PCB設計(第4版)
- AutoCAD 2020中文版從入門到精通(標準版)
- Spring Web Flow 2 Web Development
- UG NX 9.0中文版 基礎教程 (UG工程師成才之路)
- Adobe創意大學Illustrator產品專家認證標準教材(CS6修訂版)
- Premiere pro CC中文版自學視頻教程
- 三維建模與3D打印從入門到精通
- Choosing an Open Source CMS: Beginner's Guide
- 中文版CINEMA 4D R20 實用教程
- Microsoft SharePoint 2010 and Windows PowerShell 2.0: Expert Cookbook
- 科技繪圖/科研論文圖/論文配圖設計與創作自學手冊:科研動畫篇
- 邊做邊學:CorelDRAW X6圖形設計案例教程(第2版)(微課版)
- Audition CC音頻處理完全自學一本通