I'm trying to take advantage of Distributed Cache in SharePoint 2013 programmatically from a console application. I want to store custom objecst inside App Fabric Cache.

Here is the code that throws an error:

List<DataCacheServerEndpoint> servers = new List<DataCacheServerEndpoint>();
servers.Add(new DataCacheServerEndpoint("Myserver.domain", 22233));
DataCacheFactoryConfiguration configuration = new DataCacheFactoryConfiguration();
configuration.ChannelOpenTimeout = TimeSpan.FromSeconds(60);
configuration.IsCompressionEnabled = false;
configuration.MaxConnectionsToServer = 10;
configuration.RequestTimeout = TimeSpan.FromSeconds(60);
configuration.Servers = servers;
configuration.TransportProperties.MaxBufferSize = 200000;
DataCacheFactory dataCacheFactory = new DataCacheFactory(configuration);
DataCache cache = dataCacheFactory.GetDefaultCache(); // <---- Error 

dataCacheFactory.GetDefaultCache() throws an error:

Cache referred to does not exist. Contact administrator or use the Cache administration tool to create a Cache.

Addtitional info:

Distributed cache is running on my only development server

Get-CacheHostConfig command returns:

HostName        : {hostname}
ClusterPort     : 22234
CachePort       : 22233
ArbitrationPort : 22235
ReplicationPort : 22236
Size            : 410 MB
ServiceName     : AppFabricCachingService
HighWatermark   : 99%
LowWatermark    : 90%
IsLeadHost      : True

Get-CacheHost command returns:

HostName : CachePort               Service Name            Service Status Version Info
--------------------               ------------            -------------- ------------
{hostname}:22233                   AppFabricCachingService UP             3 [3,3][1,3]

I've run this powershell comamnd against my current login:

Grant-CacheAllowedClientAccount domain\user

I would be really grateful if someone could shed some light on this problem. Why do I get this error? Maybe I'm doing something wrong?

Update

I've tried the same code on a completely different SharePoint Farm. There is the same error. So, it's not server-specific. I've managed to make this code work by getting the Cache by the full name like so:

DataCache cache = dataCacheFactory.GetCache("DistributedDefaultCache_bbc9d9fe-ea79-477a-890c-867b1ae0c91b");

You can get the full name by running this PowerShell command:

Get-Cache |Where-Object{$_.CacheName -like "*Default*"}|fl
up vote 3 down vote accepted

This is how the internal implementation of GetDefaultCache method looks like:

public DataCache GetDefaultCache()
{
  string cacheName = "default";
  return this.GetCache(cacheName);
}

It's just trying to call GetCache("defaul"). 'default' cache just does not exist. The real name of the default cache should look more like:

DistributedDefaultCache_bbc9d9fe-ea79-477a-890c-867b1ae0c91b

This GUID turns out to be a farm ID.

So, this is an example that works:

    static void Main(string[] args)
    {
        List<DataCacheServerEndpoint> servers = new List<DataCacheServerEndpoint>();
        servers.Add(new DataCacheServerEndpoint("server.domain", 22233));
        DataCacheFactoryConfiguration configuration = new DataCacheFactoryConfiguration();
        configuration.ChannelOpenTimeout = TimeSpan.FromSeconds(120);
        configuration.IsCompressionEnabled = false;
        configuration.MaxConnectionsToServer = 10;
        configuration.RequestTimeout = TimeSpan.FromSeconds(120);
        configuration.Servers = servers;
        configuration.TransportProperties.MaxBufferSize = 1000000;
        DataCacheFactory dataCacheFactory = new DataCacheFactory(configuration);
        DataCache cache = dataCacheFactory.GetCache("DistributedDefaultCache_" + SPFarm.Local.Id);

        if (cache.Get("MyKEY") == null)
        {
            cache.Add("MyKEY", DateTime.Now);
        }
        DateTime dt = (DateTime)cache.Get("MyKEY");
        Console.WriteLine(dt);
    }

Important Update

I've found that you can't use default App Fabric cluster that is installed with prerequisites. Looks like it's unsupported by Microsoft.

Do not use the AppFabric cache cluster supporting your SharePoint Server 2013 farm. Run your separate AppFabric cache cluster for your custom applications on separate servers from the servers dedicated to your SharePoint Server 2013 farm.

What a shame!

i would try to repair the DC:

1) Get-SPServiceInstance cmdlet to list all services on all servers in the server farm 2) start the powershell and run below command:

$s = Get-SPServiceInstance GUID 

$s.delete()

3) wait for couple of min and in the powershell window run below command.

Add-SPDistributedCacheServiceInstance

Now try and if it fix fine. Technet
2nd option is try to clear the config cache.

  • Thanks for suggestion. I've properly cleared the config cache. Also, I've recreated Distributed Cache Application Instance with these commands: Stop-SPDistributedCacheServiceInstance. Remove-SPDistributedCacheServiceInstance. Add-SPDistributedCacheServiceInstance. I still have the same error "Cache referred to does not exist." – Denis Molodtsov Jan 4 '14 at 16:37

How to use SharePoint 2013’s AppFabric caching in your code

public sealed class CacheManager
    {
        private static readonly object _lock = new object();
        private static DataCache _defaultCache;

        private CacheManager() {}

        public static DataCache DefaultCache
        {
            get
            {
                using (SPMonitoredScope scope = new SPMonitoredScope("CacheManager.DefaultCache"))
                {
                    lock (_lock)
                    {
                        if (_defaultCache == null)
                        {
                            //By default the farm ID is appended to the name of the cluster and caches
                            //created by SharePoint.
                            var farmID = SPFarm.Local.Id;

                            //This is one of the caches created by SharePoint. Run the AppFabric's
                            //Get-Cache PowerShell to see a list of caches created by SharePoint.
                            var defaultCacheName = "DistributedDefaultCache_" + farmID;

                            var factoryConfiguration = new DataCacheFactoryConfiguration()
                            {
                                Servers = GetAllDataCacheServerEndpointsForFarm(farmID)
                            };

                            _defaultCache = new DataCacheFactory(factoryConfiguration).GetCache(defaultCacheName);
                        }
                        return _defaultCache;
                    }
                }
            }
        }

        private static List<DataCacheServerEndpoint> GetAllDataCacheServerEndpointsForFarm(Guid farmID)
        {
            var endpoints = new List<DataCacheServerEndpoint>();

            //By default this is the name of the cluster created by SharePoint.
            string cacheClusterName = "SPDistributedCacheCluster_" + farmID;

            var cacheClusterManager = SPDistributedCacheClusterInfoManager.Local;
            var cacheClusterInfo = cacheClusterManager.GetSPDistributedCacheClusterInfo(cacheClusterName);

            foreach (var cacheHost in cacheClusterInfo.CacheHostsInfoCollection)
            {
                endpoints.Add(new DataCacheServerEndpoint(cacheHost.HostName, cacheHost.CachePort));
            }
            return endpoints;
        }
    }

https://bernado-nguyen-hoan.com/2013/01/03/how-to-use-sharepoints-2013-appfabric-caching-in-your-code/

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.