I created WCF service and client for it. Firstly, the client calls the
Register() method which adds client's callback channel to
clientChannels.
When client calls
SendMessage(), server sends received data to other registered clients using
ShowMessage() method of client callback.
InstanceContextMode of service is
Single.
Service host is a console application. It works normally, but client is blocked after
Register() calling and doesn't output anything. After a while it throws
EndPointNotFoundException:
There was no endpoint listening at http://localhost:8733/Design_Time_Addresses/WCFDuplexServiceTest/Service1/ that could accept the message.
How to fix it?
Config file of host application:
<?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>
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Config file of client application:
<?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>
Contracts:
using System.ServiceModel;
namespace WCFDuplexServiceTest
{
// Server methods.
[ServiceContract(SessionMode = SessionMode.Allowed, CallbackContract = typeof(IClientCallback))]
public interface IMessageService
{
[OperationContract(IsOneWay = true)]
void Register();
[OperationContract(IsOneWay = true)]
void SendMessage(string message);
}
// Client methods.
public interface IClientCallback
{
[OperationContract(IsOneWay = true)]
void ShowMessage(string message);
}
}
Implementation:
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));
}
}
}
Client:
using System;
using WCFTestServiceClient.MessageService;
using System.ServiceModel;
namespace WCFTestServiceClient
{
class Program
{
static void Main(string[] args)
{
using (MessageServiceClient client = new MessageServiceClient(new InstanceContext(new MessageServiceHandler())))
{
client.Register();
client.SendMessage("How many people online?");
}
}
}
public class MessageServiceHandler : IMessageServiceCallback
{
public void ShowMessage(string message)
{
Console.WriteLine("Server response: " + message);
}
}
}
What I have tried:
I tried to change
InstanceContextMode of service implementation and
SessionMode of service contract, but it didn't help.