Recently, I have been studying the Ajax-enabled WCF method to provide services directly to the front-end. I have encountered some minor problems in the optimization of returned data, which are summarized as follows:
Problem 1: If you want the WCF Service method to directly return the stream, but if you directly return the stream, the browser recognizes it as a file stream. A download prompt is displayed;
Question 2: If Gzip is used to compress the JSON data stream, you need to modify the content-encoding attribute in the returned response. Otherwise, the browser will identify the stream as garbled.
Solution:WeboperationcontextClass!
In WCF, the request and response objects of the current request cannot be obtained from httpcontext. Current. Therefore, you must useWeboperationcontext(Ps, this class can only get the context in the ASP. NET compatible mode of WCF, the common WCF needs to use operationcontext)
BelowCodeSolve Problem 1 and problem 2 respectively:
Weboperationcontext. Current. outgoingresponse. contenttype ="App/JSON; charset = UTF-8";
Weboperationcontext. Current. outgoingresponse. headers. Add ("Content-encoding","Gzip");