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クライアントでこれを実行する必要がありますか?
前もって感謝します。