Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chain ContinueWith tasks

I have the following sample with two tasks. When the first is finished, the second should use the first's results. I am new in this field I I would appreciate if someone guide me how to chain it:

    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;
    }

If not the second variant:

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;
}
like image 278
serge Avatar asked Jul 25 '26 05:07

serge


1 Answers

Get the stream from the IFormFile and upload it directly

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;
}
like image 119
Dennis Kuypers Avatar answered Jul 27 '26 18:07

Dennis Kuypers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!