您的位置:首页 > 编程语言 > ASP

[WCF REST] 通过ASP.NET Output Caching实现声明式缓存

2012-02-10 08:58 1086 查看
ASP.NET的输出缓存(Output Caching)机制允许我们针对整个Web页面或者页面的某个部分(主要针对用户控件)最终呈现的HTML进行缓存。对于后续针对相同资源的请求,只需要直接将缓存的HTML予以回复而无须按照页面处理生命周期对每次请求进行重复处理。WCF通过操作行为AspNetCacheProfileAttribute利用ASP.NET的输出缓存提供一种针对于某个操作的声明式缓存机制。[源代码从这里下载]

一、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会在操作执行之后,回复消息序列化之前被执行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐