【发布时间】:2014-08-04 16:02:45
【问题描述】:
我正在开发 ASP.NET MVC 应用程序并使用 await/async。我的控制器中有以下方法:
public async Task<ActionResult> PrintCachePartial()
{
var cacheItems = await cacheProvider.GetPrintCacheItems();
return View(cacheItems);
}
然后它调用cacheProvider.GetPrintCacheItems():
public async Task<IEnumerable<PrintCacheItem>> GetPrintCacheItems()
{
var res = await atrServiceFacade.GetCacheList(CacheType.PrintCache);
var entities = DataSetToCacheItemEntities(res.dataSet).ToArray();
return entities;
}
这调用atrServiceFacade.GetCacheList():
public Task<AtrDataSetResult> GetCacheList(CacheType cacheType)
{
var tcs = new TaskCompletionSource<AtrDataSetResult>();
var client = serviceFactory.GetATRServiceClient();
client.CacheListCompleted += (sender, args) => AsyncHelper.AsyncCompleted(sender, args, tcs);
client.CacheListAsync(cacheType);
return tcs.Task;
}
这只是 SOAP 服务的一个任务包装器。
看起来工作正常,但问题是每次刷新页面(调用控制器方法)时,IIS 进程的 CPU 利用率会提高约 30%。所以它很快就达到了 100%。
我认为是因为await/async 但我不确定。
我正在使用 ASP.NET MVC 4,目标是 .NET 4。为了 await/async 兼容性,我正在使用 Microsoft.Bcl.Async 库。
【问题讨论】:
-
你为什么这么认为?您是否使用同步调用在没有它的情况下进行了检查?
-
其实你说的对,我同步调用的时候是一样的。
-
我的第一个猜测是
DataSetToCacheItemEntities,但你需要真正的分析。 -
async/awaitis undefined on ASP.NET if you use the Microsoft.Bcl.Async library。这是一个不受支持的场景;你应该更新到 4.5。 -
该问题仅在 Visual Studio (2013) 运行时出现。当解决方案未在 VS 中打开时,它运行良好。当我在 VS 中打开解决方案,甚至不运行调试器时,就会出现问题。
标签: c# asp.net .net asp.net-mvc async-await