Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

Using a Callback to pass an Event to a WCF Client

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...