Get ICacheClient from a class

Hi!
I’m trying to develop a class to better handle my users session ala: https://github.com/ServiceStackApps/TechStacks/blob/master/src/TechStacks/TechStacks.ServiceInterface/ContentCache.cs
Here is my code:

public class CacheSessions
{
    public ICacheClient Client { get; set; }

    public CacheSessions(ICacheClient cache = null)
    {
        this.Client = cache ?? HostContext.TryResolve<ICacheClient>();
    }

    public object GetAll()
    {
        var cache = HostContext.TryResolve<ICacheClient>();
        var sessionPattern = IdUtils.CreateUrn<IAuthSession>("");
        var sessionKeys = cache.GetKeysStartingWith(sessionPattern).ToList();
        var allSessions = cache.GetAll<IAuthSession>(sessionKeys);

        return allSessions;
    }
}

but I get an error on this line:

var sessionKeys = cache.GetKeysStartingWith(sessionPattern).ToList();

ToList() isn’t supported… I think I can’t get the ICacheClient in the correct way… any help?

You’re going to have to provide more details about the error is it a compilation or a runtime exception? please provide the full error or StackTrace.

Sorry! It’s a compilation error:

'System.Collections.Generic.IEnumerable<string>' does not contain a definition for 'ToList' and the best extension method overload 'ServiceStack.EnumExtensions.ToList(System.Enum)' has some invalid arguments

and, I think, a related one to the above:

Instance argument: cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'System.Enum'   

The line is:

var sessionKeys = cache.GetKeysStartingWith(sessionPattern).ToList();

I’m on ServiceStack 4.0.53.

That’s because IEnumerable.ToList() extension method is in the System.Linq namespace. You should really consider getting a tool like ReSharper which automatically resolve missing namespaces for you as you’re getting basic namespace errors I’ve not seen in years. I’ve not tried it without R# but I’m surprised vanilla VS.NET 2015 would still have issues with missing namespaces.

Oh god! I trusted VS.NET IntelliSense… but it didn’t give me any hint in this case! :frowning:
I didn’t know about R#, I will try it for sure!
Thank you Demis you always teach me something! :wink:

mmm… I have changed my class a little, but this time I get a runtime error:

Object reference not set to an instance of an object.

when I try to call my GetAll() method on this line:

var sessionKeys = this.Client.GetKeysStartingWith(sessionPattern).ToList();

This is my class:

public class CacheSessions
{
    public ICacheClient Client { get; set; }
    
    public CacheSessions(ICacheClient cache = null)
    {
        this.Client = cache ?? HostContext.TryResolve<ICacheClient>();
    }

    public object GetAll()
    {
        var sessionPattern = IdUtils.CreateUrn<IAuthSession>("");
        var sessionKeys = this.Client.GetKeysStartingWith(sessionPattern).ToList();
        var allSessions = this.Client.GetAll<IAuthSession>(sessionKeys);

        return allSessions;
   }
}

It seems it can’t get my class instance… I’m currently init it like this:

container.Register(new CacheSessions());

What I missing?

Looks like a basic C# Null Reference Error:

Have you registered an ICacheClient in the Container at this point? If not then HostContext.TryResolve<ICacheClient>() is going to resolve into a null instance.

You can change it so it tries to resolve the ICacheClient at the time it’s resolved with:

container.Register(c => new CacheSessions(c.Resolve<ICacheClient>()));

I think you should try debugging first, this seems like a trivial error that could’ve been easily identified with a break point in the constructor.

1 Like