We receive a lot of records from a remote server in the form of IAsyncEnumerable
and after a simple convert and JSON serialization we are trying to upload the result (a JSON file) to Azure Blob Storage.
Is there any way I can improve this code to reduce the maximum memory usage. The number of records we receive from the remote server is high.
public async Task ExecuteAsync()
{
var items = GetItems();
using var memoryStream = new MemoryStream();
await JsonSerializer.SerializeAsync(memoryStream, items);
var blobContainerClient = new Azure.Storage.Blobs.BlobContainerClient("connectionString", "container");
var blobClient = blobContainerClient.GetBlobClient("blobName");
memoryStream.Position = 0;
await blobClient.UploadAsync(memoryStream, overwrite: true);
}
private static async IAsyncEnumerable<Item> GetItems()
{
// This is calling a remote server that returns IAsyncEnumerable<Row>
await foreach (var row in GetIAsyncEnumerable())
{
yield return new Item(row);
}
}
Up until the JSON Serilization everything seems fine, we recive the records one-by-one and converting the object type and serilization happens one-by-one too.
But after await JsonSerializer.SerializeAsync(memoryStream, items)
we wait for all the items to finish and materialize in the memory and then we start the upload.
Is there any way we can continue this chain of IAsyncEnumerable
s and start the upload as soon as first item is received and continue uploading for each new object?