简体   繁体   English

使用WCF服务拒绝连接

[英]Getting Connection Refused with WCF Service

I made an service using WCF and can't connect to it, I keep getting the following error: 我使用WCF进行了服务,但无法连接到该服务,但不断出现以下错误:

Could not connect to net.tcp://localhost:5555/ControlChannel. 无法连接到net.tcp:// localhost:5555 / ControlChannel。 The connection attempt lasted for a time span of 00:00:02.0139182. 连接尝试持续时间为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. TCP错误代码10061:无法建立连接,因为目标计算机主动拒绝它127.0.0.1:5555。

This is the code: 这是代码:

Contract: 合同:

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

CallBackContract: CallBackContract:

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

Service: 服务:

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

Server: 服务器:

public MainForm()
{
     InitializeComponent();

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

Client: 客户:

public MainForm()
{
     InitializeComponent();

     var callback = new ControlChannelCallbackClient();

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

          proxy.Join();
     }
 }

App.config client: 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 server 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>

What am I doing wrong? 我究竟做错了什么? Is it a code problem or should I start looking for problems elsewhere? 是代码问题还是我应该开始在其他地方寻找问题?

Just a hunch, but I suspect there's nothing listening because even though the application hosting your service may still be running, the service host isn't. 只是预感,但我怀疑没有人在听,因为即使托管您的服务的应用程序可能仍在运行,但服务主机却没有。 In your constructor: 在您的构造函数中:

public MainForm()
{
    InitializeComponent();

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

You have your ServiceHost in a using block - once the using block exits (right before the constructor finishes), the ServiceHost will be closed and disposed of. 您将ServiceHost放在using块中ServiceHost块退出后(就在构造函数完成之前), ServiceHost将被关闭并处置。

Chang your code to this: 将您的代码更改为此:

public MainForm()
{
    InitializeComponent();

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

You can hook into the application closing event to close the ServiceHost at the end of the program's run. 您可以挂钩到应用程序关闭事件,以在程序运行结束时关闭ServiceHost

I'd also recommend wrapping the host.Open() in a try-catch block in case something goes wrong trying to open it. 我还建议将host.Open()包装在try-catch块中,以防尝试打开它时出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM