The main goal is to let users send message to the host. The host will think for two seconds, and then with DUAL, will send a message back. This is working fine for me.
The thing is, for every user who send a message, I’m subscribing him to a list. The thing I’m trying to accomplish is, if the user Console.Readline() == "b" ( brodadcast ), send all the subscribers "Hello".
But the list of subscribers is at the service, and the Console.Readline() is at the host.
The host:
static void Main(string[] args)
{
ServiceHost duplex = new ServiceHost(typeof (ServiceClass));
duplex.Open();
Console.WriteLine("Press 'b' To Broadcast All subscibers : Hello");
if (Console.ReadLine()=="b")
{
foreach (var registered in lstOfRegisteredUsers) //<== I cant access lstOfRegisteredUsers because its on the Service Class.
{
registered.SendBack("Hello");
}
}
Console.WriteLine("Host is running, press <ENTER> to exit.");
Console.ReadLine();
}
The service:
public class ServiceClass : ISend
{
public List<ISendBack> lstOfRegisteredUsers = new List<ISendBack>();
public void Send(string data)
{
ISendBack callback = OperationContext.Current.GetCallbackChannel<ISendBack>();
lstOfRegisteredUsers.Add(callback); // <== here i'm adding subscribers for future broadcast " hello".
Console.WriteLine("goind to process " + data);
System.Threading.Thread.Sleep(2000);
callback.SendBack("done " +data);
}
}
How can I send from the host, to each of the subscribers: “hello”?
If I understand correctly then you wish to broadcast a message to each of your service’s callback clients if a key-press input is received into the console application hosting your service?
To do this you need to be able to call into your service instance from your host application.
This can be achieved by creating an instance of your service class within your host. Then when you create your ServiceHost you pass the instance into the ServiceHost constructor. Then in your service you have a method which does the actual callback and you can call it.
For example: