sendgrid-csharp: dynamic response = await sg.SendEmailAsync(anEmail); // hangs waiting for response
Issue Summary
A few days ago, around Aug 25, 2017, I updated to all newest SendGrid code. First found out I had to modify my source code (not happy about that – oh well). I follow example. I’m running C#, .NET Version: 4.5.2.
Steps to Reproduce
My code is this:
// =============
static async Task Execute(MailAddress fromEmail, MailAddress toEmail, string subject, string inHtmlContent, string inTextContent) {
SendGrid.Helpers.Mail.SendGridMessage anEmail = MailHelper.CreateSingleEmail(new EmailAddress(fromEmail.Address,fromEmail.DisplayName), new EmailAddress(toEmail.Address, toEmail.DisplayName), subject, inTextContent, inHtmlContent);
bool sendMail = false;
if (inTextContent.Length > 0) {
sendMail = true;
}
if (inHtmlContent.Length > 0) {
sendMail = true;
}
if (sendMail) {
dynamic sg = new SendGridClient(xxxxxxxxxxx);
dynamic response = await sg.SendEmailAsync(anEmail); // get hang here waiting, seemingly forever
string resp1 = response.StatusCode.ToString();
string resp2 = response.Body.ReadAsStringAsync().Result.ToString();
string resp3 = response.Headers.ToString();
int x = 1; // so I can break here to see response
}
}
// =============
Above code sent emails fine, but UI was blocked waiting for response.
I later modified my code so I did not wait for response. Emails would send fine.
Yesterday I tried to send. Emails would not send. Later, with same code, emails would send.
I think when response hangs you have tasks accumulate and eventually take your server down.
Technical details:
- I am running SendGrid - Web API v3 v9.9.0 SendGrid.CSharp.HTTP.Client v3.3.0 SendGrid.Net40 v5.1.0 SendGrid.SmtpApi v1.3.3 .NET Version: 4.5.2 Building on Microsoft Visual Studio 2017 I note that this looks very much like a problem you had in older code about a year ago: https://github.com/sendgrid/sendgrid-csharp/issues/235 https://github.com/sendgrid/sendgrid-csharp/issues/453
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 15 (6 by maintainers)
I use same call, just put .ConfigureAwait(false) at end. So now it is SendGrid.Response response = await sg.SendEmailAsync(anEmail).ConfigureAwait(false); It works! Looking at response.Headers.ToString() I get nX-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html . I’m now trying to understand if I have a security issue with my code. I don’t think so. It’s taken me some days to get to this point. I didn’t know I was supposed to use only SendGrid, not the other 3 libraries. I knew nothing about .ConfigureAwait(false) . How should I have known?