English 中文(简体)
HttpConttext 目标上的线索 Pol 队列 QuueUerer Work 工程项目错误
原标题:ThreadPool QueueUserWorkItem error on target with HttpContext

我有一个类别, 它会点燃一个方法, 忘记它。 唯一的问题是, 如果这个方法有 < code> Httpcontext , 它会扔出 < code> NullReferenceException

我的猜测是,我无法在 Threadpool. QuueUserworkIm(动态 Invoke Shim,新目标Info(d, args))); 中使用 Htpcontext ,因为我正在获取 NullReferenceexpeption 。 周围是否有它的工作?

Httpcontext 的方法:

public static DataTable GetDataTable(string name)
{
    return (DataTable)HttpContext.Current.Cache[name];
}

火灾和忘记方法的方法:

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)
      {
          ThreadPool.QueueUserWorkItem(dynamicInvokeShim, new TargetInfo(d, args));
      }

      static void DynamicInvokeShim(object o)
      {
          try
          {
              TargetInfo ti = (TargetInfo)o;
              ti.Target.DynamicInvoke(ti.Args);
          }
          catch (Exception ex)
          {
              // Only use Trace as is Thread safe
              System.Diagnostics.Trace.WriteLine(ex.ToString());
          }
      }
  }
}
最佳回答

如usr 所说 : < code> HttpContext. < rent 仅在执行请求的线条上可用,而您开始的线条则不是同一线条。

然而,如果您想要访问 ASP. NET 缓存,您也可以使用 HtpRuntime.Cache ,这是每个线索都可以使用的静态属性。 ( HtpContext.Current.Cache 简单返回 HtpRuntime.Cache ,这样您不必担心任何差异。 )

请注意, 从 < code> HttpContext. 收到的 HtpContext 实例不应该通过到另一个线索 : 当其他线索运行时, 与所捕获的 HttpContext 相对应的请求可能已经终止, 这样您就可以使用一个被打破的 HttpContext 实例 。

问题回答

是的, Htpcontext. 只有在执行 HTTP 请求的线索上才能找到 < rent 。 简单的方法是将表达式 (Datatable) Httpcontext. Crentrent.Cache[name] 的结果传递到线索上, 这样线索本身就不需要访问 Htpcontext 。 或者, 将 httpcontext. /code> 对象传递到线索上 。

如何将数据传送到线索或任务上 这个问题已经超出了范围 但你会在网上找到很多关于它的信息





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...