I am uploading a file asynchronously to Azure blob storage using a standard function, however the only method I have available to use, uses a delegate to pass control to a second function. But I need a return value from the first function (preferably using await).
How can I await the result and therefore return the value in the first function? Can I await the delegate function? I want to return blob.uri
Here's the code:
private Uri uploadImageFileToContainer(byte[] fileContent, string containerName, string blobName)
{
CloudBlobClient blobClient = myAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
container.CreateIfNotExist();
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
// see: http://www.c-sharpcorner.com/UploadFile/40e97e/windows-azure-blockblob-putblock-method/ for details
HashSet<string> blocklist = new HashSet<string>();
foreach (FileBlock block in GetFileBlocks(fileContent))
{
blockBlob.PutBlock(
block.Id,
new MemoryStream(block.Content, true),
null
);
blocklist.Add(block.Id);
}
**AsyncCallback UploadCompleted = new AsyncCallback(OnUploadCompleted);
blockBlob.BeginPutBlockList(blocklist, OnUploadCompleted, blockBlob);**
}
private void OnUploadCompleted(IAsyncResult result)
{
CloudBlockBlob blob = (CloudBlockBlob)result.AsyncState;
blob.SetMetadata();
blob.EndPutBlockList(result );
}