Reputation: 161
Please take a look at following two methods. The first function GetQuoteImageContent is calling GetFileHeadersByDirectory to return a list of FileVM objects. However if i return the first function with :
return await fileTasks.GetFileHeadersByDirectory(imageLibraryDirectory, false,
"/quotecontents/").ConfigureAwait(false);
then it runs fine but because I need to do some processing on returned value so when I am trying to assign it to a variable:
IEnumerable<FileVM> images = await fileTasks.GetFileHeadersByDirectory(
imageLibraryDirectory, false,
"/quotecontents/").ConfigureAwait(false);
it is falling out of the function without returning anything. Please could somebody point out the error in my calling mechanism? In the end , I want to get hold of the collection IEnumerable< FileVM > to do some processing over it. Any help in this regard is much appreciated.
public async Task<IEnumerable<FileVM>> GetQuoteImageContent(string quoteId)
{
IFileTasks fileTasks = new FileTasks();
string imageLibraryDirectory = string.Format("quotecontents/{0}", quoteId.Split('/')[1]);
IEnumerable<FileVM> images = await fileTasks.GetFileHeadersByDirectory(imageLibraryDirectory, false, "/quotecontents/").ConfigureAwait(false);
return images;
}
// This is 2nd async method which queries database to return list of files
public async Task<IEnumerable<FileVM>> GetFileHeadersByDirectory(string directory, bool recursive, string rootDirectory = "")
{
using (var fileSession = TasksManager.CreateTemporaryFSSession())
{
var fileQuery = fileSession.Query().OnDirectory(directory, recursive);
var fileHeaders = await fileQuery.ToListAsync().ConfigureAwait(false);
var fileVMs = ConvertToFileVM(fileHeaders, rootDirectory);
return fileVMs;
}
}
Upvotes: 0
Views: 592
Reputation: 149646
From the comments, i'm understand that you're not awaiting on GetQuoteImageContent
, and that is why you seem to think your method isn't returning the images.
If you want to iterate the images right after your get, you'll have to await
on that too:
IEnumerable<FileVM> images = await GetQuoteImageContent(quoteId).ConfigureAwait(false);
foreach (var image in images)
{
// Do something
}
If you don't await
, the compiler will yell at you for trying to iterate something which is not iteratable (Since Task
doesn't implement IEnumerable
).
Upvotes: 2