掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
對于WCF緩存是很占內(nèi)存的,但是我覺得它也有它的好處,現(xiàn)在我們先來看看微軟官方對WCF的Session的說明:它們由調(diào)用應(yīng)用程序顯式啟動和終止。歡迎大家進行參考學(xué)習(xí)。

會話期間傳遞的消息按照接收消息的順序進行處理。會話將一組消息相互關(guān)聯(lián),從而形成對話。該關(guān)聯(lián)的含義是抽象的。例如,一個基于會話的通道可能會根據(jù)共享網(wǎng)絡(luò)連接來關(guān)聯(lián)消息,而另一個基于會話的通道可能會根據(jù)消息正文中的共享標(biāo)記來關(guān)聯(lián)消息??梢詮臅捙缮墓δ苋Q于關(guān)聯(lián)的性質(zhì)。不存在與 WCF 會話相關(guān)聯(lián)的常規(guī)數(shù)據(jù)存儲區(qū)。#t#
最后一句告訴我們,WCF中的Session是無法像Web應(yīng)用一樣存儲附加信息的。經(jīng)過研究,我們可以通過擴展MessageHeader實現(xiàn)一個附加的數(shù)據(jù)存儲區(qū)在Client端每次請求Service時發(fā)送到Server端。具體實現(xiàn)如下(以前述需求為例)。
這是一個單件類,Client正常登陸得到Server端回傳的UserIdentity實例后可以通過如下代碼將其存入WCF緩存:
其中ServerReturnedUserIdentity就是Server產(chǎn)生并回傳的UserIdentity下面我們擴展MessageHeader將我們自己定義的UserIdentity加入進去,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) { }

我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流