0

I am building a simple duplex wcf service. In this service clients send messages to the server and the server distributes the message to all connected clients. However, despite the fact that I defined the ServiceBehavior attribute as [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)], only the client who sent the message receives it back from the server, while the other clients do not. I verified that there is just one instance of the server running. What did I do wrong? I looked at other similar questions on the web, and they all say that I should define InstanceContextMode = InstanceContextMode.Single, which I already did.

user207809
  • 61
  • 1
  • 5
  • show us how you are sending the message back. Make sure you send it to all connected clients. You could have them call a register method and keep a list on the server and loop the list. – Emond Jun 07 '15 at 10:41
  • Obviously SignalR is just for such cases. It would be too difficult for WCF. – Lex Li Jun 07 '15 at 14:42
  • Here is how I am sending the messages: IMessageServiceCallback callback = OperationContext.Current.GetCallbackChannel(); callback.SendMessage(message); I also tried to have separate callback object for each user and loop over all these objects, but it does not work either. – user207809 Jun 08 '15 at 04:26
  • Do not add code as a comment. Edit your post and add it there./ – tom redfern Jun 08 '15 at 08:14

1 Answers1

1

Do you have a callback Contract. So that server will reply back to client. Check the below tutorial for Implementing Callback Contract Click here

Also check the below Project Event Notification server. This project is doing similar things what you want.

CodeProject Link

Feel free to ask me if you need any more clarification

You need to maintain the clistList as shown in the code snippet.

List<IMessageServiceCallback> clientList = new List<IMessageServiceCallback>();
public void Register()
{ 
    IMessageServiceCallback callback = OperationContext.Current.GetCallbackChannel<IMessageServiceCallback>(); 
    clientList.add(callback);
}

When you want to broadcast this message. You can iterate through the list and call the callback function to send message to clients.

Kalidoss M
  • 556
  • 8
  • 15
  • of course I defined a contract. Here it is: [ServiceContract(CallbackContract=typeof(IMessageServiceCallback))] public interface IMessageService { [OperationContract(IsOneWay = true)] void SendMessage(string s); } public interface IMessageServiceCallback { [OperationContract(IsOneWay = true)] void SendMessage(string s); } – user207809 Jun 08 '15 at 04:46
  • Do you maintain a instancecontext of all clients in a list or array? List clientList = new List(); public void Register() { IMessageServiceCallback callback = OperationContext.Current.GetCallbackChannel(); clientList.add(callback); } – Kalidoss M Jun 08 '15 at 05:56
  • Thanks a lot. I looked at the links and I manage to implement the service I wanted. In short, I need to have a callback object for each connected client. Then, I loop over all these objects in order to send messages to all the connected clients. – user207809 Jun 08 '15 at 06:35
  • @user207809 - consider awarding this as the answer if it helped you arrive at a solution. Also, don't post code in comments. Consider adding the code to your question using an edit. – tom redfern Jun 08 '15 at 08:12
  • @kalidoss Do not post code in comment. Instead edit your answer to include the comment. – tom redfern Jun 08 '15 at 08:13