简体   繁体   English

将C ++应用程序连接到WCF双工服务

[英]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. 我有一个具有net.TCP绑定的WCF双工服务,两个客户端应用程序通过该服务进行连接,可以说客户端A和客户端B连接在一起。 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. 客户端A从某个来源获取有关Tick的数据,并通过调用方法通知双工服务,该方法通过将数据通过回调通道回调给客户端B来将数据传递给客户端B。

Here is the Contract Interface of my WCF Duplex Service: 这是我的WCF双工服务的合同接口:

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. 在上面的代码中,客户端B首先通过调用RegisterClients(String ClientName)方法与双工服务建立回调通道。 Client A calls NotifyServer(EventDataType eventData) method on each data tick which in return sends eventData to the client B through the callback channel. 客户端A在每个数据滴答上调用NotifyServer(EventDataType eventData)方法,该方法随后通过回调通道将eventData发送到客户端B。 Both of my client applications, Client A and Client B are developed on C# and the system is working perfectly fine. 我的两个客户端应用程序(客户端A和客户端B)均使用C#开发,并且系统运行良好。 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. 但是,我希望有一个C ++应用程序来替换客户端A,即我的C ++应用程序正在获取有关时间滴答的数据,并且我想通过与C#客户端A应用程序相同的过程将数据传递给客户端B。 。 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? 是否有可能在C ++应用程序中使用具有netTCP绑定的WCF服务,并通过WCF服务的Client对象调用该服务的NotifyServer(EventDataType eventData)方法? 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. 也许您可以创建一个托管的C ++库,并使用WCF与C#库进行通信,并且您的本机C ++库将使用此托管库作为代理。 A managed C++ library is the easy way to communicate a C++ library with C# using WCF. 托管C ++库是使用WCF与C#通信C ++库的简单方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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