When trying to access the request object from the System.Threading.Task object that was launched in processing the request, a similar problem occurred. I use a task to reduce response time - in my case, most of the processing can be done after sending the request. That is, I had something like:
public ActionResult SomeAction() { new System.Threading.Task(() => { // here I used this.Request.UrlReferrer and got weird ArgumenException error }).Start(); return someActionResultThatDoesntRequireProcessing; }
I pulled UrlReferrer (and other this.Request.stuff that I needed) in the delayed processing) into a separate "close" variable (I selected them to have the most basic types):
public ActionResult SomeAction() { var urlReferrerAbs = this.Request.UrlReferrer.AbsoluteUri; var clientAddress = this.Request.UserHostAddress; // save other stuff from the request object new System.Threading.Task(() => { // here I used urlReferrerAbs instead of this.Request.UrlReferrer and the error has gone! }).Start(); return someActionResultThatDoesntRequireProcessing; }
It worked for me.
source share