Home > Net >  In MVC Task. Obtaining the HttpContext Run method. The Current
In MVC Task. Obtaining the HttpContext Run method. The Current

Time:05-09

I used in the project Task. Run method, but in this method, the internal need to set up a cache data, cache Settings Key used to obtain the client cookies to identify only the user, but when they get in the asynchronous method not null reference object, the cache of setting up the framework itself is written, it is a custom SessionHepler helper classes, have in the framework, such as login user information cache, if I get rid of its cost is too high, so I use the following methods:
 
[HttpPost]
Public ActionResult ImportData (HttpPostedFileBase ImportFile)
{
AjaxResult res=new AjaxResult ();
//TODO validation file type
Var filePath=Path. GetTempFileName ();
Try
{
ImportFile. SaveAs (filePath);
}
The catch (IOException ex)
{
Return the Error (" disk space is insufficient, please inform the technicians!" );
}
HttpContext CTX=System. Web. HttpContext. Current;
Task. Run (()=& gt;
{
System. Web. HttpContext. Current=CTX;
Try
{
_dev_DutyInfoBus. ImportBatch (filePath);

}
The catch (Exception ex)
{
SessionHelper. Session [" DutyInfoImportProgress "]=new AjaxResult ()
{
Success=false,
Msg=ex. Message,
Data=https://bbs.csdn.net/topics/-1
};
}


});
Return Success ();
}

Public void ImportBatch (string importFilePath)
{
//initialize the progress for 1
SessionHelper. Session [" DutyInfoImportProgress "]=new AjaxResult ()
{
Success=true,
Msg="operation is successful,"
Data=https://bbs.csdn.net/topics/1
};
.
}

I will pass the request before calling asynchronous task, after testing can actually get to the value, I would like to ask is, I do so? Will there be hidden? Whether there is any other way?
  • Related