小编rpm*_*ins的帖子

字典TryGetValue NullReferenceException

我在线上得到NullReferenceException _dicCache.TryGetValue(objID, out newObject); .我不知道为什么会发生这种情况.可以告诉我正确的方向吗?

这是我的班级:

public class Cache<T>
{
    public string Name { get; set; }

    private  Dictionary<int, T> _dicCache = new Dictionary<int, T>();

    public  void Insert(int objID, T obj)
    {
        try
        {
            _dicCache.Add(objID, obj);

            HttpContext.Current.Cache.Insert(Name, _dicCache, null, DateTime.Now.AddMinutes(10), TimeSpan.FromMinutes(0));
        }
        catch (Exception)
        {
            throw;
        }
    }

    public bool Get(int objID, out T obj)
    {
        _dicCache = (Dictionary<int, T>)HttpContext.Current.Cache.Get(Name);


        try
        {
            return _dicCache.TryGetValue(objID, out obj);
        }
        catch (Exception)
        {
            throw;
        }
    }
 }
Run Code Online (Sandbox Code Playgroud)

以下是我称之为:

   Services.Cache<Entities.User> cache = new Services.Cache<Entities.User>(); …
Run Code Online (Sandbox Code Playgroud)

.net c# dictionary nullreferenceexception

4
推荐指数
1
解决办法
4404
查看次数

使用HttpContext在目标上的ThreadPool QueueUserWorkItem错误

我有这个类,它触发一个方法并忘记它..唯一的问题是,如果调用的方法有HttpContext它抛出NullReferenceException.

我的理由是我不能Httpcontext在里面使用,ThreadPool.QueueUserWorkItem(dynamicInvokeShim, new TargetInfo(d, args));因为我得到的NullReferenceException 是它的工作吗?

方法Httpcontext:

public static DataTable GetDataTable(string name)
{
    return (DataTable)HttpContext.Current.Cache[name];
}
Run Code Online (Sandbox Code Playgroud)

触发并忘记方法的方法:

using System;
using System.Threading;

namespace XGen.Kuapo.BLL
{
  public class AsyncHelper
  {
      class TargetInfo
      {
          internal TargetInfo(Delegate d, object[] args)
          {
              Target = d;
              Args = args;
          }

          internal readonly Delegate Target;
          internal readonly object[] Args;
      }

      private static WaitCallback dynamicInvokeShim = new WaitCallback(DynamicInvokeShim);

      public static void FireAndForget(Delegate d, params object[] args)
      { …
Run Code Online (Sandbox Code Playgroud)

.net c# threadpool

2
推荐指数
1
解决办法
1374
查看次数