WCF緩存機(jī)制怎么理解

這篇文章主要講解了“WCF緩存機(jī)制怎么理解”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“WCF緩存機(jī)制怎么理解”吧!

公司主營(yíng)業(yè)務(wù):網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶(hù)真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶(hù)帶來(lái)驚喜。創(chuàng)新互聯(lián)推出太湖免費(fèi)做網(wǎng)站回饋大家。

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

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

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

UserPermissionInfo.GetInstance().SetUserIdentity(ServerReturnedUserIdentity);

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

usingSystem;  usingSystem.Collections.Generic;  usingSystem.Text;  usingSystem.ServiceModel;  usingSystem.ServiceProcess;  usingSystem.ServiceModel.Dispatcher;  usingSystem.ServiceModel.Description;  usingSystem.ServiceModel.Channels;  usingSystem.ServiceModel.Configuration;  namespaceBNCommon.ClientHelper  {  publicclassBNClientMessageInspector:IClientMessageInspector  {  IClientMessageInspector成員#regionIClientMessageInspector成員  publicvoidAfterReceiveReply(refMessagereply,objectcorrelationState)  {  }  publicobjectBeforeSendRequest(refMessagerequest,IClientChannelchannel)  {  MessageHeaderMessageHeadermh=MessageHeader.CreateHeader("UserIdentity","UINS",BNIIClientLayerPlus.UserPermissionInfo.GetInstance()._UserIdentity);  request.Headers.Add(mh);  returnnull;  }  #endregion  }  }


這個(gè)類(lèi)實(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行為集合,代碼如下:

usingSystem;  usingSystem.Collections.Generic;  usingSystem.Text;  usingSystem.ServiceModel;  usingSystem.ServiceProcess;  usingSystem.ServiceModel.Dispatcher;  usingSystem.ServiceModel.Description;  usingSystem.ServiceModel.Channels;  usingSystem.ServiceModel.Configuration;  namespaceBNCommon.ClientHelper  {  publicclassBNClientEndpointBehavior:BehaviorExtensionElement,IEndpointBehavior  {  IEndpointBehavior成員#regionIEndpointBehavior成員  publicvoidAddBindingParameters(ServiceEndpointendpoint,BindingParameterCollectionbindingParameters)  {}  publicvoidApplyClientBehavior(ServiceEndpointendpoint,ClientRuntimeclientRuntime)  {  clientRuntime.MessageInspectors.Add(newBNClientMessageInspector());  }  publicvoidApplyDispatchBehavior(ServiceEndpointendpoint,EndpointDispatcherendpointDispatcher)  {  }  publicvoidValidate(ServiceEndpointendpoint)  {  return;  }  #endregion  publicoverrideTypeBehaviorType  {  get...{returntypeof(BNClientEndpointBehavior);}  }  protectedoverrideobjectCreateBehavior()  {  returnnewBNClientEndpointBehavior();  }  }  }

感謝各位的閱讀,以上就是“WCF緩存機(jī)制怎么理解”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)WCF緩存機(jī)制怎么理解這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

當(dāng)前文章:WCF緩存機(jī)制怎么理解
新聞來(lái)源:http://bm7419.com/article44/jdssee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、外貿(mào)建站、做網(wǎng)站、小程序開(kāi)發(fā)、企業(yè)網(wǎng)站制作、微信公眾號(hào)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)