Chapter news contract

[MessageContract] attribute defines the SOAP message structure. This property is not too much sugar, because its purpose is to define the boundaries of the message rather than the message content itself. The only modification is the body of the message on how many parcels into a single SOAP message, determine whether to include all, if that is the case, also need to determine Warpper name and namespace.
2010 年 10 月 22 日
In order to use [MessageContract] types of messages generated on behalf of the client proxy code, you need to check the Add Service Reference dialog box is always senior to generate a message contract options, as shown in picture 2.9. 2.31
list typed messages using a non-contract the client initiates the communication
Whether you are using a typed or untyped messages, you may want to transfer the SOAP message header information and is not a SOAP message body. A common requirement is sent with the message session state or context information. Therefore, in addition to creating additional packaging information, SOAP message header is a convenient and easy to understand the structure of the transmission of messages.
types of messages using a non-SOAP message header
type of news you might want to define a lot of message header or message body if they are different levels of the client software to use. For example, a level of need associated with the SOAP message header to the message as a request message generated Response. Also another level may want to determine the message so that it can be appropriate to forward the message. In this case, the first two messages have their own purpose, there is no reason to merge them into a structure to go.

typed messages using [MessageHeader] and [MessageBodyMember] attribute to describe the SOAP message headers and body structure. Client and server can use the sequence of object references this data. SOAP message headers associated with additional information such as name and namespace, regardless of whether the message will be delayed, or which side is the final end and the recipient. Additional information can also be associated with the SOAP message body, such as name and namespace. If you use more than one message body, MessageContract can define the order of these parts. Message header and message body can have simple or complex type definition.
2.30 shows a list using the message type as input and output operations contract. Note that the message GetBody method is a common anti-serialized into the message body types. This method uses an XMLReader to read the SOAP message elements. Because it uses an XML reader, make can be read once; if you want to read more than once elements, you should use the message CreateBufferCopy method. Re-side SOAP operation name is the name of the requested action followed by the “Response” suffix. This can be [OperationContract] attribute (ReplyAction =) to reload.
2.34 client list to a non-typed message into the message header
contract through using direct access to the SOAP message header and message body sent to a service providing a service and to receive from the SOAP message complete control. This allows the use of simple or complex type to define the precise content of the SOAP part. Like when you need full control of data serialization when you convert from DataContractSerializer to XmlSerializer, when you need full control of the SOAP message when you convert from DataContracts to MessageContracts.
Message class by a number of create, read and write the message content. Client is responsible for the message sent to the server before the server is responsible for creating the message and creates a return message. Before sending the message, the content must be placed in the message body. This can be CreateMessage, WriteMessage or WriteBody methods.

list 2.35 shows the code generated by the client-side SOAP message. Note inserted into the appropriate namespace the TimeZone element in the message header. 2.32
list messages using a non-contract type of the SOAP response message list 2.33
typed messages using a non-contract services to access the message header [ServiceContract] public class StockService2 {[OperationContract] private Message GetPrice ( Message req) {string timeZone = OperationContext.Current.IncomingMessageHeaders.Ge tHeader (“TimeZone”, “http://EssentialWCF/”); string ticker = req.GetBody (); Message resp = Message. CreateMessage (req.Version, req.Headers.Action “Response”, timeZone “|” ticker “|” “94.85″); return resp;}} list 2.34 describes how a client adds a SOAP message head to the side that will be sent to the service untyped messages. First, create a message using CreateMessage, use the constructor to put the data message. Then a type of MessageHeader is created; in this case it is a string data into the message header by the constructor. Next, an untyped MessageHeader MessageHeader be created from the type of the last type of non-MessageHeader to be added to the message sent to the service.
untyped messages untyped operating lease allows the client and server almost in the SOAP message body can transmit any content, as long as the content of communication can be used to encode the binding stack. WSDL message content is absolutely transparent for the mercy of no XSD to define the data. Client and server use System.ServiceModel.Channels.Message class to create, read and write messages.
non-news type of contract does not use any property of a message to describe their contents. It is completely run-time code used to make the content meaningful. This is the direct use of XML message set is very useful information, in which case you may want to while away completely lost in the WCF code using the Document Object Model programming (Document Object Model). Used in conjunction with non-service operation type of the message receiver and returns the information set of XML message types.
2.33 shows a list of non-realization of a type of the message header in the operation and then read data from the service contract. Note that the message header data, timeZone, is a line of code access.

messages sent to a contract describes the services and services received from a SOAP message structure, and allows you to detect and control the SOAP message headers and body in most of the details. And data contract allows use of XML metadata definition (XSD) standard for interoperability between systems, the message contract that enables any system to communicate via SOAP interoperability.


If you are using typed messages, WCF 2.28 through the list described in [MessageHeader] attribute to explicitly support it. If you use an untyped messages, however, you need to explicitly add an untyped message header.
2.28 shows a list of typed message contract, StockPrice. the message header contains a simple type, DateTime, the message body contains a complex type, PriceDetails. PriceDetails class must be serializable, or by using a [ DataContract] attribute, or, as here shown, use the [Serializable] attribute. This example is only a message header and a message body, but can have a lot of message header and message body.
If you want to leave the operation signatures to the transmission of information, then transmit the message through the SOAP message header is useful. For example, session, or related information can be passed in the message header, rather than to add additional operating parameters or add the data itself as an attribute of information. Another example is the safe, when you want to implement a custom security protocol (bypassing the WS-Security) and the custom SOAP header to pass the certificate or certificates. The third example, is about the safe, you may want to sign, encrypt some or all of the local header of the SOAP message header to sign and encrypt. All of these situations can be handled using a message contract. The disadvantage of this technique is the client and server must be added manually from the SOAP message headers and collect information, rather than operating lease contracts and associated data serialization classes for you to achieve.
Note service operation to receive and send message type. When using a message contract, the input and output parameters must be marked [MessageContract] property of the message. More specifically, the operation must include a precise input parameter and must return to determine the unique result, they are news, because the request from the operator to send out and receive a reply message to directly map their SOAP said. Additional message-based programming and parameter-based programming can not be confused, so you can not put a data contract as an operation input parameters and returns a message that it contracts as a result of, or identify a message as an operating lease and the input parameters it returns a data contract as a result. You can tell the type of message is mixed and non-typed messages, but not the message contract and data contract. In the end you generate the WSDL from the service contract and data contracts to mix the message will cause a runtime error.
2.32 shows the list returned by the server to the list of 2.31 in the SOAP message request. Note that the operation of the SOAP message header in the back there is a “Response”, SOAP message body is a non-XML format string.

In some scenarios, the design stage you may not know the client and server transmit the message structure. For example, the information will be incorporated into the message itself, such as in the design stage to determine the routing information and services operations. Or the client and service side of a software (or hardware) level to operate the SOAP message and look forward to a special data format. Of these cases, the non-contract type of operation will be very useful.
2.35 client list into the message header to an untyped message
corresponding, you can use svcutil.exe / messageContract or / mc option. This makes the svcutil.exe to generate proxy with a public method to receive typed messages, so clients can call for the method of approach. If you are using without / mc option of svcutil.exe way or you did not use the add service reference is always selected to generate a message contract will be used to receive parameter method to generate a public agency, and will be called message-based operating within . In any case, the line will send the same XML message. 2.28 to define a typed list of news contract namespace EssentialWCF {[Serializable] public class PriceDetails {public string Ticker; public double Amount;} [MessageContract] public class StockPrice {[MessageHeader] public DateTime CurrentTime; [MessageBodyMember] public PriceDetails Price;} [ MessageContract] public class StockPriceReq {[MessageBodyMember] public string Ticker;} [ServiceContract] public interface IStockService {[OperationContract] StockPrice GetPrice (StockPriceReq req);} public class StockService: IStockService {# region IStockService Members public StockPrice GetPrice (StockPriceReq req) { StockPrice resp = new StockPrice (); resp.Price = new PriceDetails (); resp.Price.Ticker = req.Ticker; resp.Price.Amount = 94.85; return resp;} # endregion}} list 2.29 shows when a SOAP message returned from the server to the client when the transmission of XML messages. Note [MessageHeader] elements, CurrentTime, in the SOAP message header and [MessageBodyMember] elements, Price, in the SOAP message body. 2.29 to generate a list of typed messages using the SOAP Response
contract list 2.30 definitions and implementation of non-news type of contract [ServiceContract (Namespace = "http://EssentialWCF")] public class StockService2 {[OperationContract] private Message GetPrice ( Message req) {string ticker = req.GetBody (); Message resp = Message.CreateMessage (req.Version, req.Headers.Action “Response”, ticker “|” “94.85″); return res CITIC Bank that pits father, buried by an annual fee to upgrade CITIC credit card trap p;}} code similar to the services the client-side code, use CreateMessage to create a message, use the appropriate version to match the binding, then use GetBody method to read the server the returned results. Note CreateMessage method uses three parameters: version, operating, and string messages. When you create a message, the message version must be used to communicate with the server bindings compatible MessageVersion property through the channel definition. Operations, such as http://EssentialWCF/StockService/GetPrice, are SOAP and WCF based server used to forward the message to the appropriate action. 2.31 shows the list with 2.30 in the service code list to communicate with the client code.

No comments allowed