Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取与WCF服务的连接被拒绝_C#_.net_Wcf_Web Services - Fatal编程技术网

C# 获取与WCF服务的连接被拒绝

C# 获取与WCF服务的连接被拒绝,c#,.net,wcf,web-services,C#,.net,Wcf,Web Services,我使用WCF创建了一个服务,但无法连接到该服务,我不断收到以下错误: 无法连接到网络。tcp://localhost:5555/ControlChannel. 这个 连接尝试持续的时间跨度为00:00:02.0139182。传输控制协议 错误代码10061:无法建立连接,因为目标 机器主动拒绝它127.0.0.1:5555 代码如下: 合同: [ServiceContract(CallbackContract = typeof(IControlChannelCallback))] public

我使用WCF创建了一个服务,但无法连接到该服务,我不断收到以下错误:

无法连接到网络。tcp://localhost:5555/ControlChannel. 这个 连接尝试持续的时间跨度为00:00:02.0139182。传输控制协议 错误代码10061:无法建立连接,因为目标 机器主动拒绝它127.0.0.1:5555

代码如下:

合同:

[ServiceContract(CallbackContract = typeof(IControlChannelCallback))]
public interface IControlChannel
{
    [OperationContract]
    void Join();
}
回拨合同:

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

     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块中-一旦using块在构造函数完成之前退出,ServiceHost将被关闭并释放

将代码更改为:

public MainForm()
{
    InitializeComponent();

    var host = new ServiceHost(typeof(ControlChannel))
    host.Open();
}
您可以钩住应用程序关闭事件,在程序运行结束时关闭ServiceHost


我还建议包装主机。在try-catch块中打开,以防尝试打开时出错。

只是一种预感,但我怀疑没有人监听,因为即使承载服务的应用程序可能仍在运行,服务主机也没有。在构造函数中:

public MainForm()
{
    InitializeComponent();

    using (var host = new ServiceHost(typeof(ControlChannel)))
    {
        host.Open();
    }
}
您的ServiceHost位于using块中-一旦using块在构造函数完成之前退出,ServiceHost将被关闭并释放

将代码更改为:

public MainForm()
{
    InitializeComponent();

    var host = new ServiceHost(typeof(ControlChannel))
    host.Open();
}
您可以钩住应用程序关闭事件,在程序运行结束时关闭ServiceHost


我还建议包装主机。在try-catch块中打开,以防尝试打开时出错。

我怀疑这是根本原因,但为什么服务合同的实现类标记为sealed?主机的其余部分在哪里?您确定客户端连接时应用程序仍在运行吗?在命令提示符下尝试netstat-n-a | findstr 5555,查看您的主机是否仍然存在。是否打开日志记录以查看它是否会提供更多信息?很明显5555上没有人在听。怎么会?服务器根本不会抛出任何类型的异常。您的服务主机位于一个using块中,打开它后什么都没有,因此在构造函数退出后,服务主机关闭。我怀疑这是根本原因,但为什么您的服务契约的实现类被标记为sealed?您的主机的其余部分在哪里?您确定客户端连接时应用程序仍在运行吗?在命令提示符下尝试netstat-n-a | findstr 5555,查看您的主机是否仍然存在。是否打开日志记录以查看它是否会提供更多信息?很明显5555上没有人在听。怎么会?服务器根本不会抛出任何类型的异常。你的服务主机在使用块中,打开后什么都没有,所以构造函数退出后,服务主机就关闭了。天哪,我觉得自己太蠢了。。。谢谢我在使用中设置了一个Console.ReadLine,现在它工作了。天哪,我觉得自己太愚蠢了。。。谢谢我在using中设置了一个Console.ReadLine,现在它可以工作了。