WCF 4 REST - Acquiring the underlying response str

2019-06-11 04:37发布

Background: I am currently using the Visual Studio 2010 online template "WCF REST Service Template 40(CS)" and it works great for my primarily JSON based service. I've even got it working to return a stream when returning an image.

Scenario: I am currently looking into the server push & multipart/x-mixed-replace technology to replace the polling for images method I am currently using.

The Problem: The issue I am facing is that I am unable to find a way to get the underlying response stream for a REST request, I know the the template is trying to help by abstracting this away from me, but in order to implement the multipart/x-mixed-replace mechanism properly i will need to write directly to this stream and have full control of what i write to the client.

Any help would be much appreciated. The server push tech seems so cool!

1条回答
Rolldiameter
2楼-- · 2019-06-11 05:21

I have returned all sorts of things in the response, including documents, spreadsheets etc. It looks something like this:

[ServiceContract]
public class MyService
{
    [OperationContract]
    [WebGet(UriTemplate="{id}")]
    public Stream GetDocuments(int id)
    {
       WebOperationContext.Current.OutgoingResponse.ContentType = GetContentType();
       Stream result = CreateTemporaryStream();
       /* Write your data in the correct format */
       return result; 
    }  
}

You can pretty much return any format you wish. The client obviously have to be able to parse the stream and extract the individual streams from the response.

查看更多
登录 后发表回答