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

關于WCF緩存機制介紹

開發(fā) 后端
這里就WCF緩存做出了詳細的案例分析,實現(xiàn)BehaviorExtensionElement,IEndpointBehavior將剛剛建立的行為加入Client行為集合,文章有詳細的代碼。

緩存是很占內存的,緩存也有它的好處,這里就WCF緩存機制分析一個案例,希望大家可以從中得到收獲。首先我們看看MSDN中對WCF的Session的說明:它們由調用應用程序顯式啟動和終止。會話期間傳遞的消息按照接收消息的順序進行處理。會話將一組消息相互關聯(lián),從而形成對話。該關聯(lián)的含義是抽象的。

#T#例如,一個基于會話的通道可能會根據(jù)共享網(wǎng)絡連接來關聯(lián)消息,而另一個基于會話的通道可能會根據(jù)消息正文中的共享標記來關聯(lián)消息??梢詮臅捙缮墓δ苋Q于關聯(lián)的性質。不存在與 WCF 會話相關聯(lián)的常規(guī)數(shù)據(jù)存儲區(qū)。***一句告訴我們,WCF中的Session是無法像Web應用一樣存儲附加信息的。經過研究,我們可以通過擴展MessageHeader實現(xiàn)一個附加的數(shù)據(jù)存儲區(qū)在Client端每次請求Service時發(fā)送到Server端。具體實現(xiàn)如下(以前述需求為例)。

這是一個單件類,Client正常登陸得到Server端回傳的UserIdentity實例后可以通過如下代碼將其存入WCF緩存:

  1. UserPermissionInfo.GetInstance().SetUserIdentity(ServerReturnedUserIdentity); 

其中ServerReturnedUserIdentity就是Server產生并回傳的UserIdentity下面我們擴展MessageHeader將我們自己定義的UserIdentity加入進去,WCF緩存代碼如下:

  1. usingSystem;  
  2. usingSystem.Collections.Generic;  
  3. usingSystem.Text;  
  4. usingSystem.ServiceModel;  
  5. usingSystem.ServiceProcess;  
  6. usingSystem.ServiceModel.Dispatcher;  
  7. usingSystem.ServiceModel.Description;  
  8. usingSystem.ServiceModel.Channels;  
  9. usingSystem.ServiceModel.Configuration;  
  10. namespaceBNCommon.ClientHelper  
  11. {  
  12. publicclassBNClientMessageInspector:IClientMessageInspector  
  13. {  
  14. IClientMessageInspector成員#regionIClientMessageInspector成員  
  15. publicvoidAfterReceiveReply(refMessagereply,objectcorrelationState)  
  16. {  
  17. }  
  18. publicobjectBeforeSendRequest(refMessagerequest,IClientChannelchannel)  
  19. {  
  20. MessageHeaderMessageHeadermh=MessageHeader.CreateHeader("UserIdentity","UINS",BNIIClientLayerPlus.UserPermissionInfo.GetInstance()._UserIdentity);  
  21. request.Headers.Add(mh);  
  22. returnnull;  
  23. }  
  24. #endregion  
  25. }  


這個類實現(xiàn)了IClientMessageInspector接口,實現(xiàn)該接口可以在Client每次向Server請求前及請求返回后控制Client的行為對發(fā)送和接收的數(shù)據(jù)進行處理?,F(xiàn)在我們需要實現(xiàn)BehaviorExtensionElement,IEndpointBehavior將剛剛建立的行為加入Client行為集合,代碼如下:

  1. usingSystem;  
  2. usingSystem.Collections.Generic;  
  3. usingSystem.Text;  
  4. usingSystem.ServiceModel;  
  5. usingSystem.ServiceProcess;  
  6. usingSystem.ServiceModel.Dispatcher;  
  7. usingSystem.ServiceModel.Description;  
  8. usingSystem.ServiceModel.Channels;  
  9. usingSystem.ServiceModel.Configuration;  
  10. namespaceBNCommon.ClientHelper  
  11. {  
  12. publicclassBNClientEndpointBehavior:BehaviorExtensionElement,IEndpointBehavior  
  13. {  
  14. IEndpointBehavior成員#regionIEndpointBehavior成員  
  15. publicvoidAddBindingParameters(ServiceEndpointendpoint,BindingParameterCollectionbindingParameters)  
  16. {}  
  17. publicvoidApplyClientBehavior(ServiceEndpointendpoint,ClientRuntimeclientRuntime)  
  18. {  
  19. clientRuntime.MessageInspectors.Add(newBNClientMessageInspector());  
  20. }  
  21. publicvoidApplyDispatchBehavior(ServiceEndpointendpoint,EndpointDispatcherendpointDispatcher)  
  22. {  
  23. }  
  24. publicvoidValidate(ServiceEndpointendpoint)  
  25. {  
  26. return;  
  27. }  
  28. #endregion  
  29. publicoverrideTypeBehaviorType  
  30. {  
  31. get...{returntypeof(BNClientEndpointBehavior);}  
  32. }  
  33. protectedoverrideobjectCreateBehavior()  
  34. {  
  35. returnnewBNClientEndpointBehavior();  
  36. }  
  37. }  


 

責任編輯:田樹 來源: 博客
相關推薦

2009-12-07 18:33:31

WCF Service

2009-11-09 13:47:22

WCF Stream操

2016-03-09 09:54:47

Python開發(fā)緩存機制

2010-02-26 13:34:50

WCF編碼機制

2010-03-01 17:57:11

WCF緩存機制

2009-11-09 14:15:17

WCF集合類型

2009-06-12 14:28:14

WCF傳輸安全

2010-02-23 09:51:32

WCF MTOM

2010-02-22 14:18:34

WCF服務驗證

2009-11-09 16:44:18

WCF Service

2009-11-09 13:04:53

WCF事物處理

2016-01-05 12:54:52

瀏覽器瀏覽器端緩存

2009-11-06 11:07:52

WCF事務屬性

2009-11-05 11:31:00

WCF綁定

2009-11-09 17:30:20

WCF元數(shù)據(jù)

2009-12-22 17:30:47

WCF Address

2009-12-22 15:02:40

WCF限流

2009-12-22 15:14:33

WCF調用

2010-02-24 15:28:59

WCF ABC

2009-12-07 09:23:05

點贊
收藏

51CTO技術棧公眾號