2013-09-30 143 views
1

我使用WCF做了服務,並不能連接到它,我不斷收到以下錯誤:獲取連接拒絕與WCF服務

Could not connect to net.tcp://localhost:5555/ControlChannel. The connection attempt lasted for a time span of 00:00:02.0139182. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:5555.

這是代碼:

合同:

[ServiceContract(CallbackContract = typeof(IControlChannelCallback))] 
public interface IControlChannel 
{ 
    [OperationContract] 
    void Join(); 
} 

CallBackContract:

public interface IControlChannelCallback 
{ 
    [OperationContract(IsOneWay = true)] 
    void ShowMessage(string message); 
} 

服務:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)] 
public sealed class ControlChannel : IControlChannel 
{ 
    public static readonly IList<IControlChannelCallback> Callbacks = new List<IControlChannelCallback>(); 

    public void Join() 
    { 
     var client = OperationContext.Current.GetCallbackChannel<IControlChannelCallback>(); 

     if (!Callbacks.Contains(client)) 
      Callbacks.Add(client); 

     client.ShowMessage("This message came from the server"); 
    } 
} 

服務器:

public MainForm() 
{ 
    InitializeComponent(); 

    using (var host = new ServiceHost(typeof(ControlChannel))) 
    { 
      host.Open(); 
    } 
} 

客戶:

public MainForm() 
{ 
    InitializeComponent(); 

    var callback = new ControlChannelCallbackClient(); 

    using (var factory = new DuplexChannelFactory<IControlChannel>(callback, "Client")) 
    { 
      var proxy = factory.CreateChannel(); 

      proxy.Join(); 
    } 
} 

的App.config客戶端:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 
    <system.serviceModel> 
     <client> 
     <endpoint name="Client" 
        contract="Service.Contracts.Interfaces.IControlChannel" 
        binding="netTcpBinding" 
        address="net.tcp://localhost:5555/ControlChannel" /> 
     </client> 
    </system.serviceModel> 
</configuration> 

的App.config服務器

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 
    <system.serviceModel> 
     <services> 
     <service name="Service.Contracts.Objects.ControlChannel"> 
      <endpoint contract="Service.Contracts.Interfaces.IControlChannel" 
        binding="netTcpBinding" 
        address="net.tcp://localhost:5555/ControlChannel" > 
      </endpoint> 
     </service> 
     </services> 
    </system.serviceModel> 
</configuration> 

我在做什麼錯?這是代碼問題還是我應該開始在其他地方尋找問題?

+0

我懷疑這是根本原因,但爲什麼您的服務合同的實施類標記爲「密封」? – Tim

+1

你的主機的其餘部分在哪裏?您確定應用程序在客戶端連接時仍在運行嗎?試試'netstat -n -a |從命令提示符找到「5555」',看看你的主機是否仍然在那裏。 –

+0

你打開日誌記錄,看看它是否會提供一些更多的信息? http://msdn.microsoft.com/en-us/library/ms730064.aspx – jcwrequests

回答

6

只是一個預感,但我懷疑沒有聽,因爲即使承載您的服務的應用程序可能仍在運行,服務主機不是。在你的構造:

public MainForm() 
{ 
    InitializeComponent(); 

    using (var host = new ServiceHost(typeof(ControlChannel))) 
    { 
     host.Open(); 
    } 
} 

你有你的usingServiceHost塊 - 一旦使用塊退出(構造函數中完成右)之前,將ServiceHost將被關閉和處置。

長安您的代碼如下:

public MainForm() 
{ 
    InitializeComponent(); 

    var host = new ServiceHost(typeof(ControlChannel)) 
    host.Open(); 
} 

你可以連接到應用程序關閉事件關閉ServiceHost在程序運行結束。

我還建議將host.Open()包裝在try-catch塊中,以防萬一出現錯誤嘗試打開它。

+0

耶穌基督,我覺得很愚蠢......謝謝。我在使用中設置了一個Console.ReadLine(),現在它工作。 – Unicco