farsil
farsil

Reputation: 1045

Service proxy freezes application

this is a problem I have encountered that I'm completely clueless about. I have defined an IWorkerServiceContract class, as follows:

[ServiceContract]
public interface IWorkerServiceContract
{
    [OperationContract]
    int Test(int test);
}

Nothing special, I just wanted to test connectivity. Here is my service class:

class WorkerService : IWorkerServiceContract
{
    public static ILogger Logger { get; set; }

    public int Test(int test)
    {
        return test + 1;
    }

    public static ServiceHost Listen(Uri baseAddress)
    {
        ServiceHost host = new ServiceHost(typeof(WorkerService), baseAddress);

        try
        {
            host.Open();
            Logger.WriteLine("Listening at address: " + baseAddress.ToString());
        }

        catch (Exception e)
        {
            Logger.WriteLine("An exception was thrown, reason: {0}", e.Message);
        }

        return host;
    }
}

The logger object is instantiated by an initializer class, it basically logs to a console allocated with AllocConsole(). When I invoke Listen(), everything works fine, and I'm able to connect via WCF test client and remotely invoke the Test() method. Although, when I define a proxy:

public partial class WorkerProxy : ClientBase<IWorkerServiceContract>,              
                                   IWorkerServiceContract
{
    public WorkerProxy(EndpointAddress remoteAddress) :
        base("workerEndpoint", remoteAddress)
    { }

    public WorkerProxy(Uri remoteAddress) :
        base("workerEndpoint", new EndpointAddress(remoteAddress))
    { }

    public WorkerProxy(string remoteAddress) :
        base("workerEndpoint", new EndpointAddress(remoteAddress))
    { }

    public int Test(int test)
    {
        return base.Channel.Test(test);
    }
}

And use the following code:

WorkerService.Listen("net.tcp://localhost:19000/dcalc");
WorkerProxy wp = new WorkerProxy("net.tcp://localhost:19000/dcalc");
wp.Test(0);

the application freezes! Here's my app.config file:

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding messageEncoding="Mtom" maxReceivedMessageSize="10485760">
          <readerQuotas maxArrayLength="10485760" />
        </binding>
      </wsHttpBinding>
      <netTcpBinding>
        <binding maxReceivedMessageSize="10485760">
          <readerQuotas maxArrayLength="10485760" />
        </binding>
      </netTcpBinding>
    </bindings>
    <services>
      <service name="DCalc.Manager.ManagerService" behaviorConfiguration="managerServiceBehavior">
        <endpoint address="" binding="wsHttpBinding" contract="DCalc.Common.IManagerServiceContract"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
      <service name="DCalc.Worker.WorkerService" behaviorConfiguration="workerServiceBehavior">
        <endpoint address="" binding="netTcpBinding" contract="DCalc.Common.IWorkerServiceContract"/>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <client>
      <endpoint address="" binding="netTcpBinding" contract="DCalc.Common.IWorkerServiceContract" name="workerEndpoint" />
      <endpoint address="" binding="wsHttpBinding" contract="DCalc.Common.IManagerServiceContract" name="managerEndpoint" />
    </client>
    <behaviors>
      <serviceBehaviors>
        <behavior name="managerServiceBehavior">
          <serviceMetadata httpGetEnabled="true" policyVersion="Policy15"/>
          <serviceDebug includeExceptionDetailInFaults="true" /> 
        </behavior>
        <behavior name="workerServiceBehavior">
          <serviceMetadata policyVersion="Policy15"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

What I don't understand is that, as you can see, I have defined a ManagerService, and the application can connect to it without any problems. It doesn't seem to be a protocol problem, I'm getting the same result with wsHttpBinding.

What am I doing wrong?

Upvotes: 0

Views: 377

Answers (1)

Toan Nguyen
Toan Nguyen

Reputation: 11601

The reason you saw your app was frozen because of a thread deadlock, which means that two threads lock each other when executing a portion of code/ accessing a common resource.

In this case:

On the client side, when the worker proxy thread calls Test(0), it will wait for the response to come back.

On the "server side", the server thread at the same time tries to take control and return the result. Because of thread affinity between the two threads, the server thread will wait until the worker thread finishes, but the worker thread is waiting for the response, so none of them could go further, and deadlock happens.

Upvotes: 2

Related Questions