如果你有一个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