Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
share|improve this question

2 Answers 2

up vote 2 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!

share|improve this answer

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.

share|improve this answer
    
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 at 16:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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