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

深入探討WCF緩存機(jī)制基本概念

開發(fā) 開發(fā)工具
我們今天將會(huì)在這篇文章中,通過一個(gè)簡(jiǎn)單的示例來為大家詳細(xì)解讀一下WCF緩存機(jī)制的基本概念,希望可以給大家?guī)硪恍椭?/div>

WCF開發(fā)工具是一個(gè)由微軟公司開發(fā)的功能強(qiáng)大的開發(fā)插件。它的應(yīng)用可以幫助我們實(shí)現(xiàn)包括在通信方面的各種功能需求。那么接下來,我們將會(huì)為大家分析一下其中一個(gè)比較基礎(chǔ)的內(nèi)容,WCF緩存機(jī)制的相關(guān)概念。#t#

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

例如,一個(gè)基于會(huì)話的通道可能會(huì)根據(jù)共享網(wǎng)絡(luò)連接來關(guān)聯(lián)消息,而另一個(gè)基于會(huì)話的通道可能會(huì)根據(jù)消息正文中的共享標(biāo)記來關(guān)聯(lián)消息??梢詮臅?huì)話派生的功能取決于關(guān)聯(lián)的性質(zhì)。不存在與 WCF 會(huì)話相關(guān)聯(lián)的常規(guī)數(shù)據(jù)存儲(chǔ)區(qū)。***一句告訴我們,WCF中的Session是無法像Web應(yīng)用一樣存儲(chǔ)附加信息的。經(jīng)過研究,我們可以通過擴(kuò)展MessageHeader實(shí)現(xiàn)一個(gè)附加的數(shù)據(jù)存儲(chǔ)區(qū)在Client端每次請(qǐng)求Service時(shí)發(fā)送到Server端。具體實(shí)現(xiàn)如下(以前述需求為例)。

這是一個(gè)單件類,Client正常登陸得到Server端回傳的UserIdentity實(shí)例后可以通過如下代碼將其存入WCF緩存機(jī)制:

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

其中ServerReturnedUserIdentity就是Server產(chǎn)生并回傳的UserIdentity下面我們擴(kuò)展MessageHeader將我們自己定義的UserIdentity加入進(jìn)去,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. publicclassBNClientMessageInspector:IClientMessageInspector 
    { IClientMessageInspector成員  
  12. #regionIClientMessageInspector成員   
  13. publicvoidAfterReceiveReply(refMessagereply,objectcorrelationState) { }   
  14. publicobjectBeforeSendRequest(refMessagerequest,IClientChannelchannel) {   
  15. MessageHeaderMessageHeaderMessageHeadermh=MessageHeader.CreateHeader
    ("UserIdentity","UINS",BNIIClientLayerPlus.UserPermissionInfo
    .GetInstance()._UserIdentity);   
  16. request.Headers.Add(mh);   
  17. returnnull; }   
  18. #endregion } }  

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

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

以上就是對(duì)WCF緩存機(jī)制的基本介紹。

責(zé)任編輯:曹凱 來源: IT專家網(wǎng)
相關(guān)推薦

2010-07-21 09:38:15

PHP緩存技術(shù)

2010-11-22 14:18:32

MySQL鎖機(jī)制

2009-12-21 10:27:52

WCF基本概念

2010-02-23 16:32:29

WCF服務(wù)

2009-12-16 16:44:31

Ruby on Rai

2009-12-21 15:33:07

WCF集合元素

2010-03-01 14:50:30

WCF行為類型

2010-03-01 18:04:35

WCF配置綁定

2010-03-01 16:25:07

WCF體系架構(gòu)

2010-02-24 17:17:04

WCF宿主環(huán)境

2010-03-02 13:14:38

WCF MSMQ隊(duì)列

2009-12-10 15:02:07

OSPF動(dòng)態(tài)路由協(xié)議

2010-03-01 16:41:04

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

2010-03-02 16:22:31

WCF狀態(tài)應(yīng)用

2009-12-21 16:52:02

WCF序列化

2009-12-21 14:37:14

2009-12-22 10:16:54

WCF服務(wù)狀態(tài)

2010-02-25 14:46:31

2010-03-02 11:10:43

WCF標(biāo)準(zhǔn)終結(jié)點(diǎn)

2009-12-23 16:13:00

WPF Attache
點(diǎn)贊
收藏

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