In the last chapter, the two topics of message are roughly covered: the messaging version and the five different ways to create the messages. This article will detail the other two topics of the message: the basic operations of messages, such as read, write, copy, close, and messaging state Machine.
Knowing how the message was created, we went on to discuss some basic operations of the message. In addition to the message creation described above, the operations involved in a message are generally grouped into the following 4 categories:
Read the message: Read the contents of the entire message or selectively read the header or the main part of the content;
Write message: The content of the entire message or the main part of the content to write files or streams;
Copy message: Generate another new message with the same content through a message copy;
Close message: Closes the message and reclaims some unmanaged resources.
The basic operations of these messages are closely related to the state of the message, and the relationship between message operations and message status is reflected in the following two areas:
The state of the message determines the action that can be taken;
The message operation is accompanied by a change in the message state.
One, the Messaging state Machine (Machine)
Figure 1 shows a message based state machine, from which we can draw some of the following rules about the state transition of the Messages object:
The read, write, and copy operations of a message can only be used on messages with a status of created;
The reading, writing, and copying of messages converts the status of messages from created to read, written, and copied.
All state messages can be turned off directly, and the status of the message is converted to closed.
Figure 1 Message Object state machine
In WCF, the state of a message is represented by a System.ServiceModel.Channels.MessageState enumeration, MessageState defines 5 message states, corresponding to the 5 state one by one shown in the previous illustration. The definition of MessageState is as follows:
1:public enum MessageState
2: {
3:created,
4:read,
5:written,
6:copied,
7:closed
8:}
Second, read the message
Reading the contents of the Message body section is the most common operation. If the contents of the principal part correspond to an object that can be serialized, you can read the message body by getbody<t> method and deserialize the corresponding object. And through the getreaderatbodycontents to get a XmlDictionaryReader object, through this object can further extract the contents of the message body part.
1:public Abstract class Message:idisposable
2: {
3://other Members
4:public T getbody<t> ();
5:public T getbody<t> (Xmlobjectserializer serializer);
6:public XmlDictionaryReader getreaderatbodycontents ();
7:}
Let's demonstrate an example of the Getbody<t> method. Suppose the message body part corresponds to the customer class shown below, which is a data contract.
1: [DataContract (Namespace = "http://www.artech.com")]
2:public class Customer
3: {
4: [DataMember]
5:public string Name
6: {get; set;}
7:
8: [DataMember]
9:public string Compnay
{get; set;}
11:
: [DataMember]
13:public string Address
: {get; set;}
15:
16:public override bool Equals (object obj)
17: {
18:customer customer = obj as Customer;
19:if (Customer = = null)
20: {
21:return false;
22:}
23:return this. Name = = Customer. Name && This.compnay = = Customer.compnay && this. Address = = Customer. address;
24:}
25:}