I have a WCF Duplex Service having net.TCP Binding with which two client Applications, lets say Client A and Client B are connected. Client A gets data with respect to Time Tick from some source and notifies the Duplex Service by calling a method which in return passes that data to the Client B by calling back a method to the client B through a call back channel.
Here is the Contract Interface of my WCF Duplex Service:
namespace BroadcastorService
{
[ServiceContract(CallbackContract = typeof(IBroadcastorCallBack))]
public interface IBroadcastorService
{
[OperationContract(IsOneWay = true)]
void RegisterClients(string clientName);
[OperationContract(IsOneWay = true)]
void NotifyServer(EventDataType eventData);
}
public interface IBroadcastorCallBack
{
[OperationContract(IsOneWay = true)]
void BroadCastToClient(EventDataType eventData);
}
[DataContract]
public class EventDataType
{
[DataMember]
public string ClientName { get; set; }
[DataMember]
public string Data { get; set; }
}
}
}
Following is the code of the Service Class which inherits the contract interface:
namespace BroadcastorService
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class BroadcastorService : IBroadcastorService
{
private static Dictionary<string, IBroadcastorCallBack> clients = new Dictionary<string, IBroadcastorCallBack>();
private static object locker = new object();
public void NotifyServer(EventDataType eventData)
{
lock(locker)
{
var InactiveClients = new List<string>();
foreach(var client in clients)
{
if(client.Key != eventData.ClientName)
{
try
{
if(client.Key == eventData.symbol)
{
client.Value.BroadCastToClient(eventData);
}
}
catch
{
InactiveClients.Add(client.Key);
}
}
}
if(InactiveClients.Count > 0)
{
foreach(var client in InactiveClients)
{
clients.Remove(client);
}
}
}
}
public void RegisterClients(string clientName)
{
if(clientName != null && clientName != "")
{
try
{
IBroadcastorCallBack callback = OperationContext.Current.GetCallbackChannel<IBroadcastorCallBack>();
lock(locker)
{
if(clients.Keys.Contains(clientName))
{
clients.Remove(clientName);
}
clients.Add(clientName, callback);
}
}
catch(Exception ex)
{
}
}
}
}
}
In the above code, Client B first establishes a callback channel with the Duplex Service by calling the RegisterClients(String ClientName) method. Client A calls NotifyServer(EventDataType eventData) method on each data tick which in return sends eventData to the client B through the callback channel. Both of my client applications, Client A and Client B are developed on C# and the system is working perfectly fine. However, I would like to have a C++ application to replace Client A, i.e my C++ application is getting data with respect to time tick and I would like to pass the data to the client B by the same process as that of C# Client A application. Is there any possibility of consuming a WCF service having netTCP binding in a C++ Application and calling NotifyServer(EventDataType eventData) method of the service through the WCF Service's Client object? If yes, then please share it with me.
Thank you!