In this post I will describe how to install AppFabric Cache (AFC) and begin talking to it with a C# application. There are many useful things that can be done with AFC, but through I series of post I will discuss a scenario that I am using it for now which is to provide a scalable middle tier data service for routing events from back end systems to Silverlight clients.
But lets start with just getting this to work, which initially seems easy, but like everything else has its issues. First, we need to get a few installation packages.
Download the proper install for AFC from:
http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=8197ad8d-673f-4efb-b165-82710f2648c3
I'm installing this on a fresh Windows 2008 R2 box, so I also need to install .NET 4.0, which you can get here:
http://www.microsoft.com/downloads/details.aspx?FamilyID=9cfb2d51-5ff4-4491-b0e5-b386f32c0992&displaylang=en
Once you have those and .NET 4 installed, start the AppFabric Cache setup, sign your life away, and press next.

You then get the customer experience improvement dialog:

Right now I don’t want to participate in the improvement program so I just press ‘Next’, which takes us to the select services dialog:

I only need caching for this demonstration, so I select them and press next, and it takes you to the confirmation screen:

Press install. When done, you get the ‘installed ok’ screen:

At this point we need to configure the cache service, so leave the check box selected and press ‘Finish’. A few moments later you’ll be presented with the send feedback page for the configuration wizard:

I select ‘no’ and press next to get to the main configuration screen:

Now with this screen I’m not sure why it is possible to not check the Set Caching Service configuration box. I tried that, and could not get things to work (in retrospect, I think it was because of the yet to be described security issue, but it’s just best to do this with a cluster [even a one node cluster] anyway). So, check it and fill out the information as needed.
For my configuration I created an account named ‘velocity’ on the local machine and use that as the login account for the cache service; the setup program does not let you use a built in account.
I am also going to use the XML configuration technique as I want to keep this lightweight without using SQL Server. To do this you must have a network share accessible, so I created a folder on this system (VELOCITYA) and shared it as ‘VelocityConfig’.
Since this is the first system installed, I select ‘New cluster’, and I’m going to use a small cluster (1-5 machines).
Select ‘Next’ and you will be taken to the page to configure the ports that the cache will use.

Leave these as the defaults. Note, I have the windows firewall disabled so those options are also disabled.
Now press ‘Finish’, and accept the confirmation dialog. After a few seconds your configuration will be completed.
But, you are not done, yet.
There are a few powershell commands that first need to be executed. The install will have installed several menu items:

Select “Caching Administrator Windows PowerShell’. This will open a powershell console where you need to run several commands. First run this command: ‘Use-CacheCluster’.

The output is pretty basic – nothing. Now run ‘Start-CacheCluster’. A teal box will show some status of starting the cluster:

And then you’ll get some status of the nodes in the cluster.

Now that we have a one node cluster running, lets try to verify we can talk to it with some C# code.
First, you’ll need the caching client assemblies. I believe you can install these on your development system with the AppFabric Cache installer, but I grabbed mine from the cache server itself in the \windows\system32\appfabric folder:

The code I have here is the simplest code you can use to simply connect to the local system’s cache and retrieve the default cache (with the caveat of some security code needed to solve a security problem that is described soon).

When running this on the VELOCITYA server, the following is the result:

What’s the deal with this? Well, that was a lot of googling on my part to figure out, so I’ll save you the trouble.
The short of it is because the AppFabric cache is not running in a domain it is not authenticating the request, even from the local box. To fix this, we must modify our cache clusters configuration file. There are a few ways to do this, but I found it simplest to just open the XML file in the network share (\\VELOCITYA\VelocityConfig\ClusterConfig.XML"). This is the default content after the configuration that we previously performed:

We need to add the following lines (26-28):

Ideally not the best for security but it is good enough for our purposes of demonstration. Also, I needed to reboot the cache server after making this change. I tried to restart the service, but that didn’t seem to get the settings. Once successful, the program output will be the following:

Success!
Note that those lines added to the configuration file also required the addition of lines 32-34 in the source code. I’ll describe other security models in later posts, but this suffices at this point in time.
Also, please note that the install be default does not set the cache service to start automatically, so you’ll need to go and change that in the services dialog (either start it manually or configure it to auto start).

Next post... Adding items to the cache and getting notifications of new items in the cache.
a7505cbc-f77a-4fb6-87e2-72da90c172bc|0|.0