gpt4 book ai didi

c# - 使用 WCF 服务拒绝连接

转载 作者:太空宇宙 更新时间:2023-11-03 18:33:24 24 4
gpt4 key购买 nike

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

这是代码:

契约(Contract):

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

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 block 中 - 一旦 using block 退出(就在构造函数完成之前),ServiceHost 将是关闭并处置。

将您的代码更改为:

public MainForm()
{
InitializeComponent();

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

您可以挂接到应用程序关闭事件以在程序运行结束时关闭 ServiceHost

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

关于c# - 使用 WCF 服务拒绝连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19084876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com