使用回调将事件传递给WCF客户端 [英] Using a Callback to pass an Event to a WCF Client

查看:115
本文介绍了使用回调将事件传递给WCF客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我试图创建一个event,该回调将通过从回调内部调用OnNotification(...)方法来触发.但是,不能从Callback方法中调用它,我不确定为什么.

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.

这是我的客户端库,用于连接到WCF服务:

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(...)不能在Callback方法中调用.

OnNotification(...) cannot be called in the Callback method.

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

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

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

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?

谢谢.

推荐答案

我想我明白了.我只需要将DCCClient参考传递给回调,然后从中调用OnNotification().

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

在DCC_Client中:

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

这篇关于使用回调将事件传递给WCF客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆