HttpContext.Current空在MVC4制作功能,当异步 [英] HttpContext.Current null when making a function Asynchronous in MVC4
问题描述
我目前正在MVC4在VS2010-SP1。我提出的功能中的一个
控制器类异步。由于这部分我做的控制器类
从AsyncController来源并添加以下两个方法(见code第1节和
2以下)。一个用异步方法结束(见code第1节)和结束的另一种方法
与已完成(见code第2节)。问题是在模型类我想
从HttpContext的访问我的web服务的凭证(请参见下面3 code)。该
进行异步调用时的上下文是怎么回事空。也就是说,在新的线程
HttpContext的是不可用。如何通过从主线程新主题上下文
创建。
code第1节
公共无效SendPlotDataNewAsync(字符串寄件者,串TODATE,串项)
{ AsyncManager.OutstandingOperations.Increment();
VAR highChartModel =新HighChartViewModel();
Task.Factory.StartNew(()=>
{
AsyncManager.Parameters [DATAPLOT] =
highChartModel.GetGraphPlotPointsNew(寄件者,TODATE,项目);
AsyncManager.OutstandingOperations.Decrement();
});}
code第2节
公共JsonResult SendPlotDataNewCompleted(词典<字符串列表< ChartData>>
DATAPLOT)
{
返回JSON(新{数据= DATAPLOT});
}
code第3节
公开名单< MeterReportData> GetMeterDataPointReading(MeterReadingRequestDto
meterPlotData)
{ VAR的客户= WcfClient.OpenWebServiceConnection< ReportReadingClient,
IReportReading>(NULL,(串)HttpContext.Current.Session [WebserviceCredentials]?
的String.Empty); 尝试
{
返回
ReadReportMapper.MeterReportReadMap(client.GetMeterDataPointReading(meterPlotData));
}
赶上(异常前)
{
Log.Error(元数据异常:{0},{1},{2},{3},
ex.GetType()。toString()方法,ex.Message,(ex.InnerException!= NULL)?
ex.InnerException.Message:的String.Empty,);
扔;
}
最后
{
WcfClient.CloseWebServiceConnection< ReportReadingClient,
IReportReading> (客户);
} }
HttpContext.Current
是空
因为你任务没有 AspNetSynchronizationContext
于池中的线程上执行同步上下文。
使用 TaskScheduler.FromCurrentSynchronizationContext()
:
Task.Factory.StartNew(()=>
{
AsyncManager.Parameters [DATAPLOT] =
highChartModel.GetGraphPlotPointsNew(寄件者,TODATE,项目);
AsyncManager.OutstandingOperations.Decrement();
},
CancellationToken.None,
TaskCreationOptions.None,
TaskScheduler.FromCurrentSynchronizationContext());
I am currently working on MVC4 in VS2010-SP1. I made one of the function in the controller class Asynchronous. As part of that I made the controller class derived from AsyncController and added the below two methods ( see code section 1 and 2 below). one method ending with Async(See Code Section 1 ) and another method ending with Completed ( See Code Section 2 ). The problem is in the model class I am trying to access my webservice with credentials from HttpContext ( See Code below 3 ). The context is going null when making an asynchronous call. ie, In the new thread httpcontext is not available. How to pass the context from main thread to new threads created.
Code Section 1
public void SendPlotDataNewAsync(string fromDate, string toDate, string item)
{
AsyncManager.OutstandingOperations.Increment();
var highChartModel = new HighChartViewModel();
Task.Factory.StartNew(() =>
{
AsyncManager.Parameters["dataPlot"] =
highChartModel.GetGraphPlotPointsNew(fromDate, toDate, item);
AsyncManager.OutstandingOperations.Decrement();
});
}
Code Section 2
public JsonResult SendPlotDataNewCompleted(Dictionary<string, List<ChartData>>
dataPlot)
{
return Json(new { Data = dataPlot });
}
Code Section 3
public List<MeterReportData> GetMeterDataPointReading(MeterReadingRequestDto
meterPlotData)
{
var client = WcfClient.OpenWebServiceConnection<ReportReadingClient,
IReportReading>(null, (string)HttpContext.Current.Session["WebserviceCredentials"] ??
string.Empty);
try
{
return
ReadReportMapper.MeterReportReadMap(client.GetMeterDataPointReading(meterPlotData));
}
catch (Exception ex)
{
Log.Error("MetaData Exception:{0},{1},{2},{3}",
ex.GetType().ToString(), ex.Message, (ex.InnerException != null) ?
ex.InnerException.Message : String.Empty, " ");
throw;
}
finally
{
WcfClient.CloseWebServiceConnection<ReportReadingClient,
IReportReading> (client);
}
}
HttpContext.Current
is null
because your task is executed on a pool thread without AspNetSynchronizationContext
synchronization context.
Use TaskScheduler.FromCurrentSynchronizationContext()
:
Task.Factory.StartNew(() =>
{
AsyncManager.Parameters["dataPlot"] =
highChartModel.GetGraphPlotPointsNew(fromDate, toDate, item);
AsyncManager.OutstandingOperations.Decrement();
},
CancellationToken.None,
TaskCreationOptions.None,
TaskScheduler.FromCurrentSynchronizationContext());
这篇关于HttpContext.Current空在MVC4制作功能,当异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!