views:

75

answers:

1

I've attempted the question different ways, check my profile for the other two questions explaining the difficulty I've had with the approaches I've taken to this scenario. I'll skip them here.

I just need an example (C# preferred) of a server calling back to a client (over a nettcp channel), the client calculates an answer, and returns a result.

  • The server's calling thread is blocked until the client sends the response.
  • The server's callback is prompted by some event, such as a timer, not a call by the client.
  • The client does not need to call back to the server within the context of the server's callback, so there should be no deadlocking challenges.
  • The client may take (and for this exercise, should take) one or more parameters with which to perform the calculation
  • The client may return (and for this exercise, should return) a non-void result
  • The approach used should at least be compatible, if not implemented, with the ability to handle multiple clients in turn, including a subscribe() and unsubscribe() functionality.
  • This is not homework, it is to serve as an example of how to build a WCF-based subscriber/publisher server that supports (few) trusted clients with low latency server-client communication without polling and without throwing messages (over the fence)
  • I am specifically NOT interested in solutions that involve (IsOneWay = true), unless it becomes clear that I am very confused about its meaning and its consequences.

Thanks!

+1  A: 

Check this article on CodeProject. This describes basic example of callbacks. Few things that you may have to change:

  • On callback contract, operations marked as one way - this is to avoid blocking of server due to bad client (a recommended practice). But if you must block the server then you need to remove one way. Note that if you are going to callback multiple clients one by one then you may have to callback each one on different thread other wise first client will block callback to next client.

  • When to invoke callback is really a server implementation. The given example maintains a list of client callback channels whenever client joins (or subscribes in your requirement). Now this list can be used to invoke callback in any way you want. So you can invoke callbacks on timer by simply iterating over the list. Note that you have to ensure thread-safe access to the list.

  • If client has to return some result in callback then again OneWay cannot be used.

  • As mentioned earlier, subscribe means simply adding to the list (join party in example) and unsubscribe means removing from the list (leave party).

Edit:

I have taken the source code from the example sighted and modified it as follows:

Added a method Echo in callback contract:

public interface IBeerInventoryCallback
{
   ...

    [OperationContract]
    string Echo(string message);
}

Invoked Echo from service when someone left the party and printed response from client on console. And it worked w/o any issues.

Note that this example uses VS generated client proxy that inherits from System.ServiceModel.DuplexClientBase<T> which makes client code much simpler. Perhaps, you should try it.

VinayC
Easier to describe IsOneWay=false than done.
uosɐſ
What's the issue in not specifying IsOneWay attribute on callback contract?
VinayC
It doesn't work, as far as I've tried. [See my example here](http://stackoverflow.com/questions/3392123/wcf-duplex-callback-sample-failing). Does it work for you?
uosɐſ
Sorry for the delayed response - StackOverflow seems to be getting a little slow with message notifications...
uosɐſ
@uosɐſ - I am not certain if your code works or not. However, I just modified the example (that I have sighted) source code to add sync callback and it work w/o any issues. I have edited my answer to add my changes.
VinayC