HttpResponseMessageProperty::SuppressPreamble Property

.NET Framework (current version)
 

Gets or sets whether the message preamble is suppressed.

Namespace:   System.ServiceModel.Channels
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

public:
property bool SuppressPreamble {
	bool get();
	void set(bool value);
}

Property Value

Type: System::Boolean

true if the message preamble is suppressed; otherwise, false.

The SuppressPreamble property enables users to write content into the OutputStream from within a WCF operation body. This takes effect only on webhosted scenarios. The SuppressPreamble property is false by default.

System_CAPS_warningWarning

If the SuppressPreamble property is set to true, you must set the headers, content-type, status code on the response because WCF will no longer do it.

The following code shows an example of how to do this.

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

      // enable the property
      HttpResponseMessageProperty 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();
   }
}

.NET Framework
Available since 3.0
Return to top
Show: