【发布时间】:2014-10-15 23:40:15
【问题描述】:
我已经将这段代码投入生产很长时间了,而这个问题才刚刚开始。基本上它是从 MVC 控制器下载文件。这些文件大约 5 到 20 兆字节。这是一个在 iOS 上运行的 PCL 库。用户从服务器下载文件时遇到错误。我假设这是一个超时,但我不是 100% 确定。我们无法在内部重新创建此问题,但我们在日志中看到错误并且用户已报告此问题。
我可以在服务器端做些什么来改善这一点?客户没有任何改变,这已成为我们客户群的普遍问题。
服务器代码
public virtual FileResult BuildDatabaseForSite(Guid organizationId, Guid siteId) {
var basePath = RESTContext.Config.DBPath;
var filePath = basePath + @"\" + fileId + ".db";
byte[] existingDbBytes = null;
using (new Impersonator(RESTContext.Config.VideoPathUsername, RESTContext.Config.VideoPathDomain,
RESTContext.Config.VideoPathPassword)) {
if (System.IO.File.Exists(filePath))
existingDbBytes = System.IO.File.ReadAllBytes(filePath);
}
return File(existingDbBytes, "application/octet-stream", "CompassLocal.db");
}
客户端代码
public void DownloadDatabase(Guid organizationId, Guid siteId) {
Platform.FileSystem.DeleteFile(Paths.LocalDatabase);
var url = string.Format("organizations/{0}/Sites/{1}/InitialDatabaseCompressed", organizationId, siteId);
var webClient = new HttpClient();
webClient.Timeout = new TimeSpan(0,0,5,0);
webClient.DefaultRequestHeaders.Add("Authorization", authHeader);
var stream = webClient.GetStreamAsync(url).Result;
using (Stream file = File.OpenWrite(Paths.LocalDatabase)) {
stream.CopyTo(file);
}
}
堆栈跟踪
System.AggregateException: One or more errors occurred
at System.Threading.Tasks.Task.Wait (Int32 millisecondsTimeout, CancellationToken cancellationToken) [0x00000] in <filename unknown>:0
at System.Threading.Tasks.Task.Wait () [0x00000] in <filename unknown>:0
at System.Threading.Tasks.Task`1[TResult].get_Result () [0x00000] in <filename unknown>:0
at DataFinch.Mobile.Core.Network.RemoteAPI.DownloadDatabase (Guid organizationId, Guid siteId) [0x00000] in <filename unknown>:0
at DataFinch.Mobile.Core.Services.Registration.RegistrationService.RegisterDeviceAndDownloadDatabase (System.String username, System.String password, Guid siteId) [0x00000] in <filename unknown>:0
【问题讨论】:
-
使用
Task.Result调用异步重载和阻塞任务有什么意义? -
这已经在后台线程中,所以它需要在该线程中同步。
-
是的,要么做完全异步,要么不做异步,否则你要求死锁。此外,如果您提供实际异常会很好,而不仅仅是 AggregateException(即迭代 InnerExceptions 属性),因为 AggregateExceptions 并没有真正提供太多信息。
-
仅供参考,您仍然可以进行 async/await 调用并使其串行执行。
-
聚合异常是:System.Threading.Tasks.TaskCanceledException:任务被取消,我认为是超时。
标签: c# asp.net-mvc xamarin task-parallel-library async-await