public async Task GenerateAndSaveCsv()

in Hands-on lab/lab-files/src/TollBooth/TollBooth/FileMethods.cs [34:74]


        public async Task<bool> GenerateAndSaveCsv(IEnumerable<LicensePlateDataDocument> licensePlates)
        {
            var successful = false;

            _log.LogInformation("Generating CSV file");
            string blobName = $"{DateTime.UtcNow:s}.csv";

            using (var stream = new MemoryStream())
            {
                using (var textWriter = new StreamWriter(stream))
                using (var csv = new CsvWriter(textWriter, CultureInfo.InvariantCulture, false))
                {
                    csv.WriteRecords(licensePlates.Select(ToLicensePlateData));
                    await textWriter.FlushAsync();

                    _log.LogInformation($"Beginning file upload: {blobName}");
                    try
                    {
                        var container = _blobClient.GetContainerReference(_containerName);

                        // Retrieve reference to a blob.
                        var blob = container.GetBlockBlobReference(blobName);
                        await container.CreateIfNotExistsAsync();

                        // Upload blob.
                        stream.Position = 0;
                        // TODO 7: Asynchronously upload the blob from the memory stream.
                        // COMPLETE: await blob...;

                        successful = true;
                    }
                    catch (Exception e)
                    {
                        _log.LogCritical($"Could not upload CSV file: {e.Message}", e);
                        successful = false;
                    }
                }
            }

            return successful;
        }