【发布时间】:2011-02-19 23:34:17
【问题描述】:
我正在尝试将此方法 ExportTo3rdParty() 转换为使用 AsyncController:
public JsonResult SaveSalesInvoice(SalesInvoice invoice)
{
SaveInvoiceToDatabase(invoice); // this is very quick
ExportTo3rdParty(invoice); // this is very slow and should be async
}
但是 ExportTo3rdParty() 方法在多个地方使用了 HttpContext.Current(太多无法更改 - 原始编码器没有使用足够的依赖注入)。例如,它调用 GetDefaultCurrency()。当通过 AsyncController 调用 ExportTo3rdParty() 时,这仍然有效吗?
public Currency GetDefaultCurrency()
{
Currency currency;
string key = string.Format("DefaultCurrency_{0}",
HttpContext.Current.User.Identity.Name);
currency = HttpRuntime.Cache.Get(key) as Currency;
if (currency == null)
{
currency = LookupDefaultCurrency();
HttpRuntime.Cache[key] = currency;
}
}
我知道,如果我使用 Thread.Start,我将无法访问 HttpContext.Current。但是 AsyncController 呢?
【问题讨论】:
标签: c# jquery asp.net-mvc-2 asynccontroller