[WCF REST] 通过ASP.NET Output Caching实现声明式缓存
2012-02-10 08:58
1086 查看
ASP.NET的输出缓存(Output Caching)机制允许我们针对整个Web页面或者页面的某个部分(主要针对用户控件)最终呈现的HTML进行缓存。对于后续针对相同资源的请求,只需要直接将缓存的HTML予以回复而无须按照页面处理生命周期对每次请求进行重复处理。WCF通过操作行为AspNetCacheProfileAttribute利用ASP.NET的输出缓存提供一种针对于某个操作的声明式缓存机制。[源代码从这里下载]
[/code]
AspNetCacheProfileAttribute构造函数参数cacheProfileName表示的CacheProfile的配置名称,目标操作按照定义在相应CacheProfile的缓存策略实施缓存。CacheProfile配置在<system.web>/<caching>/<outputCacheSettings>/<outputCacheProfiles>节点下。
[/code]
在如上所示的配置片断中,我们定义了一个名称为default的CacheProfile。代表缓存时间的duration属性被设置为60,意味着缓存项在被存储之后1分钟之后实失效;属性varyByParam被设置为none表示缓存项与请求的查询字符串无关。此外,该CacheProfile还设置针对某个本地数据库中的TestTable表的SQL依赖(SQL Dependency)。关于CacheProfile的配置属于ASP.NET的范畴,在这里我们不会作过多的讨论。
既然是采用ASP.NET输出缓存,WCF服务自然需要采用IIS寄宿并采用ASP.NET 兼容模式。值得一提的是,基于AspNetCacheProfileAttribute的输出缓存仅仅针对HTTP-GET。
[/code]
实现了契约接口ITime的服务类型TimeService定义如下。我们将AspNetCompatibilityRequirementsAttribute特性应用在服务类型上并将RequirementsMode属性设置为Allowed以提供对ASP.NET兼容模式的支持。
[/code]
在一个Web项目中我们为通过IIS寄宿的服务TimeService添加一个对应的.svc文件(TimeService.svc),如下所示的是<%@ServiceHost%>指令的定义。表示ServiceHostFactory类型的指令属性Factory被设置为System.ServiceModel.Activation.WebServiceHostFactory.
[/code]
我们在作为服务宿主的Web项目下添加一个配置文件(Web.config)并定义如下的配置。除了服务寄宿的基本配置外,我们将<system.serviceModel>/<serviceHostingEnvironment >配置节的aspNetCompatibilityEnabled属性设置为True以开启ASP.NET兼容模式。应用在操作方法GetCurrentTime上的AspNetCacheProfileAttribute特性中指定的名称为default的CacheProfile定义在该配置中,duration和varyByParam分别被设置为60和none。
[/code]
作为客户端的控制台程序在进行了相应配置之后通过如下的代码进行服务的调用。在这段代码中,我们通过创建的服务代理进行了5次服务调用,并将获取的时间打印出来。每次服务的时间间隔为1秒。
[/code]
客户端代码执行之后会在控制台上输出如下的结果。由于服务端通过ASP.NET的输出缓存对第一次执行GetCurrentTime操作的结果进行了缓存,所以客户端返回的时间都是相同的。
[/code]
如果对ASP.NET具有一定的了解,应该知道可以通过当前HttpResponse(HttpContext.Current.Response)的Cache属性表示的HttpCachePolicy对象来控制当前输出缓存的基本策略。实际上AspNetCacheProfileAttribute就是通过这种方式将定义在指定CacheProfile的缓存策略应用到针对当前操作的调用上的。
具体来说,AspNetCacheProfileAttribute针对输出缓存策略的控制是通过一个实现了接口IParameterInspector的自定义参数检验器实现的,这是一个名称为CachingParameterInspector的内部类型。操作行为AspNetCacheProfileAttribute通过实现的ApplyDispatchBehavior方法将针对某个CacheProfile创建的CachingParameterInspector对象添加到当前分发操作(DispatchOperation)的参数检验器列表中。
[/code]
如上面的代码片断所示,当AfterCall方法被执行的之后,在构造函数中指定的CacheProfile定义的输出缓存策略应用到当前HttpResponse。而AfterCall会在操作执行之后,回复消息序列化之前被执行。
一、AspNetCacheProfileAttribute
WCF对ASP.NET缓存的支持是通过AspNetCacheProfileAttribute特性来实现的。通过如下的代码我们不难看出AspNetCacheProfileAttribute是实现了IOperationBehavior接口的操作行为,我们可以直接将其应用到契约接口/类中的某个具有缓存需要的操作方法上。[code] [AttributeUsage(AttributeTargets.Method)] public sealed class AspNetCacheProfileAttribute : Attribute, IOperationBehavior { //其他成员 public AspNetCacheProfileAttribute(string cacheProfileName); public string CacheProfileName { get; } }
[/code]
AspNetCacheProfileAttribute构造函数参数cacheProfileName表示的CacheProfile的配置名称,目标操作按照定义在相应CacheProfile的缓存策略实施缓存。CacheProfile配置在<system.web>/<caching>/<outputCacheSettings>/<outputCacheProfiles>节点下。
[code] <configuration> <connectionStrings> <add name="localDb" connectionString="Server=.; Database=TestDb; Uid=sa; Pwd=password" providerName="System.Data.SqlClient"/> </connectionStrings> <system.web> <caching> <outputCacheSettings> <outputCacheProfiles> <add name="default" duration="60" varyByParam="none" sqlDependency="TestDb: TestTable"/> </outputCacheProfiles> </outputCacheSettings> <sqlCacheDependency> <databases> <add name="TestDb" connectionStringName="localDb"/> </databases> </sqlCacheDependency> </caching> </system.web> </configuration>
[/code]
在如上所示的配置片断中,我们定义了一个名称为default的CacheProfile。代表缓存时间的duration属性被设置为60,意味着缓存项在被存储之后1分钟之后实失效;属性varyByParam被设置为none表示缓存项与请求的查询字符串无关。此外,该CacheProfile还设置针对某个本地数据库中的TestTable表的SQL依赖(SQL Dependency)。关于CacheProfile的配置属于ASP.NET的范畴,在这里我们不会作过多的讨论。
既然是采用ASP.NET输出缓存,WCF服务自然需要采用IIS寄宿并采用ASP.NET 兼容模式。值得一提的是,基于AspNetCacheProfileAttribute的输出缓存仅仅针对HTTP-GET。
二、实例演示:创建采用输出缓存的服务
接下来我们通过一个简单的实例来演示如何通过操作行为对某个操作的返回值实施缓存,为此我们创建一个用于返回当前时间的服务。如下所示的是作为服务契约的ITime接口的定义,AspNetCacheProfileAttribute特性被应用到了用于返回当前时间的操作方法GetCurrentTime上。[code] using System; using System.ServiceModel; using System.ServiceModel.Web; namespace Artech.WcfServices.Service.Interface { [ServiceContract(Namespace = "http://www.artech.com/")] public interface ITime { [WebGet(UriTemplate = "/current")] [AspNetCacheProfile("default")] DateTime GetCurrentTime(); } }
[/code]
实现了契约接口ITime的服务类型TimeService定义如下。我们将AspNetCompatibilityRequirementsAttribute特性应用在服务类型上并将RequirementsMode属性设置为Allowed以提供对ASP.NET兼容模式的支持。
[code] using System; using System.ServiceModel.Activation; using Artech.WcfServices.Service.Interface; namespace Artech.WcfServices.Service { [AspNetCompatibilityRequirements( RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class TimeService : ITime { public DateTime GetCurrentTime() { return DateTime.Now; } } }
[/code]
在一个Web项目中我们为通过IIS寄宿的服务TimeService添加一个对应的.svc文件(TimeService.svc),如下所示的是<%@ServiceHost%>指令的定义。表示ServiceHostFactory类型的指令属性Factory被设置为System.ServiceModel.Activation.WebServiceHostFactory.
[code] <%@ ServiceHost Service="Artech.WcfServices.Service.TimeService" Factory="System.ServiceModel.Activation.WebServiceHostFactory"%>
[/code]
我们在作为服务宿主的Web项目下添加一个配置文件(Web.config)并定义如下的配置。除了服务寄宿的基本配置外,我们将<system.serviceModel>/<serviceHostingEnvironment >配置节的aspNetCompatibilityEnabled属性设置为True以开启ASP.NET兼容模式。应用在操作方法GetCurrentTime上的AspNetCacheProfileAttribute特性中指定的名称为default的CacheProfile定义在该配置中,duration和varyByParam分别被设置为60和none。
[code] <configuration> <system.serviceModel> <services> <service name="Artech.WcfServices.Service.TimeService"> <endpoint binding="webHttpBinding" contract="Artech.WcfServices.Service.Interface.ITime"/> </service> </services> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> </system.serviceModel> <system.web> <caching> <outputCacheSettings> <outputCacheProfiles> <add name="default" duration="60" varyByParam="none"/> </outputCacheProfiles> </outputCacheSettings> </caching> </system.web> </configuration>
[/code]
作为客户端的控制台程序在进行了相应配置之后通过如下的代码进行服务的调用。在这段代码中,我们通过创建的服务代理进行了5次服务调用,并将获取的时间打印出来。每次服务的时间间隔为1秒。
[code] using (ChannelFactory<ITime> channelFactory = new ChannelFactory<ITime>("timeService")) { ITime proxy = channelFactory.CreateChannel(); for (int i = 0; i < 5; i++) { Console.WriteLine(proxy.GetCurrentTime().ToLongTimeString()); Thread.Sleep(1000); } }
[/code]
客户端代码执行之后会在控制台上输出如下的结果。由于服务端通过ASP.NET的输出缓存对第一次执行GetCurrentTime操作的结果进行了缓存,所以客户端返回的时间都是相同的。
[code] 4:48:43 PM 4:48:43 PM 4:48:43 PM 4:48:43 PM 4:48:43 PM
[/code]
三、 AspNetCacheProfileAttribute是如何实现输出缓存的?
既然我们采用ASP.NET兼容模式来寄宿服务,意味着我们调用某个服务与访问某个页面没有本质的区别,所以基于Web页面的输出缓存能够应用于基于某个服务操作的调用就不足为奇了。现在有这么一个问题:通过AspNetCacheProfileAttribute特性指定CacheProfile是如何生效的?如果对ASP.NET具有一定的了解,应该知道可以通过当前HttpResponse(HttpContext.Current.Response)的Cache属性表示的HttpCachePolicy对象来控制当前输出缓存的基本策略。实际上AspNetCacheProfileAttribute就是通过这种方式将定义在指定CacheProfile的缓存策略应用到针对当前操作的调用上的。
具体来说,AspNetCacheProfileAttribute针对输出缓存策略的控制是通过一个实现了接口IParameterInspector的自定义参数检验器实现的,这是一个名称为CachingParameterInspector的内部类型。操作行为AspNetCacheProfileAttribute通过实现的ApplyDispatchBehavior方法将针对某个CacheProfile创建的CachingParameterInspector对象添加到当前分发操作(DispatchOperation)的参数检验器列表中。
[code] internal class CachingParameterInspector : IParameterInspector { public CachingParameterInspector(string cacheProfileName); public object BeforeCall(string operationName, object[] inputshens); public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState) { //将指定CacheProfile的输出缓存策略应用到当前HttpResponse } }
[/code]
如上面的代码片断所示,当AfterCall方法被执行的之后,在构造函数中指定的CacheProfile定义的输出缓存策略应用到当前HttpResponse。而AfterCall会在操作执行之后,回复消息序列化之前被执行。
相关文章推荐
- [WCF REST] 通过ASP.NET Output Caching实现声明式缓存
- [转载]Web开发中的缓存技术:通过ETag实现缓存处理(Asp.Net)
- ASP.NET Web API中通过ETag实现缓存
- Web开发中的缓存技术之三:通过ETag实现缓存处理(ASP.NET MVC版)
- ASP.NET Web API通过ActionFilter来实现缓存
- ASP.NET缓存技术学习入门---页面缓存(OutPut Caching)
- 通过 SignalR 类库,实现 ASP.NET MVC 的实时通信
- 浅谈asp.net通过本机cookie仿百度(google)实现搜索input框自己主动弹出搜索提示
- ASP.NET jQuery 食谱12 (通过使用jQuery validation插件简单实现用户注册页面验证功能)
- ASP.NET简单实现服务端图片缓存
- memcache实战之四:接口编程(分别用asp.net自带的System.Web.Caching,以及分布式缓存MemcachedProviders)来实现缓存接口
- IIS7 host WCF通过多播实现出版-预订(Build pub-sub with IIS7 Host WCF by msmq multicast and netmsmqintegrationbinding)
- ASP.NET通过IHttpModule实现伪静态
- 通过ASP.NET实现到Word的导出方案的对比和疑问!!!
- 通过Flash实现ASP.NET多文件上传
- 搭建基于asp.net的wcf服务,ios客户端调用的实现记录
- asp.net实现邮箱验证功能,通过邮箱链接激活该帐号
- 通过Docker Compose及NGINX反向代理实现ASP.NET5应用的负载均衡
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【七】——实现资源的分页
- asp.net通过js实现Cookie创建以及清除Cookie数组的代码