深入探讨WCF缓存机制基本概念

开发 开发工具
我们今天将会在这篇文章中,通过一个简单的示例来为大家详细解读一下WCF缓存机制的基本概念,希望可以给大家带来一些帮助。

WCF开发工具是一个由微软公司开发的功能强大的开发插件。它的应用可以帮助我们实现包括在通信方面的各种功能需求。那么接下来,我们将会为大家分析一下其中一个比较基础的内容,WCF缓存机制的相关概念。#t#

缓存是很占内存的,缓存也有它的好处,这里就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. 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 } }  

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

以上就是对WCF缓存机制的基本介绍。

责任编辑:曹凯 来源: IT专家网
相关推荐

2010-07-21 09:38:15

PHP缓存技术

2010-11-22 14:18:32

MySQL锁机制

2010-02-23 16:32:29

WCF服务

2009-12-21 10:27:52

WCF基本概念

2009-12-16 16:44:31

Ruby on Rai

2009-12-10 15:02:07

OSPF动态路由协议

2010-02-24 17:17:04

WCF宿主环境

2010-03-02 13:14:38

WCF MSMQ队列

2010-03-01 16:25:07

WCF体系架构

2010-03-01 14:50:30

WCF行为类型

2010-03-01 18:04:35

WCF配置绑定

2009-12-21 15:33:07

WCF集合元素

2010-02-25 14:46:31

2010-03-02 11:10:43

WCF标准终结点

2009-12-21 14:37:14

2010-03-02 16:22:31

WCF状态应用

2009-12-21 16:52:02

WCF序列化

2010-03-01 16:41:04

WCF数据表

2009-12-22 10:16:54

WCF服务状态

2009-12-23 16:13:00

WPF Attache
点赞
收藏

51CTO技术栈公众号