0

WCFクライアントにコールバックから情報を受信させようとしています。すべてのWCFクライアントがWCFサービスへの接続に使用できるクライアントライブラリを作成しました。コールバックをクライアントライブラリに実装する必要があるのか​​、WCFクライアント自体に実装する必要があるのか​​わかりません。

eventコールバック内からメソッドを呼び出すことによって起動されるを作成しようとしましたOnNotification(...)。ただし、Callbackメソッド内から呼び出すことはできず、理由はわかりません。

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(...)コールバックメソッドで呼び出すことはできません。

これは、EventHandlerを使用してWCFクライアントを実装する方法の例です。

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クライアントでこれを実行する必要がありますか?

前もって感謝します。

4

1 に答える 1

1

私はそれを理解したと思います。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
        }
    }
于 2011-06-08T20:44:53.963 に答える