0

I've just started learning TAP pattern, and I'm getting error

An asynchronous module or handler completed while an asynchronous operation was still pending

I'm getting this error on the line:

var r = await _purchaseService.UpdateEbookDownloadLinkAsync(resultOfAdding.Id, ebookUrls);

Here is how UpdateEbookDownloadLinkAsync method looks like:

public async Task<bool> UpdateEbookDownloadLinkAsync(Guid purchaseId, List<string> urls)
    {
        using (var database = new DatabaseContext())
        {
            var item = await database.PurchasedItems.FindAsync(purchaseId);

            if (item == null) 
            {
                return false;
            }

            var stringBuilder = new StringBuilder();

            foreach (var url in urls)
            {
                stringBuilder.Append(url).Append(';');
            }

            item.EbookDownloadLink = stringBuilder.ToString();
            database.PurchasedItems.AddOrUpdate(item);

            var i = await database.SaveChangesAsync();
            return true;

        }

And before I call UpdateEbookDownloadLinkAsync I have ForEach loop which ends with this piece of code:

 var blob = container.GetBlockBlobReference(nameWithoutExstension + "-" + guid + ".pdf");
 await blob.UploadFromFileAsync(mappedPathOut, FileMode.Open);
 ebookUrls.Add(blob.Uri.ToString());

Could you please help me solve this problem ?

hyperN
  • 2,674
  • 9
  • 54
  • 92
  • Please add some more code - we need to see the code around the bad line where you're getting the error. The problem is likely caused by something before or potentially after that line. The error message makes sense though - you don't have to wait for the database save to complete before returning "true" - a constant not dependent on previous operations. Finish the code, and return true or false based on whether the save was successful, see if that helps. – Jasmine Oct 14 '15 at 17:37
  • Hey, I've just added that statement as test, because I was not sure what the error was, I'll add code as soon as I remove as many irrelevant lines as I can, because there is a lot going on – hyperN Oct 14 '15 at 17:39
  • 3
    You are not awaiting one of your Async methods. Most likely one that ends up calling the `UpdateEbookDownloadLinkAsync` (and that code is not in the post) – Alexei Levenkov Oct 14 '15 at 17:44
  • 2
    Please opt for not removing lines you think are irrelevant. They might be relevant. After all, you're admitting to not understanding the problem here, right? It might be a lot of code, that's fine. Bottom line though, you're returning from your function while some operation is still pending - I think it's the database save that is still pending. If you want to "fire and forget" that save and don't care if it works or not, this isn't how you do that. See this - http://stackoverflow.com/questions/28805796/asp-net-controller-an-asynchronous-module-or-handler-completed-while-an-asynchr – Jasmine Oct 14 '15 at 17:44
  • @AlexeiLevenkov, Hey, that line is in the post: var r = await _purchaseService.UpdateEbookDownloadLinkAsync(resultOfAdding.Id, ebookUrls); and that's the only time I'm calling it – hyperN Oct 14 '15 at 17:49
  • @Jasmine, @AlexeiLevenkov, thanks for your help, but I've managed to find solution, problem was my method inside which `UpdateEbookDownloadLinkAsync` was called, it was declared like this: `public async void Pay()`, but when I changed it to: `public async Task Pay()` it worked – hyperN Oct 14 '15 at 17:53
  • So, the problem was in code you didn't post. See what I mean? – Jasmine Oct 14 '15 at 18:54
  • Yes, you were completely right ! thank you very much for your assistance :) – hyperN Oct 14 '15 at 19:01

0 Answers0