我为它创建了WCF服务和客户端。首先,客户端调用Register()
方法,该方法将客户端的回调通道添加到clientChannels
。
当客户端调用SendMessage()
时,服务器使用ShowMessage()
客户端回调方法将接收到的数据发送到其他注册客户端。服务InstanceContextMode
为Single
。
服务主机是一个控制台应用程序。它工作正常,但客户端在Register()
调用后被阻止,并且不输出任何内容。过了一会儿,它会抛出EndPointNotFoundException
:
http://localhost:8733/Design_Time_Addresses/WCFDuplexServiceTest/Service1/没有可以接受该消息的端点。
如何解决?
主机应用程序的配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<system.serviceModel>
<services>
<service name="WCFDuplexServiceTest.MessageService">
<endpoint address="" binding="wsDualHttpBinding" contract="WCFDuplexServiceTest.IMessageService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/Design_Time_Addresses/WCFDuplexServiceTest/Service1/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!--Чтобы избежать раскрытия метаданных,
до развертывания задайте следующим параметрам значение "false".-->
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<!-- Чтобы получить сведения об исключениях в ошибках для отладки,
установите ниже значение TRUE. Перед развертыванием установите значение FALSE,
чтобы избежать разглашения сведений об исключении -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
&#13;
客户端应用程序的配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<system.serviceModel>
<bindings>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_IMessageService"
clientBaseAddress="http://localhost:8000/myClient/">
<security mode="None" />
</binding>
</wsDualHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8733/Design_Time_Addresses/WCFDuplexServiceTest/Service1/"
binding="wsDualHttpBinding" bindingConfiguration="WSDualHttpBinding_IMessageService"
contract="MessageService.IMessageService" name="WSDualHttpBinding_IMessageService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
&#13;
IMessageService
实施:
using System.Collections.Generic;
using System.ServiceModel;
namespace WCFDuplexServiceTest
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class MessageService : IMessageService
{
private List<IClientCallback> clientChannels = new List<IClientCallback>();
private int numberOfUsers = 0;
public MessageService() { }
public void Register()
{
clientChannels.Add(OperationContext.Current.GetCallbackChannel<IClientCallback>());
numberOfUsers++;
}
public void SendMessage(string message)
{
foreach (var channel in clientChannels)
channel.ShowMessage(message + string.Format("/nPeople online: {0}", numberOfUsers));
}
}
}