c# - MVC App - Is this the correct approach for using Async and Await? -


i have mvc controller method number of things, last thing this:

    public void performcheckout(int salesaddressid)     {         try         {             ...             ...             // made sale. send emails market owners.             sendsalesemailsasync(master);         }         catch (system.exception exception)         {             // log error         } 

and sendsalesemailesasynch() looks this:

    private async task sendsalesemailsasync(salesmaster salesmaster)     {         ...         ...         // send email owner of each marker         foreach(string market in markets)             await sendsalesemailasync(salesmaster, salesdetaillist, market).configureawait(false);     } 

sendsalesemailasynch() looks this:

    // send email market owner     private async task sendsalesemailasync(salesmaster salesmaster, list<salesdetail> salesdetail, string marketname)     {         ...         ...         email email = new email(new list<string> { selleremailaddress }, emailsubject, emailhtmlbody);         await email.sendasync().configureawait(false); 

and finally, method sends email:

    public async task sendasync()     {         // create network credentials object         var credentials = new networkcredential(azureusername, azurepassword);          // create web transport sending email         var transportweb = new web(credentials);          // send email. don't care current context let's run in thread pool context.         // more information: http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html         await transportweb.deliverasync(this._email).configureawait(false);     } 

i pretty new async , await. proper? also, i'm not entirely sure catch block in controller action method called if error occurs while sending email.

as suspected, catch block won't exceptions since didn't synchronize current context. in order have mark action method async , use await calling sendsalesemailsasync. action method this:

public async task performcheckout(int salesaddressid) {     try     {         ...         ...         // made sale. send emails market owners.         await sendsalesemailsasync(master);     }     catch (system.exception exception)     {         // log error     } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -