I am having some issues while using async methods. I am kind of new to it, but I am doing some Blazor code and using Blob storage and some calls need to be async. Here is my code and issue. This code needs to be async because it is getting Blob data from Azure. (FileUpload is my custom object):
public async Task<List<FileUpload>> GetAllFilesAsync()
{
// TODO: GENERATE fileupload objects here and return
await foreach (var blobItem in _container.GetBlobsAsync())
{
var name = blobItem.Name;
BlobClient blobClient = _container.GetBlobClient(name);
FileUpload fileUploadViewModel = new FileUpload()
{
FileName = blobItem.Name,
FileStorageUrl = blobClient.Uri.ToString(),
ContentType = blobItem.Properties.ContentType,
};
_allFiles.Add(fileUploadViewModel);
}
return _allFiles;
}
In my Blazor code (Server) I have this code calling the service on initalize:
private List<FileUpload> fileUploadViewModels = new();
protected override async Task OnInitializedAsync()
{
GetAllBlobsAsync();
}
private void GetAllBlobsAsync()
{
var allFiles = blobStorageService.GetAllFilesAsync();
fileUploadViewModels = allFiles; // This is where the error is thrown
}
Error: Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.List' to 'System.Collections.Generic.List'
I have been searching here and the web and just have not found an answer. I tried Task.FromResult() and others with no luck. As I said, I am kind of new to async and await, so I am hoping I am just missing something small. Can you please clarify what I am doing wrong?