使用WCF服务拒绝连接

时间:2013-09-30 00:29:18

标签: c# .net wcf web-services

我使用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>

我做错了什么?这是一个代码问题还是我应该开始在其他地方寻找问题?

1 个答案:

答案 0 :(得分:7)

只是预感,但我怀疑没有什么可听的,因为即使主持您的服务的应用程序可能仍在运行,服务主机也不会。在你的构造函数中:

public MainForm()
{
    InitializeComponent();

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

您在ServiceHost块中有using - 一旦使用块退出(在构造函数完成之前),ServiceHost将被关闭并被丢弃。

将你的代码改为此:

public MainForm()
{
    InitializeComponent();

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

您可以挂钩应用程序关闭事件,以便在程序运行结束时关闭ServiceHost

我还建议将host.Open()包装在try-catch块中,以防尝试打开它时出现问题。