简体   繁体   中英

WCF REST Streamed GET response

I have a requirement to return streamed response from WCF service.

The client would call GET on WCF REST URI and the server would send XML response when available. If no response is available, server would send a dummy XML response every few seconds to keep the connection alive.

I know this should ideally be done using Signal R (WebSockets) but I would like to know if there is a way to achieve this in WCF (without using Signal R). I don't have to return large data, I would like to send intermittent small sized XML data.

Let me know if someone has achieved something like this with WCF REST?

I am not sure how flexible you are by using WebAPI, sorry if its not what you want, but I came across this code below which is basically an api controller that pushes data back to the client on a continuous basis as HTTP response..

The client is basically a HttpClient which connects to a Uri address issuing a GET HttpRequestMessage . The message is sent with SendAsync and the stream is received as response.Content.ReadAsStreamAsync() ...

Here is the link: http://aspnet.codeplex.com/SourceControl/changeset/view/bb167f0b0013#Samples/Net4/CS/WebApi/PushContentControllerSample/PushContentController/Controllers/PushContentController.cs

The best way to create a persistent connection between server and client is to use WebSockets.

WCF can use WebSockets via the NetHttpBinding .

Once configured, you can force communication to always be over WebSockets via:

transportUsage=Always

Since you have a persistent connection, you will need to use callbacks to manage application flow when data is sent across.

There's also a detailed article here describing a few different ways to create WebSocket connections in .NET (without SignalR).

Create a WCF Service that Communicates over WebSockets

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM