自定义任务运行器方法抛出ArgumentException

由于CultureInfo没有从一个线程复制到另一个线程,我已经使用以下方法为我做这件事。

public static StartCustomTask(Action action, TaskCreationOptions tco = TaskCreationOptions.None) { var currentCult = Thread.CurrentThread.CurrentCuture; var currentUiCult = Thread.CurrentThread.CurrentUICulture; return Task.Factory.StartNew(() => { Thread.CurrentThread.CurrentCuture = currentCult; Thread.CurrentThread.CurrentUICulture = currentUiCult; action(); }, tco); } 

基本上这个代码将文化信息从当前线程复制到将要执行action的线程。 我不知道为什么,但它抛出System.ArgumentExceptionValue does not fall within the expected range 。 我试图在主线程上定期运行动作,它完美无缺。 我的意思是,作为一个动作的方法本身没有问题,我想在上面的代码中有一个问题。

这是exception的堆栈跟踪

 at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo) at System.Web.Hosting.IIS7WorkerRequest.GetServerVariableInternal(String name) at System.Web.HttpRequest.BuildUrl(Func`1 pathAccessor) at System.Web.HttpRequest.get_Url() at SL.CoreLogic.Web.FrontEnd.Controllers.AccountController.c__DisplayClass37.b__31() in d:\CoreProjects\CoreLogic\CoreLogic-RusSlot\SL.CoreLogic\SL.CoreLogic.Web.FrontEnd\Controllers\AccountController.cs:line 447 at SL.CoreLogic.Common.CustomTask.c__DisplayClass1.b__0() in d:\CoreProjects\CoreLogic\CoreLogic-RusSlot\SL.CoreLogic\SL.CoreLogic.Common\CustomTask.cs:line 22 at System.Threading.Tasks.Task. 

还有一件事。 这段代码工作得很好,但突然之间就开始这么做了。

我知道了。 问题是该操作包含一行像这样的Url = Request.Url,因为我猜在执行代码时,Request对象不存在或未设置。