自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

WCF事件通知具體實(shí)現(xiàn)方法詳解

開發(fā) 開發(fā)工具
WCF事件通知的實(shí)現(xiàn)方法可以通過本文介紹的相關(guān)方法來實(shí)現(xiàn),主要就是通過使用觀察者模式來進(jìn)行操作。下面將會對此一一做一個介紹。

WCF開發(fā)工具是由微軟公司推出的一款功能強(qiáng)大的開發(fā)插件,它在實(shí)際應(yīng)用中可以為我們提供許多有用的幫助。在這里我們將會為大家詳細(xì)介紹一下WCF事件通知的相關(guān)實(shí)現(xiàn)方法,以方便大家掌握這方面的應(yīng)用技巧。#t#

看了一些WCF的例子,實(shí)現(xiàn)WCF事件通知使用的是多播委托的特性,有點(diǎn)復(fù)雜,操作起來也不是很直觀,看到一堆委托和事件我一般頭就暈了。下面介紹一種使用觀察者模式實(shí)現(xiàn)事件通知的簡單方法。沒別的,就是簡單,簡單最美。

工程代碼如下:

1.定義接口

  1. [ServiceContract(SessionModeSessionMode = SessionMode.Required, 
    CallbackContract = typeof(IWriteLogCallback))]  
  2. public interface ILogService  
  3. {  
  4. [OperationContract(IsInitiating = trueIsTerminating = false)]  
  5. void Write(string logMsg);  
  6. [OperationContract(IsInitiating = trueIsTerminating = false)]  
  7. void RegisterListener();  
  8. [OperationContract(IsInitiating = falseIsTerminating = false)]  
  9. void UnregisterListener();  
  10. }  
  11. [ServiceContract]  
  12. public interface IWriteLogCallback  
  13. {  
  14. [OperationContract(IsOneWay = true)]  
  15. void OnWriteLog(string logMsg);  

為了簡單舉了一個寫日志的例子, Write(string logMsg)就是寫入日志的方法,參數(shù)logMsg是需要寫入的日志信息。當(dāng)客戶單沒有調(diào)用RegisterListener()訂閱事件的時候,是不會收到寫日志的WCF事件通知的,相應(yīng)的要獲得寫日志的事件通知,就需要調(diào)用RegisterListener()方法。如果要取消訂閱就調(diào)用UnregisterListener()方法。寫日志的功能和事件的訂閱功能是分開的。

2.服務(wù)實(shí)現(xiàn)

  1. [ServiceBehavior(  
  2. IncludeExceptionDetailInFaults = true,  
  3. InstanceContextModeInstanceContextMode = InstanceContextMode.Single,  
  4. ConcurrencyModeConcurrencyMode = ConcurrencyMode.Multiple)]  
  5. class LogService:ILogService  
  6. {  
  7. public LogService()  
  8. {  
  9. Trace.WriteLine("Create LogService Instance.");  
  10. }  
  11. Dictionary<string, OperationContext> listeners = new Dictionary
    <string, OperationContext>();  
  12. private void BroadCast(string logMsg)  
  13. {  
  14. List<string> errorClints = new List<string>();  
  15. foreach (KeyValuePair<string, OperationContext> listener in listeners)  
  16. {  
  17. try  
  18. {  
  19. listener.Value.GetCallbackChannel<IWriteLogCallback>().OnWriteLog(logMsg);  
  20. }  
  21. catch (System.Exception e)  
  22. {  
  23. errorClints.Add(listener.Key);  
  24. Trace.WriteLine("BROAD EVENT ERROR:" + e.Message);  
  25. }  
  26. }  
  27. foreach (string id in errorClints)  
  28. {  
  29. listeners.Remove(id);  
  30. }  
  31. }  
  32. #region ILogService 成員  
  33. public void Write(string logMsg)  
  34. {  
  35. Trace.WriteLine("Write LOG:"+logMsg);  
  36. BroadCast(logMsg);  
  37. }  
  38. public void RegisterListener()  
  39. {  
  40. listeners.Add(OperationContext.Current.SessionId, OperationContext.Current);  
  41. Trace.WriteLine("SessionID:" + OperationContext.Current.SessionId);  
  42. Trace.WriteLine("Register listener. Client Count:" + 
    listeners.Count.ToString());  
  43. }  
  44. public void UnregisterListener()  
  45. {  
  46. listeners.Remove(OperationContext.Current.SessionId);  
  47. Trace.WriteLine("SessionID:" + OperationContext.Current.SessionId);  
  48. Trace.WriteLine("Unregister listener. Client Count:" + 
    listeners.Count.ToString());  
  49. }  
  50. #endregion  
  51. } Dictionary<string, OperationContext> listeners 

包含了所有的事件訂閱者。發(fā)布WCF事件通知的時候,如果調(diào)用訂閱者的回調(diào)函數(shù)失敗,就把該訂閱者從listeners移除。代碼很簡單,就不多說了。

3.客戶端訪問

定義回調(diào)的客戶端:

  1. class LogClient:IWriteLogCallback  
  2. {  
  3. #region IWriteLogCallback 成員  
  4. public void OnWriteLog(string logMsg)  
  5. {  
  6. Trace.WriteLine("RECV LOG EVENT:" + logMsg);  
  7. }  
  8. #endregion  

然后在程序中使用它:

  1. class Program  
  2. {  
  3. static void Main(string[] args)  
  4. {  
  5. Trace.Listeners.Add(new ConsoleTraceListener());  
  6. LogClient client = new LogClient();  
  7. ILogService service = DuplexChannelFactory<ILogService>
    .CreateChannel(client,  
  8. new WSDualHttpBinding(), new EndpointAddress
    ("http://localhost:8888/log"));  
  9. //訂閱消息  
  10. service.RegisterListener();  
  11. service.Write("Client start");  
  12. Console.WriteLine("Press enter key to exit.");  
  13. Console.ReadLine();  
  14. service.UnregisterListener();  

WCF事件通知實(shí)現(xiàn)時需要注意的問題:

A. 因?yàn)榭蛻粢惨O(jiān)聽端口,所以確保防火墻沒有對它進(jìn)行阻止。

B. 這里使用的是單實(shí)例的服務(wù),所以需要進(jìn)行多進(jìn)程訪問的保護(hù),才能實(shí)際使用。

責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2009-12-21 18:10:50

WCF實(shí)現(xiàn)事件通知

2010-02-24 16:30:52

WCF常見錯誤

2010-02-25 09:58:05

WCF配置指定Addr

2010-02-26 15:53:35

WCF套接字連接中斷

2010-02-24 16:39:27

WCF客戶端處理

2010-02-26 11:22:16

LitwareHR使用

2009-12-21 16:04:45

WCF Dispose

2010-02-22 17:34:39

WCF依賴屬性

2010-02-26 16:20:56

WCF程序事務(wù)

2010-02-24 09:38:58

WCF應(yīng)用編碼

2009-12-21 18:32:22

關(guān)閉WCF鏈接

2009-12-22 16:36:38

WCF重載

2010-02-04 11:23:25

C++反射機(jī)制

2009-12-28 16:10:38

WPF生成文件

2009-12-30 14:36:29

Silverlight

2010-02-25 16:20:02

WCF客戶端

2010-02-25 13:54:48

WCF安全參數(shù)

2010-03-02 17:35:20

WCF服務(wù)加載

2010-02-25 11:23:29

WCF返回自定義格式

2009-12-21 17:48:30

WCF方法重載
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號