使用回调将事件传递给WCF客户端



我正试图让我的WCF客户端从回调中接收信息。我已经创建了一个客户端库,任何WCF客户端都可以使用它来连接到我的WCF服务。我不确定是应该在客户端库中实现回调,还是在WCF客户端本身中实现回调。

我已经尝试创建一个event,它将通过从回调中调用OnNotification(...)方法来激发。然而,它不能从回调方法中调用,我不知道为什么。

这是我用来连接WCF服务的客户端库:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;  //needed for WCF communication
namespace DCC_Client
{
    public class DCCClient
    {
        private DuplexChannelFactory<ServiceReference1.IDCCService> dualFactory;
        public ServiceReference1.IDCCService Proxy;
        public DCCClient()
        {
            //Setup the duplex channel to the service...
            NetNamedPipeBinding binding = new NetNamedPipeBinding();
            dualFactory = new DuplexChannelFactory<ServiceReference1.IDCCService>(new Callbacks(), binding, new EndpointAddress("net.pipe://localhost/DCCService"));
        }
        public void Open()
        {
            Proxy = dualFactory.CreateChannel();
        }
        public void Close()
        {
            dualFactory.Close();
        }
        /// <summary>
        /// Event fired an event is recieved from the DCC Service
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnNotification(EventArgs e)
        {
            if (Notification != null)
            {
                Notification(this, e);
            }
        }
    }
    public class Callbacks : ServiceReference1.IDCCServiceCallback
    {
        void ServiceReference1.IDCCServiceCallback.OnCallback(string id, string message, Guid key)
        {
            //Can't call OnNotification(...) here?
        }
    }
}

无法在回调方法中调用OnNotification(...)

以下是我的WCF客户端如何使用EventHandler实现的示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DCC_Client;
namespace Client_Console_Test
{
    class Program
    {
        private static DCCClient DCCClient;
        static void Main(string[] args)
        {
            try
            {
                DCCClient = new DCCClient();
                DCCClient.Notification += new EventHandler(DCCClient_Notification);
                DCCClient.Open();
                DCCClient.Proxy.DCCInitialize();
                Console.ReadLine();
                DCCClient.Proxy.DCCUninitialize();
                DCCClient.Close();
            }
            catch (Exception e)
            {
                DCCClient.Log.Error(e.Message);
            }
        }
        static void DCCClient_Notification(object sender, EventArgs e)
        {
            //Do something with this event
        }
    }
}

这是将回调信息传递给WCF客户端的正确方式吗?我觉得添加EventHandler是多余的,我应该只使用回调本身。我在客户端库中实现回调是正确的,还是应该在每个WCF客户端中实现回调?

提前谢谢。

我想我想通了。我只需要将DCCClient引用传递给回调,然后从中调用OnNotification()

在DCC_Client:中

public class DCCClient
{
    private DuplexChannelFactory<ServiceReference1.IDCCService> dualFactory;
    private Callbacks notificationCallback; //Add callback object here
    public ServiceReference1.IDCCService Proxy;
    public DCCClient()
    {
        //Setup the duplex channel to the service...
        NetNamedPipeBinding binding = new NetNamedPipeBinding();
        notificationCallback = new Callbacks(this); //Pass DCCClient reference here
        dualFactory = new DuplexChannelFactory<ServiceReference1.IDCCService>(notificationCallback, binding, new EndpointAddress("net.pipe://localhost/DCCService"));
    }
    //....
    public class Callbacks : ServiceReference1.IDCCServiceCallback
    {
        private DCCClient client;
        public Callbacks(DCCClient client)
        {
            this.client = client; //grab client refernce
        }
        void ServiceReference1.IDCCServiceCallback.OnCallback(string id, string message, Guid key)
        {
            client.OnNotification(n); //send the event here
        }
    }

最新更新