views:

757

answers:

2

I have a certain service where specific functions will take longer to call than others, sometimes they might take seconds to return. In order to prevent the client's UI being blocked when this happens what is the preferred solution:

  1. Use a Duplex channel and simply use the callbacks to update the UI when data is received.
  2. Use a separate thread to call the service, and simply use request-reply operations, and then update the ui thread when data is returned.

Which solution is better, particularly when interoperability is favored but not strictly necessary, and in your opinion, which one is faster (and cleaner) to implement and maintain?

A: 

You should implement a CallBackcontract.

Here is an example.

Igor Zelaya
Is there any particular reason for using a CallbackContract (aka Duplex channels) over using a thread on the client side?
lozzar
+3  A: 

If you implement callback contracts then you are removing the need for the client to implement multithreading code. This might not be a significant advantage when working with .Net clients (as VS will auto generate the asynch proxy code for you), though could prove beneficial when working with clients of other platforms/languages.

Which one is cleaner? Well, that depends whether you are a client or server developer. If, as I suspect in your case, you are both, and you can just use .Net for client and server, then I'd probably be tempted to avoid callbacks for now. If you'd have implied that the service calls where taking 45 seconds then I'd say call back contracts, it really is subjective, but if I were to stick my neck out then I'd say that if responses take longer than 5 seconds then it is time to move to callbacks.

ng5000