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.
The basics:
- First, decide how long you want each "long poll" to be. For argument's sake I'm going to choose 5min timeouts.
- On the client side binding, change the
sendTimeout="00:05:00"
.
- Just like using XmlHttpRequest (XHR) for long polling, when the timeout does actually occur, you will need to detect it and re-issue the next polling request. This is quite easy in WCF because there is a specific exception,
TimeoutException
, that you can catch to easily detect this was the issue vs. some other exception.
- Depending on how you're hosting your WCF service, you will need to make sure to configure yourself to allow processing for up to 5mins. From a pure WCF perspective you'll want to make sure you set the
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).
Being efficient in the client
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.
Being efficient on the server
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.
Updated Prescription
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.