静态变量未设置

时间:2017-11-06 15:25:54

标签: c# static

我有以下代码:

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);

    }

这在我的Windows 10和一些Windows 2012计算机上运行。我现在部署到Windows 2008R2机器,我收到错误:

  

System.NullReferenceException
  对象引用未设置为对象的实例。

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)
...

因此,除常量null之外的参数都不能为空。

我哪里错了?

0 个答案:

没有答案