2012-03-12 118 views
0

這是我遇到的問題,我完全無能爲力。我已經定義了一個IWorkerServiceContract類,如下所示:服務代理凍結應用程序

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

沒什麼特別的,我只是想測試連通性。這裏是我的服務類:

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; 
    } 
} 

的Logger對象由初始化類實例化,它基本上記錄到()與AllocConsole分配的控制檯。當我調用Listen()時,一切正常,我可以通過WCF測試客戶端連接並遠程調用Test()方法。雖然,當我定義代理:

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); 
    } 
} 

,並使用下面的代碼:

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

申請凍結!這是我的app.config文件:

<?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> 

我不明白的是,你可以看到,我已經定義了一個ManagerService,應用程序可以連接到它沒有任何問題。它似乎不是一個協議問題,我得到了與wsHttpBinding相同的結果。

我在做什麼錯?

回答

2

由於線程死鎖,您看到您的應用程序的原因被凍結,這意味着當執行一部分代碼/訪問公共資源時,兩個線程互相鎖定。

在這種情況下:

在客戶端,當工人代理線程調用Test(0),它會等待響應回來。

在「服務器端」,服務器線程同時嘗試控制並返回結果。由於兩個線程之間存在線程關聯,服務器線程將等待工作線程完成,但工作線程正在等待響應,因此它們都不能繼續,並且發生死鎖。

+0

謝謝,我完全忽略了這個問題!我可以連接到ManagerService,因爲我製作了一個在單獨的線程上運行的異步代理。 – silverhx 2012-03-13 11:43:25