see also "WCF push to client through firewall"
I need to have a WCF client that connect to a WCF server, then when some of the data changes on the server the clients need to update its display.
As there is likely to be a firewall between the clients and the server.
As I am writing both the client and the server I do not need to limit the solution to only using soap etc.
I am looking for built in surport for "long polling" / "Comet" etc
Thanks for the most informative answer from Drew Marsh on how to implement long polling in WCF. However I thought the main “selling point” of WCF was that you could do this sort of thing just by configuring the channels to be used in the config file. E.g I want a channel that logically two way but physically incoming only.
With the service running, right click the project that will contain the WCF client proxy and select Add > Service Reference. In the Add Service Reference Dialog, type in the URL to the service you want to call and click the Go button. The dialog will display a list of services available at the address you specify.
A WCF client is a local object that represents a WCF service in a form that the client can use to communicate with the remote service. WCF client types implement the target service contract, so when you create one and configure it, you can then use the client object directly to invoke service operations.
It sounds to me like you already know the answer: use long polling. :) So I guess the only thing left to explain is how you might be able to accomplish this with WCF and in the most efficient manner possible.
sendTimeout="00:05:00"
.TimeoutException
, that you can catch to easily detect this was the issue vs. some other exception.receiveTimeout="00:05:00"
. However, if you're hosting inside of ASP.NET you will also need to configure the ASP.NET runtime to have a higher timeout which is done using the <httpRuntime executionTimeout="300" />
(note: the measurements are in seconds for this attribute).If you just setup your client to synchronously call the service and the client blocks for 5mins while waiting for a response, that's not a very efficient use of system resources. You could put these calls on background threads, but that's still going to chew up a thread resource while the call is outstanding. The most efficient way to deal with this is to use async operations.
If you're creating your service contracts by hand, I would suggest checking out this section on MSDN on OperationContractAttribute.AsyncPattern
for details on how to add a BeginXXX
/EndXXX
async method pair for each of your calls. However, if you're using svcutil
to generate your operation contracts for you, all you need to do to have async methods generated is pass the /async
option on the command line. For more details on this topic, check out the Synchronous and Asynchronous topic on MSDN.
Now that you've go your async operations define, the pattern is very much like working with XHR. You call the BeginXXX
method to which you pass an AsyncCallback
delegate. The BeginXXX
method will return you an IAsyncResult
, which you can either hold onto if you wanted to be able to wait on the operation (in more advanced scenarios) or ignore, and then the WCF infrastructure will asynchronously send the request to the server and wait for a response behind the scenes. When a response is received or an exception occurs, the callback you passed into the BeginXXX
method will be invoked. Inside of this callback method you need to call the corresponding EndXXX
method passing in the IAsyncResult
that is handed to you. During the call to the EndXXX
method you need to employ exception handling to deal with any kind of logical fault that may have occurred while calling the method, but this is also where you'd now be able to catch the TimeoutException
we talked about earlier. Assuming you got a good response, the data will be the returned from the EndXXX
call and you can react to that data in whatever way makes sense.
NOTE: One thing to keep in mind about this pattern is the nature of the threading. The async callbacks from WCF will be received on a thread from the managed thread pool. If you're planning on updating the UI in a technology such as WPF or WinForms, you need to make sure you marshal the calls back to the UI thread using the Invoke
or BeginInvoke
methods.
If we're going to be worried about efficiency in the client, we should be doubly so when it comes to the server. Obviously this type of approach puts more demand on the server side because a connection must remain open and pending until there is a reason to send notification back to the client. The challenge here is that you only want to tie the WCF runtime up with the processing of those clients who are actually being sent an event. Everything else should just be asleep, waiting for the event to occur. Luckily the same async pattern we just used on the client side also works on the servers side. However, there is now a major difference: now you must return the IAsyncResult
(and thus a WaitHandle
) from the BeginXXX
method which the WCF runtime will then wait to be signaled on before calling your EndXXX
method.
You will not find much in the way of documentation inside of MSDN other than the links I've already provided earlier and, unfortunately, their samples on writing an async-service are less than useful. That said, Wenlong Dong wrote a piece about scaling WCF services with the async model some time ago that I highly recommend you check out.
Beyond this, I honestly I can't give too much advice on how best to implement the asynchronous model on the server side for you bcause it depends entirely on what kind of data source your events will be coming from in the first place. File I/O? A message queue? A database? Some other proprietary software with its own messaging service that you're trying to provide a façade over? I don't know, but they should all offer an async models of their own on which you can piggy back your own service to make it as efficient as possible.
Since this seems to be a popular answer, I figured I should come back here and provide an update given the recent changes in the landscape. At this point there is now a .NET library called SignalR which provides this exact functionality and is definitely how I would recommend implementing any such communication with the server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With