2011-06-07 43 views
0

我想让我的WCF客户端从回调中接收信息。我创建了一个客户端库,任何WCF客户端都可以使用它来连接到我的WCF服务。我不确定是否应该在客户端库或WCF客户端中实现回调。使用回调将事件传递给WCF客户端

我试图创建一个event,它将通过在回调中调用OnNotification(...)方法来解雇。但是,它不能从回调方法中调用,我不知道为什么。

下面是用于连接到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(...)不能在回调方法被调用。

下面是一个例子我怎么我的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客户端中完成?

预先感谢您。

回答

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 
     } 
    } 
相关问题