Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Using Await and async in ASP.NET app
Message
De
14/04/2016 12:23:23
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
01634709
Message ID:
01634793
Vues:
48
J'aime (1)
>>>>To be clear: Do you want to simply notify the user that the request to send an email has been received - or that the email was successfully sent ?
>>>>
>>>>If the former then you could just use a call to an async task without the await. In essence :
public bool EmailSendRequest(Email e)
>>>>        {
>>>>            SendEmail(e);
>>>>            return true;
>>>>        }
>>>>
>>>> public async Task SendEmail(Email e)
>>>>        {
>>>>            //Do whatever to send etc.
>>>>        }
If you wanted to notify the user on the actual success/failure of the send you could use something like SignalR to push to the user later....
>>>
>>>Therefore it looks like "await" is not necessary.
>>
>>If you wanted code to run after the SendEmail() task completed you could use await and add that code after the call. But to use 'await' the enclosing method must be an async task.....
>>
>>> I am not at all familiar with SignalR, will Google it.
>>
>>In simple terms SignalR would allow your client side to listen for messages sent by the server so the browser could be sent a notification if the email send failed
>
>Please confirm that I understand it correctly. If I DO NOT use await the code runs and is forgotten. But if some code has to run AFTER the process (e.g. email procedure ) I have to have AWAIT and put the follow-up code after AWAIT? Correct?
>
>As far as SignalR, it sounds very clear the way you explain it. I am sure it will be not so simple to implement.
>Thank you very much.

If you do not use await, the code continues and your async function runs in a separate thread. Await tells the calling thread to wait for your function to finish. As far as code that happens after the process, you have a couple of options. If you want the code to run in the original thread, or want to guarantee that it finishes before returning the response, use await. If you want your post processing to also occur in a background thread, you can chain the original call with ContinueWith. See https://msdn.microsoft.com/en-us/library/dd270696%28v=vs.110%29.aspx.

You do need to be careful with your background threads in ASP.NET. If they run too long after the response is returned they may be aborted. See http://stackoverflow.com/questions/29577652/calling-an-async-method-without-awaiting and http://blog.stephencleary.com/2012/12/returning-early-from-aspnet-requests.html.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform