浅谈WCF缓存机制

    文章来源:万象互联 更新时间:2012-10-30 12:53:34
分享:

 缓存是很占内存的,缓存也有它的好处,这里就WCF缓存机制分析一个案例,希望大家可以从中得到收获。首先我们看看MSDN中对WCF的 Session的说明:它们由调用应用程序显式启动和终止。会话期间传递的消息按照接收消息的顺序进行处理。会话将一组消息相互关联,从而形成对话。该关 联的含义是抽象的。例如,一个基于会话的通道可能会根据共享网络连接来关联消息,而另一个基于会话的通道可能会根据消息正文中的共享标记来关联消息。可以从会话派生的功能取 决于关联的性质。不存在与 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. }  
  38. }

    文章来源:http://www.hulian.top,转载请注明!

版权说明:本站原创文章,由万象互联SEO优化发表.
本文地址:https://www.hulian.top/zixun/post/5332.html
在线咨询
  • 在线时间
  • 8:00-21:00