我正在尝试使用 Asp.NET Core创建一个Web API,该API公开了启动和取消大文件长时间下载的路由。服务器应该能够同时处理多个下载。
- 下载是使用
WebClient.DownloadFileAsync
执行的,以便缩短响应时间并返回downloadId
供以后使用。WebClient 的实例作为值存储在静态字典中,其对应的key
自然是downloadId
。 - 应使用
WebClient.CancelAsync
在通过访问与downloadId
键对应的字典的值检索的客户端实例上取消下载。
当下载完成而不被取消时,以下代码可以完美运行;正确调用 AsyncCompletedEventHandler(在这种情况下OnDownloadFileCompleted
)。
问题:调用WebClient.CancelAsync
时,文件继续下载,OnDownloadFileCompleted
不会立即调用。Web客户端似乎等到下载完成才调用处理程序。然而,在这两种情况下,属性AsyncCompletedEventArgs.Canceled
都设置正确(例如,如果确实调用了WebClient.CancelAsync
则true
。
- 我错过了什么吗?
- 是否有更好/更智能的模式来处理 WebAPI 中的多个下载?
任何帮助将不胜感激!
下载控制器.cs
[Route ("api/download")]
public class DownloadController {
private readonly DownloadService service;
public DownloadController (DownloadService service) {
this.service = service;
}
[Route ("start")]
[HttpPost]
public string Start ([FromForm] string fileUrl) => this.service.StartDownload (fileUrl);
[Route ("cancel")]
[HttpPost]
public void Cancel ([FromForm] string downloadId) => this.service.CancelDownload (downloadId);
}
下载服务.cs
public class DownloadService {
public string DOWNLOAD_FOLDER { get => "C:\tmp"; }
public static Dictionary<string, WebClient> DownloadClients = new Dictionary<string, WebClient> ();
public string StartDownload (string fileUrl) {
var downloadId = Guid.NewGuid ().ToString ("N");
DownloadClients[downloadId] = new WebClient ();
DownloadClients[downloadId].DownloadFileCompleted += OnDownloadFileCompleted;
DownloadClients[downloadId].DownloadFileAsync (new Uri (fileUrl), Path.Combine (DOWNLOAD_FOLDER, downloadId), downloadId);
return downloadId;
}
public void CancelDownload (string downloadId) {
if (DownloadClients.TryGetValue (downloadId, out WebClient client)) {
client.CancelAsync ();
}
}
private void OnDownloadFileCompleted (object sender, AsyncCompletedEventArgs e) {
var downloadId = e.UserState.ToString ();
if (!e.Cancelled) {
Debug.WriteLine ("Completed");
} else {
Debug.WriteLine ("Cancelled"); //will only be reached when the file finishes downloading
}
if (DownloadClients.ContainsKey (downloadId)) {
DownloadClients[downloadId].Dispose ();
DownloadClients.Remove (downloadId);
}
}
}
我能够复制你所看到的:CancelAsync
实际上并没有取消下载。
使用HttpClient
,您可以获取流并使用CopyToAsync
将其保存到文件中,接受CancellationToken
。取消令牌会立即停止下载。
这是我修改为使用HttpClient
的DownloadService
类。
public class DownloadService {
public string DOWNLOAD_FOLDER {
get => "C:\tmp";
}
public static readonly ConcurrentDictionary<string, Download> Downloads = new ConcurrentDictionary<string, Download>();
public async Task<string> StartDownload(string fileUrl) {
var downloadId = Guid.NewGuid().ToString("N");
Downloads[downloadId] = new Download(fileUrl);
await Downloads[downloadId].Start(Path.Combine(DOWNLOAD_FOLDER, downloadId));
return downloadId;
}
public void CancelDownload(string downloadId) {
if (Downloads.TryRemove(downloadId, out var download)) {
download.Cancel();
}
}
这将使用如下所示的Download
类:
public class Download {
private static readonly HttpClient Client = new HttpClient();
private readonly string _fileUrl;
private readonly CancellationTokenSource _tokenSource = new CancellationTokenSource();
private Task _copyTask;
private Stream _responseStream;
private Stream _fileStream;
public Download(string fileUrl) {
_fileUrl = fileUrl;
}
public async Task Start(string saveTo) {
var response = await Client.GetAsync(_fileUrl, HttpCompletionOption.ResponseHeadersRead);
_responseStream = await response.Content.ReadAsStreamAsync();
_fileStream = File.Create(saveTo);
_copyTask = _responseStream.CopyToAsync(_fileStream, 81920, _tokenSource.Token).ContinueWith(task => {
if (task.IsCanceled) return;
_responseStream.Dispose();
_fileStream.Dispose();
});
}
public void Cancel() {
_tokenSource.Cancel();
_responseStream.Dispose();
_fileStream.Dispose();
}
}
您仍然需要做一些工作才能从Downloads
列表中删除成功完成的下载,但我会留给您。