如果你有一個OperationContract的,返回類型始終序列化爲XML或optionaly爲JSON。如果您不希望將返回值序列化,請將其定義爲Stream。
[OperationContract]
[WebGet]
Stream EventSource();
// Implementation Example for returning an unserialized string.
Stream EventSource()
{
// These 4 lines are optional but can spare you a lot of trouble ;)
OutgoingWebResponseContext context = WebOperationContext.Current.OutgoingResponse;
context.Headers.Clear();
context.Headers.Add("cache-control", "no-cache");
context.ContentType = "text/event-stream"; // change to whatever content type you want to serve.
return new System.IO.MemoryStream(Encoding.ASCII.GetBytes("Some String you want to return without the WCF serializer interfering."));
}
如果您自己構建流,請記得先執行.Seek(0, SeekOrigin.Begin);
,然後再返回它。
編輯: 改變命令的順序來設置ContentType後,頭部得到清除。否則,你會清除剛剛設置的ContentType太;)
請參閱:http://stackoverflow.com/questions/992533/wcf-responseformat-for-webget – seraphym