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

属性值

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

注解

属性 SuppressPreamble 使用户能够将内容从 WCF 操作主体中的 写入到 OutputStream 中。 这仅在 Web 托管方案中生效。 默认情况下, SuppressPreamble 属性为 false

警告

如果 属性 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();
   }
}

适用于