2

I have the following code:

public static class SessionManager
{
    private static readonly Cache cache = new Cache();

    private static readonly TimeSpan TokenTimeout = TimeSpan.FromDays(7);

    private static void RemoveCallback(string key, object value, CacheItemRemovedReason reason)
    {
        ...
    }

    public static string CreateToken(string objectGUID)
    {
        var sessionGUID = Guid.NewGuid().ToString();
        ...
        if(objectGUID==null) throw new ArgumentNullException("objectGUID")
        SessionManager.cache.Add(sessionGUID, objectGUID, null, Cache.NoAbsoluteExpiration, TokenTimeout, CacheItemPriority.Normal, SessionManager.RemoveCallback);

    }

This runs on my Windows 10 and some Windows 2012 machines. I have now deployed to a Windows 2008R2 machine and I am getting the error:

System.NullReferenceException
Object reference not set to an instance of an object.

at System.Web.Caching.Cache.Add(String key, Object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
at TestApp.App_Start.SessionManager.CreateToken(String objectGUID)
at TestApp.Controllers.LoginController.Login(HttpRequestMessage req)
...

So no parameter except the constant null can be null.

Where am I wrong?

Alexander
  • 19,906
  • 19
  • 75
  • 162
  • You probably don't have permission on a 2008R2 to read registry. – jdweng Nov 06 '17 at 15:35
  • 2
    The exception is not in your code but inside the System.Web.Caching.Cache.Add method. – felix-b Nov 06 '17 at 15:37
  • @jdweng I don't think that would cause a NRE though. – DavidG Nov 06 '17 at 15:37
  • @jdweng The app pool is running as IUSR on all machines, and is able to access the AD, but I never knowingly tried to write the registry. – Alexander Nov 06 '17 at 15:40
  • 2
    "This API supports the product infrastructure and is not intended to be used directly from your code." on the [Cache constructor](https://msdn.microsoft.com/en-us/library/system.web.caching.cache.cache.aspx) – Hans Kesting Nov 06 '17 at 15:41
  • ... the thing Hans said... – Stefan Nov 06 '17 at 15:42
  • @HansKesting How would I access the Cache from a static class then? And why does it work on some machines, but not on others? – Alexander Nov 06 '17 at 15:43
  • 2
    I believe this https://stackoverflow.com/a/12746758/4544845 answers your question – felix-b Nov 06 '17 at 15:46
  • "Information about an instance of this class is available through the Cache property of the HttpContext object or the Cache property of the Page object.". As to why it sometimes works, that I don't know. – Hans Kesting Nov 06 '17 at 15:49
  • @HansKesting Well, `HttpContext.Current.Cache` is null. – Alexander Nov 06 '17 at 15:52
  • 1
    At start you probably don't have a context yet, so that is an issue. Maybe use System.Runtime.Caching (see https://stackoverflow.com/a/44688398/121309)? – Hans Kesting Nov 06 '17 at 15:57
  • 1
    You are running as a service and Users connecting http are probably Guest Accounts which do not have the credentials. – jdweng Nov 06 '17 at 16:14

0 Answers0