HttpResponseMessageProperty.SuppressPreamble 属性

定义

获取或设置是否禁止发送消息前导码。

public:
 property bool SuppressPreamble { bool get(); void set(bool value); };
public bool SuppressPreamble { get; set; }
member this.SuppressPreamble : bool with get, set
Public Property SuppressPreamble As Boolean

属性值

Boolean

如果禁止发送消息前导码,则为 true;否则,为 false

注解

SuppressPreamble 属性使用户能够将内容从 WCF 操作正文中 OutputStream 写入。 这仅适用于 Web 托管方案。 The SuppressPreamble property is false by default.

警告

如果属性 SuppressPreamble 设置为 true,则必须在响应上设置标头、内容类型和状态代码,因为 WCF 将不再执行此操作。

以下代码演示了如何执行此操作的示例。

public class Service1 : IService1  
{  
    public void GetData()  
    {  
        HttpContext hc = HttpContext.Current;  
        string str = @"<?xml version=""1.0"" encoding=""utf-8"" ?>";  
        var buffer = new byte[str.Length];  
        buffer = ASCIIEncoding.UTF8.GetBytes(str);  

        // Enable the property.
        var responseProperty = new HttpResponseMessageProperty();  
        responseProperty.SuppressPreamble = true;  
        OperationContext.Current.OutgoingMessageProperties[HttpResponseMessageProperty.Name] = responseProperty;  

        // Set the response.
        hc.Response.StatusCode = 200;  
        hc.Response.ContentType = "text/xml; charset=utf-8";  
        hc.Response.ClearContent();  
        hc.Response.Flush();  

        hc.Response.OutputStream.Write(buffer, 0, buffer.Length);  
        hc.Response.Flush();  
   }  
}  

适用于