Quantcast
Viewing all articles
Browse latest Browse all 764

Unable to catch exception with async dataportal call (async/await)

Hello,

 

I am developing window form application by using  CSLA 4.5.501.0 with visual studio 2013 and dot net 4.0. I can successfully fetch data from remote dataportal. 

 

The following is static factory method from BusinessBase class.

 

 public static async Task<PurchaseHeader> GetPurchaseVoucher(string prefix, long serial)

        {

            VoucherNoCriteria criteria = new VoucherNoCriteria()

            {

                Prefix = prefix,

                Serial = serial

            };

            PurchaseHeader purchase = await DataPortal.FetchAsync<PurchaseHeader>(criteria);

            purchase.MarkOld();

            return purchase;

     }

 

----------------------------------------------------------------------------------

 

private async void SearchPurchase(VoucherNo vno) {

            try

            {

                this.Cursor = Cursors.WaitCursor;

 

                if (vno != null)

                {

                    this.ultraStatusBar1.Text = string.Format("Loading purchase voucher ({0}-{1}).", vno.Prefix, vno.Serial);

                    this.purchase = await PurchaseHeader.GetPurchaseVoucher(vno.Prefix, vno.Serial);

                    BindData();

                    commandToolBar1.SetSavedState();

                    this.ultraStatusBar1.Text = string.Format("Done loading purchase voucher ({0}-{1}).", vno.Prefix, vno.Serial);

                }

            }

            catch (Csla.DataPortalException ex) {

 

                if (ex.BusinessException is RecordNotFoundException)

                {

                    UltraMessageBoxManager.Show("Purchase voucher cannot found.", "Purchase", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }

                else if (ex.BusinessException != null)

                {

                    UltraMessageBoxManager.Show("Fail to load purchase voucher\n" + ex.BusinessException.Message, "Purchase", MessageBoxButtons.OK, MessageBoxIcon.Error);

                } 

                this.ultraStatusBar1.Text = "Ready";

            }

            catch (Exception ex)

            {

                UltraMessageBoxManager.Show("Fail to load purchase voucher\n" + ex.Message, "Purchase", MessageBoxButtons.OK, MessageBoxIcon.Error);

                this.ultraStatusBar1.Text = "Ready";

            }

            finally {

                this.Cursor = Cursors.Default;

            }

        }

 

 

 

But if awaiting  async dataportal call throw exception,  catch block is unable to catch exception and TargetInvocationException is throw at Application.Run() method in Program.cs.

 

How can I handle exception in this case.


Viewing all articles
Browse latest Browse all 764

Trending Articles