简体   繁体   English

WCF中的MustUnderstand属性的目的是什么?

[英]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 . 我已经阅读了一些有关WCF中的流式通信的示例,并且我注意到MessageHeader属性是在MustUnderstand属性设置为true情况下指定的。 What is the purpose of this property? 此属性的目的是什么? Why is this property set to true ? 为什么将此属性设置为true

The MustUnderstand attribute specifies whether the node processing the header must understand it. MustUnderstand属性指定处理标头的节点是否必须理解它。

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. 假设您被要求编写一个Web服务,该服务需要提供一个允许用户使用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 我们首先打开Visual Studio并创建WCF服务库,默认情况下它包含IServiceService.cs我们将其重命名为IFileUploadService.cs

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

There are two classes introduced here 这里介绍了两个类

  1. File Info 文件信息

  2. FileReceivedInfo FileReceivedInfo

    These classes are both decorated with the MessageContract attribute. 这些类都用MessageContract属性装饰。 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. WCF规定,保存要流数据的参数必须是该方法中的唯一参数。 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 Attribute创建一个新类并传入其他参数来解决该问题。

      [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. 通过将MessageHeader属性应用于FileName和Length属性,可以将此信息放置在SOAP消息的标题中。 When streaming a file the body of the SOAP message must only contain the actual file itself. 流式传输文件时,SOAP消息的正文只能包含实际的文件本身。 By applying the MessageBodyMember attribute to the Stream property you place it in the body of the SOAP message. 通过将MessageBodyMember属性应用于Stream属性,可以将其放置在SOAP消息的主体中。

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 mustUnderstand = 1表示接收到的消息必须处理标头元素

must understand=0 or missing means the header element is optional 必须了解= 0或丢失意味着header元素是可选的

Simply, MustUnderstand=true means; 简单来说,MustUnderstand = true意味着; 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. 如果接收方无法理解(无法处理)标头或未收到标头,则会引发错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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