簡體   English   中英

使用WCF服務拒絕連接

[英]Getting Connection Refused with WCF Service

我使用WCF進行了服務,但無法連接到該服務,但不斷出現以下錯誤:

無法連接到net.tcp:// localhost:5555 / ControlChannel。 連接嘗試持續時間為00:00:02.0139182。 TCP錯誤代碼10061:無法建立連接,因為目標計算機主動拒絕它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>

我究竟做錯了什么? 是代碼問題還是我應該開始在其他地方尋找問題?

只是預感,但我懷疑沒有人在聽,因為即使托管您的服務的應用程序可能仍在運行,但服務主機卻沒有。 在您的構造函數中:

public MainForm()
{
    InitializeComponent();

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

您將ServiceHost放在using塊中ServiceHost塊退出后(就在構造函數完成之前), ServiceHost將被關閉並處置。

將您的代碼更改為此:

public MainForm()
{
    InitializeComponent();

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

您可以掛鈎到應用程序關閉事件,以在程序運行結束時關閉ServiceHost

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM