DynamicListBase saveitem problem in a WebAPI controller action

DynamicListBase saveitem problem in a WebAPI controller action

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


j0552 posted on Wednesday, September 25, 2013

Hi

Not sure what I need to do here. I have a  DynamicListBase object. When I send an HTTP POST request and call dyn.Saveitem(item), if the DataPortal throws an exception, I get this exception in the response:

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

 // POST api/values

public void Post([FromBody] string value)
{
 DynamicRootList dyn = DynamicRootList.NewDynamicRootList();
 DynamicRoot item = dyn.AddNew();
 item.Name = value;
 dyn.SaveItem(item);
}

I have found that this hides the error:

 public void Post([FromBody] string value)
{
            Task.Run(() =>
            {
                DynamicRootList dyn = DynamicRootList.NewDynamicRootList();
                DynamicRoot item = dyn.AddNew();
                item.Name = value;
                dyn.SaveItem(item);
            });
}

My question is: Is this really the best way to get around the problem?

Thanks
Andrew

 

JonnyBee replied on Wednesday, September 25, 2013

Hi,

I suspect that SaveItem actually does an async save based on the exception you get. 

Have you tried to call:

await dyn.SaveItemAsync(item);

Added issue in CSLA: https://github.com/MarimerLLC/csla/issues/204

j0552 replied on Wednesday, September 25, 2013

Still using v4.3.13 so no SaveItemAsync. Can't upgrade just yet. We are using the .NET v4.5. I think the save is done on a BackGroundWorker isn't it?

Is my workaround reasonable in the circumstances? I'm using another thread and also lose the security context. 

Maybe I can do this sort of fire and forget:

Task.Run(() => dyn.SaveItem(item));

I also need to add a handler to the 'Saved' event to catch an errors for later reporting. This would be done on the request thread. Could that be a problem?

Just looking for a bit of guidance for a temp fix before we can upgrade to CSLA v4.5.

 

Copyright (c) Marimer LLC