0

The following method, should be downloading a csv file from Azure storage to a memory stream.

Note: AzureResponse is a custom class that returns a bool, a string (message) and either a stream or a string result

public async Task<AzureResponse> ReadCsvFileToStreamFromBlobAsync(CloudBlobContainer container, string fileName)
    {
        var ar = new AzureResponse();
        // Retrieve reference to a blob (fileName)
        var blob = container.GetBlockBlobReference(fileName);
        if (blob != null)
        {
            string message;
            try
            {
                using (var memoryStream = new MemoryStream())
                {
                   **// code gets here and then falls right through to the end of the
                   //calling method, bypassing the catch portion here** 
                   await blob.DownloadToStreamAsync(memoryStream)                          
                    **//Tried adding .ConfigureAwait(false); to the above call                    //downloads blob's content to a stream
                   //that did not work
                   //per this SO post, https://stackoverflow.com/questions/28526249/azure-downloadtostreamasync-method-hangs

                    //code doesn't reach here**
                    message = message = $"Csv file read to Stream correctly. Filename:  {fileName}.";
                    ar.Status = true;
                    ar.Message = message;
                    ar.FileAsStream = memoryStream;
                    return ar;

                }
            }
            catch (Exception ex)
            {
                message = $"Csv file was not loaded to the memory stream on {DateTime.Now} for file {fileName} with exception message {ex.Message}";
                ar.Status = false;
                ar.Message = message;
                ar.FileAsStream = null;
                return ar;
            }
        }

        ar.Status = false;
        ar.Message = $" File not found error for {fileName} on {DateTime.Now}";
        ar.FileAsStream = null;
        return ar;
    }

NOTE: And this method is called in my Main method(its a console app) with these lines

 az = new AzureStorageCommon(config);
        var fileContainer = az.GetAzureFilesContainer();
        var afs = new AzureFileMethods();
        var returned = afs.ReadCsvFileToStreamFromBlobAsync(fileContainer, "EarningsEvents_Dec_2018.csv");

The file path is not the issue, when that Uri is put in the browser it prompts you to download the file (Is this the issue? Is that blocking the async download to the stream?)

There is an overload that takes a cancellation token, but I cannot find anything on how to use that properly.

This is what's returned to the calling method

Id = 37, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"

Every tutorial I've found does this in this manner, not sure why this isn't working.

  1. Is it because its a csv file? (I doubt it)
  2. Is it due to the browser requesting whether you want to save the file?
  3. How can you run this with the cancellation token to ensure the file is actually finished before this code completes.
dinotom
  • 4,498
  • 10
  • 59
  • 124
  • Could you please let me know how do you call the ReadCsvFileToStreamFromBlobAsync() method? I create a console project and try to repro your issue, but it works fine at my side. – Ivan Yang Dec 10 '18 at 07:00
  • @IvanYang...see the update in the OP..I've tried xml files,txt files. I tried different methods, they all fall through on the first Azure Async method called. – dinotom Dec 10 '18 at 08:41
  • 1
    In your main method, I see that you are not using await before this afs.ReadCsvFileToStreamFromBlobAsync method. Are you using async keyword in your main method declaration? – Ivan Yang Dec 10 '18 at 08:53
  • @IvanYang... no I wasn't. Each method in the calling chain has to be async, that makes sense. It works now – dinotom Dec 10 '18 at 08:58
  • ok, glad that you solve it:) – Ivan Yang Dec 10 '18 at 08:59

0 Answers0