AppFabric simple check cs file
So I was playing with appfabric and ran into some interesting bits.

I discovered that if you have appfabric running on multiple non clustered machines and using MS SQL as the configuration store then.
If you shut down the SQL server
* The distributed cluster still functions
* You can not use the powershell interface to interact with any of the distributed nodes.
* You can however interact with each member via code, in my case C#

Now to switch config servers is relatively easy.

My scenario.
Have 4 nodes running in non clustered mode, I did not have win datacenter or enterprise installed and was not looking for the hig availability at the server level.
No config server.

Result wanted.
The ability to restart the nodes as needed, which meant a new config server being put in place.

Solution.
Take one node offline.
Using the appfabreic config tool, create and setup a new config database in SQL. To Microsofts credit this is really simple to do, just use the appfabric configuration tool (Configure AppFabric is the name in the start menu)

Once I had completed this step it was a matter of re-configuring the other nodes. Since the system was now under load I had to project expected downtime. Which was reasonable easy based on the ease or reconfiguration. 5 minutes downtime expected.

Having a look over the MS powershell configuration guide I checked to see how I can verify all is working as expected.

Verification, well a simple Get-CacheStatistics -CacheName NAME_OF_CACHE is as easy as it gets.

So first node gets started up again and we create the named cached we need using New-Cache -CacheName NAME_OF_CACHE

Right we all set to roll over the other nodes to the new config server.

Going through the “Configure AppFabric” tool and proceeding right to the end but not clicking the final button to do the actual configuration. This way all nodes are ready to roll over. Quick test on a VM shows the service will be restarted and reconfigured. Ok then we wait till a quiet time.

Then it was time, buttons pressed and I could see the machines chugging away as they switch to the new config server. Then bam alarms start to trigger, something is wrong. A quick investigation showed that one of the named cached was not doing as expected so a quick Remove-Cache -CacheName NAME_OF_CACHE then recreate it New-Cache -CacheName NAME_OF_CACHE and tada alarms go quiet.

A quick check that each node can infact use the powerchell inteface.

Get-CacheStatistics -CacheName NAME_OF_CACHE

All seemed to be good at that point. So a super quick console app in C# to test and we were done.

You will of course need the nuget package ServerAppFabric.Client to make this work.

The console code looks like this

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ApplicationServer.Caching;

namespace AppfabricTest
{
internal class Program
{
public static List cacheNameList = new List();

private static void Main(string[] args)
{
// Names of named caches
cacheNameList.Add(“TEST”);

GetCacheInfo();
}

private static void GetCacheInfo()
{
StreamWriter file = new System.IO.StreamWriter(“Details.txt”, true);

try
{
// Declare array for cache host(s) – in our case just one to quickly test
DataCacheServerEndpoint[] servers = new DataCacheServerEndpoint[1];
servers[0] = new DataCacheServerEndpoint(“localhost”, 22233);

// Setup the DataCacheFactory configuration.
DataCacheFactoryConfiguration factoryConfig = new DataCacheFactoryConfiguration();
factoryConfig.Servers = servers;

factoryConfig.SecurityProperties = new DataCacheSecurity(
DataCacheSecurityMode.None,
DataCacheProtectionLevel.None);

// Create a configured DataCacheFactory object.
DataCacheFactory mycacheFactory = new DataCacheFactory(factoryConfig);

try
{
foreach (var cacheName in cacheNameList)
{
WriteInfo(string.Format(“Using: {0}”, cacheName), file);

// Get a cache client for the default cache
//DataCache myCache = mycacheFactory.GetDefaultCache(); //or change to mycacheFactory.GetCache(myNamedCache);
DataCache myCache = null;

try
{
myCache = mycacheFactory.GetCache(cacheName);

WriteInfo(string.Format(“Inserting dummy data into: {0}”, cacheName), file);

//inserty dummytest data to test
myCache.Put(“key1”, “test1”);
myCache.Put(“key2”, “test 2”);
myCache.Put(“key3”, “test three”);

//read back our values
string result1 = (string)myCache.Get(“key1”);
string result2 = (string)myCache.Get(“key2”);
string result3 = (string)myCache.Get(“key3”);

WriteInfo(string.Format(“Retrieved result1: {0}”, result1), file);
WriteInfo(string.Format(“Retrieved result2: {0}”, result2), file);
WriteInfo(string.Format(“Retrieved result3: {0}”, result3), file);

//list all items in the cache : important part
foreach (string region in myCache.GetSystemRegions())
{
WriteInfo(string.Format(“Cache name: {0} — Region name: {1}”, cacheName, region), file);

try
{
foreach (var kvp in myCache.GetObjectsInRegion(region))
{
string content =
String.Format(
“Cache: {0} — data item (‘{1}’,'{2}’) in region {3} of cache {4}”,
cacheName,
kvp.Key,
kvp.Value.ToString(),
region,
cacheName);

WriteInfo(content, file);
}
}
catch (Exception exItem)
{
WriteInfo(string.Format(“Error 1: {0} – {1}”, cacheName, exItem.Message), file);
}
}
}
catch (Exception)
{
WriteInfo(string.Format(“Failed to GetCache: {0}”, cacheName), file);
myCache = null;
}
}
}
catch (Exception exItem)
{
WriteInfo(string.Format(“Error 2: {0}”, exItem.Message), file);
}

}
catch (Exception exItem)
{
WriteInfo(string.Format(“Error 3: {0}”, exItem.Message), file);
}
finally
{
file.Close();
file.Dispose();
}
}

static void WriteInfo(string message, StreamWriter file)
{
Console.WriteLine(message);
file.WriteLine(message);
}
}
}

AppFabric simple check cs file