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

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

Old forum URL: forums.lhotka.net/forums/t/12889.aspx


richardb posted on Tuesday, May 26, 2015

I have this error "An asynchronous module or handler completed while an asynchronous operation was still pending" in my MVC 5 web application when deleting children from my model, via a grid which fires ajax requests to the MVC controller.

There is an async business rule on the parent, IsAsync = true and context.Complete is called when it Executes.

My delete signature in the MVC controller is (using a Kendo grid here);

public async Task<JsonResult> CustomerUser_Destroy([DataSourceRequest] DataSourceRequest request, AccountUserEdit record, int id)

and the content......

if (record != null)
               {
                   //get existing model(s)
                   var parentModel = AccountEdit.GetAccount(record.AccountId);
                   AccountUserEdit child = (from p in parentModel.CustomerAccountUsers
                                             where p.Id == id
                                             select p).First();

                    //delete it
                   parentModel.CustomerAccountUsers.Remove(child);

                    //can we save it
                   if (parentModel.IsSavable)
                   {
                       parentModel = await parentModel.SaveAsync(true);
                   }
                   else
                   {
                       //get broken rules message.
                       throw new ValidationException(MyBrokenRulesMessage.GetBrokenRules(parentModel, child));

                    }
               }
               return Json(ModelState.ToDataSourceResult());

I have this wrapped within a try catch exception block but the error is not being caught here.  

So the behavior to reproduce the error is that I can add some children in my MVC grid and they are saved ok.  If I then start removing them I get the error.  Oddly, if I refresh the browser page, and then delete a child it works.

Hard to track the error down.....so what am I doing wrong?

"An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

[InvalidOperationException: An asynchronous module or handler completed while an asynchronous operation was still pending.]

"

 

Any pointers appreciated.

ajj3085 replied on Wednesday, May 27, 2015

Could the error be misleading?  Are you getting the newly inserted child objects DB IDs back to the client?  The fact that refreshing the page fixes it makes me think that the client data is out of sync and refreshing builds a new page with all the IDs in place.

richardb replied on Thursday, May 28, 2015

Thanks Andy.

The error may well be misleading, the web app gives the error 500 internal error and using Fiddler this is the response I see.  

Perhaps related to my other post here, what I did was;

"In the end, the solution I used was to use a viewmodel.  So when the grid loads, I map (using the wondeful AutoMapper) my children into a list of viewmodels (simple poco's without a Parent property) and bind that to the grid.  The kendo grid can then post the child vm back nicely and then I load up my parent model and its children, find the model I'm editing and map the changes back from the vm into the child - thus I get the csla business rules firing as required.

The viewmodel works for me in my MVC world.  If I was doing a WPF UI I know from experience that the grid controls do tend to support complex objects, and that a WPF grid would typically be able to do a save call on the parent which would then ripple down to the children and save our amended child."

Ultimately I think the trick here is to remember that inline grid editing in an MVC world is often really a postback updating an individual record, thus you need a root BusinessBase object model to support this.

Hopefully I explained that ok.  

 

Copyright (c) Marimer LLC