Using a Callback to pass an Event to a WCF Client

假如想象 提交于 2019-12-11 22:50:04

问题


I am trying to have my WCF client receive info from a callback. I have created a Client Library that any WCF Client can use to connect to my WCF Service. I am uncertain if I should implement the Callback in the Client Library or the WCF Client itself.

I have attempted to create an event that will be fired by calling the OnNotification(...) method from within the callback. However, it cannot be called from within the Callback method and I'm not sure why.

Here is my Client Library used to connect to the WCF Service:

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(...) cannot be called in the Callback method.

Here is an example of my how my WCF Client would be implemented using an 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
        }
    }
}

Is this the correct way to pass the callback info to my WCF Client? I feel like adding an EventHandler is redundant and I should just use the callback itself. Am I correct to have implemented the Callback in my Client Library, or should this be done in each WCF Client?

Thank you in advance.


回答1:


I think I figured it out. I simply need to pass the DCCClient reference to the callback, and then call OnNotification() from it.

In 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
        }
    }


来源:https://stackoverflow.com/questions/6270791/using-a-callback-to-pass-an-event-to-a-wcf-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!