我正在尝试让我的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?
}
}
}
无法在Callback方法中调用 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客户端中完成?
提前谢谢。
答案 0 :(得分: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
}
}