c# - new MailAddress() Exits out of method -
i have weird issue can't wrap head around. here code:
[httpget] [allowanonymous] public actionresult sendemail() { sendregisteremail("test", "testagain", "lasttest"); return view("index"); } public async task<bool> sendregisteremail(string subject, string message, string to) { var email = new email(); var mailaddress = new mailaddress(to); email.subject = subject; email.body = message; var mailaddresscollection = new mailaddresscollection(); mailaddresscollection.add(mailaddress); email.to = (mailaddresscollection); return await send(email); } now prove problem broke down code single lines can see on line breaks. here found:
when debug , step sendregisteremail method, line says var mailaddress = new mailaddress(to); gets run , exits out function , runs line return view("index"); , page loads up. weird thing added logging method on send @ end , logging never gets hit. put breakpoint on send , never got hit. if creation of email crashed, decided exit out caller function , continued code.
i don't have faintest clue why.
sendregisteremail method asynchronous, you're not awaiting it.
currently, start fire-and-forget operation , return view instruction executed while you're creating new mailaddress instance. throws formatexception can't catch because exception thrown in thread.
update action method
public async task <actionresult> sendemail() { await sendregisteremail("test", "testagain", "lasttest"); return view("index"); } also awaiting before return not needed, can change
public task<bool> sendregisteremail(string subject, string message, string to) { var email = new email(); // ... return send(email); } but that's not related problem you've got.
Comments
Post a Comment