关于WCF缓存机制介绍
创始人
2024-06-11 04:10:22
0

缓存是很占内存的,缓存也有它的好处,这里就WCF缓存机制分析一个案例,希望大家可以从中得到收获。首先我们看看MSDN中对WCF的Session的说明:它们由调用应用程序显式启动和终止。会话期间传递的消息按照接收消息的顺序进行处理。会话将一组消息相互关联,从而形成对话。该关联的含义是抽象的。

#T#例如,一个基于会话的通道可能会根据共享网络连接来关联消息,而另一个基于会话的通道可能会根据消息正文中的共享标记来关联消息。可以从会话派生的功能取决于关联的性质。不存在与 WCF 会话相关联的常规数据存储区。***一句告诉我们,WCF中的Session是无法像Web应用一样存储附加信息的。经过研究,我们可以通过扩展MessageHeader实现一个附加的数据存储区在Client端每次请求Service时发送到Server端。具体实现如下(以前述需求为例)。

这是一个单件类,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. }  


这个类实现了IClientMessageInspector接口,实现该接口可以在Client每次向Server请求前及请求返回后控制Client的行为对发送和接收的数据进行处理。现在我们需要实现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. }  


 

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...