user481779
user481779

Reputation: 1081

WCF service is deadlocking

Below, I have outlined my WCF service/client setup. When a single client calls IWCFService.Process multiple times in succession the service deadlocks between WCFService.Process and the line in WCFService.Run that executes this.Subscribers.ProcessingCompleted.

An instance of the class WCFService is passed into the ServiceHost constructor and WCFService's Run API is called back from a thread created using QueueUserWorkItem. I am guessing the conflict originates between the thread WCF uses to call into WCFService.Process and the thread WCFService.Run is running under.

I have already added a ServiceThrottlingBehavior to allow 1000 MaxConcurrentSessions and 1000 MaxConcurrentCalls. Can I add attributes on the service or client classes that will prevent this deadlock?

[ServiceContract(Name = "IWCFClient")]
public interface IWCFClient
{
  [OperationContract]
  void ProcessingCompleted(Guid guid);
}

[ServiceContract(Name = "IWCFService", CallbackContract = typeof(IWCFClient))]
public interface IWCFService
{
  [OperationContract]
  Guid Process(string filename);
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
class WCFService : IWCFService, WFGenericThread<WFProcessingResult>, 
{
  public Dictionary<Guid, IWCFClient> Subscribers { get; set; }
  public override void Run(WFProcessingResult queueobj)
  {
    if(this.Subscribers.ContainsKey(queueobj.Guid))
      this.Subscribers.ProcessingCompleted(queueobj.Guid);
  }

  public Guid Process(string filename)
  {
    Subscribers.Add(guid, (IWCFClient)OperationContext.Current.GetCallbackChannel<IWCFClient>());
    // send filename off to be processed
  }
}

Upvotes: 2

Views: 1289

Answers (1)

Phil Degenhardt
Phil Degenhardt

Reputation: 7264

Your server is not thread safe.

The InstanceContextMode = InstanceContextMode.Single means WCF is properly preventing multi-threaded access, but your use of the thread pool behind the scenes is not subject to the same restrictions.

You need to make your server thread-safe or don't use the thread pool.

Here's a good introductory article on multi-threading.

Upvotes: 1

Related Questions