Search code examples
.netwcfduplex

Should I use a lock to prevent a WCF deadlock in this situation?


I have a WCF duplex service with a 2 way method called GetList, plus a one way callback method ItemUpdated.

I've detected a deadlock in the following situation:

  1. The service calls the ItemUpdated callback, which is handled in the client by the method OnItemUpdated.
  2. Before the previous method returns, some user interaction causes the client to call GetList on the service.

I could set the ConcurrencyMode of my service to Reentrant, but that's not what I need. What I need is to make sure that my client is not processing any callback from the service before calling GetList.

So I can synclock an object to make sure that this doesn't happen. Is that my better alternative?

UPDATE:

Just to clarify what my current design is, my service is actually a windows service that does some processing on a list of objects on a schedule and when each item is updated it raises the event ItemUpdated. This service has a WCF service facade that allows one or many consoles (clients) to subscribe to its events and see what is going on in the service.


Solution

  • The problem was not really on the service but on the callback handler in my client. This is basically what was happening:

    1. My service raises an event that calls ItemUpdated on the client.
    2. While the callback is still processing ItemUpdated, the user forces GetList to be executed.
    3. The service receives the request and sends back the result of GetList, but my client cannot process the response from the service because it is already processing another callback.

    So I ended up changing the ConcurrencyMode in the callback using the CallbackBehavior attribute. The service remains with ConcurrencyMode = Single.