c# - 链 ContinueWith 任务

标签 c# asp.net asp.net-core async-await task

我有以下包含两个任务的示例。第一个完成后,第二个应该使用第一个的结果。我是这个领域的新手,如果有人指导我如何链接它,我将不胜感激:

    public async Task<string> UploadFile(string containerName, IFormFile file)
    {
        //string blobPath = "";
        var container = GetContainer(containerName);
        var fileName = file.FileName;
        CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
        using (var memoryStream = new MemoryStream())
        {
            // await file.CopyToAsync(memoryStream);
            // await blob.UploadFromStreamAsync(memoryStream);

            // upload only when the 'memoryStream' is ready 
            Task.Factory.StartNew(() => file.CopyToAsync(memoryStream))
                .ContinueWith(m => blob.UploadFromStreamAsync(m.Result)); // ??
        }
        return blob.Uri.AbsoluteUri;
    }

如果不是第二种变体:

public string UploadFile(string containerName, IFormFile file)
{
    var container = GetContainer(containerName);
    var fileName = file.FileName;
    CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
    using (var memoryStream = new MemoryStream())
    {
        file.CopyToAsync(memoryStream).Wait();
        blob.UploadFromStreamAsync(memoryStream).Wait();

        //Task.Factory.StartNew(() => file.CopyToAsync(memoryStream))
        //    .ContinueWith(m => blob.UploadFromStreamAsync(m.Result)); // ??
    }
    return blob.Uri.AbsoluteUri;
}

最佳答案

IFormFile中获取流并直接上传

public async Task<string> UploadFile(string containerName, IFormFile file)
{
    //string blobPath = "";
    var container = GetContainer(containerName);
    var fileName = file.FileName;
    CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
    await blob.UploadFromStreamAsync(file.OpenReadStream())
    return blob.Uri.AbsoluteUri;
}

关于c# - 链 ContinueWith 任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45866513/

相关文章:

c# - WPF 无边框窗口调整大小

c# - 用于检查日月年日期的 C# 函数

c# - 在使用 MySqlTransaction 方面需要帮助

c# - MVC 将 View 组件文件放在哪里

amazon-web-services - 如何在Azure网站中配置AWS S3凭证

c# - 使用 SQL Server Management Studio 进行数据库查询时遇到问题

c# - 这种并行排序合并是否正确实现?

c# - 什么时候使用存储过程优于硬编码查询?

c# - ASP.net 中的父子 Gridview

linux - Linux 上的 ASP.NET Core Windows 身份验证