How can I stream a response from WCF without buffering?

后端 未结 3 1710
攒了一身酷
攒了一身酷 2021-02-09 10:53

I have a restful (webHttpBinding) self-hosted WCF service. Most methods are returning xml or json version of objects to the client.

I have a couple of GET methods that t

相关标签:
3条回答
  • 2021-02-09 11:03

    I've done something like what you're asking about here - self hosted, too. I wrote a WCF (BasicHttpBinding) service that did both streaming and buffering of data to client devices consuming my service for data synchronization. Streaming is hard, as you've probably figured out, and I don't think there's any way to "write into the stream".

    In a basic sense, Streaming over a WCF service works the same way that File.IO works, as seen in the code below

     FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
     BinaryReader br = new BinaryReader(fs);
    

    If the file in question is 1 GB, your filestream will begin returning bytes before its read to the end of the file. Streaming over WCF works the same way (in fact, it implements FileStream, in my experience), which is why it's good for huge chunks of data. It reads ... it sends; it reads ... it sends. So I'm not sure how you'd inject some information into that stream for output to your screen.

    Having said that, our Synch UI displays the count of bytes coming down, plus the percentage complete, to keep the users from turning off the machine or canceling. We do this by having a separate thread reads the size of the downloading file every 10 seconds and calculating the percentage of the whole (the complete size is send back as a parameter in the response), then writing the results out to the UI results window. So the solution is actually pretty simple, in our case.

    0 讨论(0)
  • 2021-02-09 11:08

    I do file streaming like this:

    Group the methods that returns data which needs to be streamed into an endpoint and then add the streamed transferMode on that endpoint.

    Here is the configuration I use (for basicHttpBinding).

    <services>
      <service name="CustomersService">
        <endpoint address="FilesService.svc" binding="basicHttpBinding" bindingConfiguration="StreamedBinding" contract="Soap.Interfaces.IFilesService" />
      </service>
    </services>
    

    and the define the binding configuration:

    <bindings>
        <basicHttpBinding>
            <binding name="IntersolveWebServicesStreamedBinding" allowCookies="true" transferMode="Streamed" maxReceivedMessageSize="67108864" />
        </basicHttpBinding>
    </bindings>
    

    Basically, you have to set the transferMode in the binding configuration.

    I haven't tried it with webHttpBinding, so please let me know if it works for you.

    0 讨论(0)
  • 2021-02-09 11:16

    Just trick the browser into thinking there is an HTML response with the Multipart Content-Type

    http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html

    I have used this for quite a few things including MJPEG but you can also use it for COMET / WebSocket like responses.

    0 讨论(0)
提交回复
热议问题