- 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
- Learning SQL Server 2008 Reporting Services
- 中文版CorelDRAW 2022基礎教程
- 中文版AutoCAD 2016從入門到精通
- InDesign平面設計案例教程:從設計到印刷
- SOLIDWORKS Visualize 實例詳解(微視頻版)
- ASP.NET 3.5 Application Architecture and Design
- Photoshop CC移動UI設計實用教程
- UG NX 9中文版從入門到精通
- Photoshop CC 2017 淘寶美工設計實例教程
- Elasticsearch數據搜索與分析實戰
- 中文版Photoshop CC基礎培訓教程
- 修片有道:PHOTOSHOP攝影后期專業技法
- ChatGPT+AI文案寫作實戰108招
- Creo快速入門教程(Creo 8.0中文版)
- Getting Started with Oracle Hyperion Planning 11