简体   繁体   中英

Connecting C++ Application to WCF Duplex Service

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, ie 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!

Maybe you can create a managed C++ library which communicate with the C# library using WCF, and your native C++ library will use this managed lib as proxy. A managed C++ library is the easy way to communicate a C++ library with C# using WCF.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM