App Fabric–Walkthrough Of A Simple Client Code

Posted: February 27, 2011 in .NET, App Fabric

Windows Server AppFabric has been released for quite sometime now.I did not had much time to look into it then.Since last week I have started playing around with the App Fabric Caching.In this post we will build a small App Fabric Cache client and try to understand how it works.

Information regarding the App Fabric Caching architecture and it’s configuration can be found at:

The caching client code adding & retrieving values from the cache is shown below:

static void Main(string[] args) 
    DataCacheServerEndpoint[] servers = new DataCacheServerEndpoint[1] { new DataCacheServerEndpoint("SANKARSANPC", 22233) }; 
    DataCacheFactoryConfiguration factoryConfig = new DataCacheFactoryConfiguration(); 
    factoryConfig.Servers = servers; 
    DataCacheFactory mycacheFactory = new DataCacheFactory(factoryConfig); 
    DataCache cache = mycacheFactory.GetCache("Test"); 
    Person p = new Person() { FirstName = "Sankarsan", LastName = "Bose" }; 
    cache.Put("P1", p); 
    Console.WriteLine((cache.Get("P1") as Person).FirstName); 

Let’s try to understand this step by step.

  • Microsoft.ApplicationServer.Caching.DataCacheServerEndpoint class represents a cache host and host is identified by the hostname and port.
  • Microsoft.ApplicationServer.Caching.DataCacheFactoryConfiguration class is used to modify the configuration settings related to transport,cache hosts,security etc.Out of these here I have used only the cache host part which was set using the Servers property.
  •  Microsoft.ApplicationServer.Caching.DataCacheFactory is the factory class which provides access to the named caches. This class is instantiated using the instance of the DataCacheFactoryConfiguration.This is a very important step.In this step
    • The connection is established to the cache hosts mentioned in the configuration. The communication is a WCF (System.ServiceModel.Channels namespace elements) based custom TCP channel using net.tcp protocol.
    • The routing table (contains the partitioning information based on which the client decides which server to contact to for a particular cached item) is fetched from the cache host. Since this routing table is used for putting/getting values from the cache henceforth question might come then why we need to mention a list of servers in the configuration? This is just to build some redundancy in case one server is not responding for some failure etc.
  • Microsoft.ApplicationServer.Caching.DataCache is the key class representing a named cache and used for storing/retrieving information.The most important and commonly used methods are
    • Add – Add a new key/value to the cache
    • Put – Adds or replaces a key/value to the cache
    • Get – Retrieves a value from cache based on key
  • Rest of the code is pretty simple where we Put an object into cache and Get it back.When we are invoking the Add/Put/Get method then the data needs to be  transferred over wire to the cache host so there is  a serialization and deserialization of data required.Since the underlying channels are WCF channels it uses XML Serialization using System.Runtime.Serialization.NetDataContractSerializer.
  1. App Fabric–Walkthrough Of A Simple Client Code « Sankarsan’s Journal…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

  2. […] App Fabric–Walkthrough Of A Simple Client Code « Sankarsan's Journal […]

  3. […] App Fabric–Walkthrough Of A Simple Client Code « Sankarsan's Journal […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.