简体   繁体   中英

What is the purpose of MustUnderstand property in WCF?

I have read some samples about the streaming communication in WCF and I noticed that the MessageHeader attributes are specified with the MustUnderstand property set to true . What is the purpose of this property? Why is this property set to true ?

The MustUnderstand attribute specifies whether the node processing the header must understand it.

Imagine you were asked to write a web service that needed to provide a single operation (method) that allowed the user to upload a file using WCF.

We start by opening up Visual Studio and create WCF service library.BY default it contains IService and Service.cs we rename it to IFileUploadService.cs

[ServiceContract]
public interface IFileUploadService
{
    [OperationContract]
    FileReceivedInfo Upload(FileInfo fileInfo);
 }

There are two classes introduced here

  1. File Info

  2. FileReceivedInfo

    These classes are both decorated with the MessageContract attribute. To upload the file I opted to use streaming. WCF stipulates that the parameter that holds the data to be streamed must be the only parameter in the method. But because of this you cant send any additional information along with it. You can resolve it by creating a new class with MessageContract Attribute and pass in your additional parameters.

      [MessageContract] public class FileInfo { [MessageHeader(MustUnderstand = true)] public string FileName { get; set; } [MessageHeader(MustUnderstand = true)] public long Length { get; set; } [MessageBodyMember(Order = 1)] public Stream Stream { get; set; } } 

By applying the MessageHeader attribute to the FileName and Length propery you place this information in the header of the SOAP message. When streaming a file the body of the SOAP message must only contain the actual file itself. By applying the MessageBodyMember attribute to the Stream property you place it in the body of the SOAP message.

Headers are allowed to be processed independently of the body.This allows an intermediary application to determine if it can process the body,provide the required security,session etc etc.

mustUnderstand=1 means the message receipent must process the header element

must understand=0 or missing means the header element is optional

Simply, MustUnderstand=true means; The header contains crucial data to process, and the recipient of the message (a service) must process the headers. If the recepient, can't understand (cannot process) the header or didn't received the header, an error will be raised.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM