我使用 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();
}
回调合约:
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>
我究竟做错了什么?是代码问题还是我应该开始在其他地方寻找问题?