Adding caching to WCF RESTful services using the REST Starter Kit

As promised, this is the first of a series of posts that intend to cover the new capabilities implemented in the WCF REST Starter Kit to enhance the development of RESTful services using WCF. Specifically, this post is focus on how to enable caching on RESTFul services by using the REST Starter Kit . Undoubtedly, caching is one of the greatest benefits of the Web programming model and one of the main attractive of REST  over alternatives such as SOAP/WS-*. Throughout the evolution of the web, the industry has developed very innovative techniques for optimizing content retrieval by the use of caching. These techniques have been reflected in technologies such as MemCache, Oracle Coherence and more recently Microsoft's Velocity that specialized in distributed caching. Additionally, web technologies like ASP.NET provides generic programming models that address some of the most common caching scenarios. All this infrastructure and technologies can be naturally leveraged by RESTful services without the need of creating new caching mechanisms.  Precisely, the REST Starter Kit leverages ASP.NET extending WCF RESTful services with caching capabilities.

The fundamental steps to add caching to a WCF RESTful service consists on adding the WebCache attribute to the operation intended to cache. The following code illustrates this concept on a simple WCF RESTful service which WAS NOT implemented using the WCF REST Starter Kit .

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class SampleService : ISampleService
{
    [WebGet(UriTemplate= "/results")]
    [WebCache(CacheProfileName = "SampleProfile")]

 public Atom10FeedFormatter GetData()
 {
      
        SyndicationFeed feed = new SyndicationFeed();
        feed.LastUpdatedTime = DateTime.Now;
        feed.Id = OperationContext.Current.Host.BaseAddresses[0].ToString()
                                          + "/" + Guid.NewGuid().ToString();
        feed.Title = new TextSyndicationContent("Sample feed");
        SyndicationItem item= new SyndicationItem();
        item.Title = new TextSyndicationContent( "sample feed" );
        item.Content= new TextSyndicationContent("Time: " +
                                                  DateTime.Now.ToString());
        item.Id= OperationContext.Current.Host.BaseAddresses[0].ToString()
                                          + "/" + Guid.NewGuid().ToString();
        item.LastUpdatedTime= DateTime.Now;
        List<SyndicationItem> items= new List<SyndicationItem>();
        items.Add(item);
        feed.Items = items;
        Atom10FeedFormatter formatter = new Atom10FeedFormatter(feed);
        return formatter;
}

 

As you can see, our sample operation is decorated with a WebCache attribute that references a specific caching profile. The details of the caching profile can either be configured as parameters of the WebCache attribute or as a configuration file section as illustrated in the following code.

<system.web>
  ...
      <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <clear/>
          <add name="SampleProfile" duration="30" enabled="true"
                                                  location="Any" />
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>

  ...
</system.web>

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
   
  <services>
   <service name="SampleService"
                              behaviorConfiguration="ServiceBehavior">
    <!-- Service Endpoints -->
    <endpoint address="" binding="webHttpBinding"
                                             contract="ISampleService">
  
     …
    </endpoint>
   </service>
  </services>
  <behaviors>
   ...
  </behaviors>
</system.serviceModel>

The configuration detailed above instructs ASP.NET to cache the results of the operation for thirty seconds. We can corroborate this by querying the following URI multiple times and checking the date returned in the Atom entry.

Although some scenarios might require different caching techniques, this model extend all the benefits of the ASP.NET caching programming model into WCF RESTFul services which directly a large variety of the most common caching use cases in real world REST-based solutions.

How does this work?

From the WCF programming model standpoint, the WebCache attribute is implemented as a WCF operation behavior.

[AttributeUsage(AttributeTargets.Method)]
public sealed class WebCacheAttribute : Attribute, IOperationBehavior
{
…Implementation omitted…

public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
    …Implementation omitted…

    dispatchOperation.ParameterInspectors.Add(
                                      new CachingParameterInspector(…));
}
}

When the host initializes the behavior adds an instance of the CachingParameterInspector class to the dispatcher parameter inspector's collection.  Ultimately, is this parameter inspector which executes the caching algorithms by leveraging the ASP.NET infrastructure.

If you are interested on getting deep into the things you can achieve with the WCF REST Starter Kit make sure you check the Hands On Labs available here.

Published Wednesday, October 29, 2008 9:35 AM by gsusx

Comments

# Adding caching to WCF RESTful services using the REST Starter Kit

As promised, this is the first of a series of posts that intend to cover the new capabilities implemented

Wednesday, October 29, 2008 9:56 AM by externsblogs

# WF 4.0: A First Look at Microsoft PDC 2008

This week we are finally taking the wraps off of Workflow 4.0 and Monday we had a great talk from Kenny

Wednesday, October 29, 2008 10:00 AM by Ron Jacobs

# Adding caching to WCF RESTful services using the REST Starter Kit - Jesus Rodriguez's WebLog

Pingback from  Adding caching to WCF RESTful services using the REST Starter Kit - Jesus Rodriguez's WebLog

# New and Notable 275

After a flurry of PDC specific posts, back to a mixture. Architecture I wanted to give a big mention

# Using SqlCache dependencies with the new WCF WebCache attribute (REST Starter KIT)

Let's begin from a hypothetical example that we want to publish a simple product catalog as an ATOM feed

Monday, November 03, 2008 9:32 AM by Pablo M. Cibraro (aka Cibrax)

# re: Adding caching to WCF RESTful services using the REST Starter Kit

<a href= estrazionesuperenalottoitalia.fathersaua.info >estrazione superenalotto italia</a>  

<a href= http://freeovulationcharts

Tuesday, December 02, 2008 2:47 AM by Aron

# re: Adding caching to WCF RESTful services using the REST Starter Kit

A great resource - many thanks!

Sunday, January 04, 2009 7:12 PM by Mike

# re: Adding caching to WCF RESTful services using the REST Starter Kit

Gute Arbeit hier! Gute Inhalte.

Tuesday, March 03, 2009 4:54 PM by ...

# re: Adding caching to WCF RESTful services using the REST Starter Kit

Dies ist ein gro�er Ort. Ich m�chte hier noch einmal.

Friday, March 06, 2009 5:25 PM by ...

# Caching using WCF REST Starter Kit in Medium Trust &laquo; Emmer Inc

Pingback from  Caching using WCF REST Starter Kit in Medium Trust &laquo;  Emmer Inc

# re: Adding caching to WCF RESTful services using the REST Starter Kit

Overview of US restaurants.  <a href="restaurants-us.com/.../">Kentucky Fried Chicken-Long John Silvers</a>

Tuesday, January 25, 2011 1:07 AM by ChetteEresy

# re: Adding caching to WCF RESTful services using the REST Starter Kit

Yeah, it happens sometimes ... Nothing special.

Tuesday, March 27, 2012 7:25 PM by Angela

# re: Adding caching to WCF RESTful services using the REST Starter Kit

Cool! That's a clever way of looikng at it!

Saturday, September 29, 2012 6:38 PM by Melissa

# re: Adding caching to WCF RESTful services using the REST Starter Kit

Standing Indication <br /> The actual quickest tool in order to discover invisible end users upon web and is never decrease!

Wednesday, October 24, 2012 6:18 AM by seo

# re: Adding caching to WCF RESTful services using the REST Starter Kit

Real love could possibly be the effective difficulty for your everyday living and then the increase of that which most people really like.

soyoyo http://www.soyoyoso.com/

Wednesday, October 31, 2012 5:26 AM by soyoyo