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:

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 15 (6 by maintainers)

Most upvoted comments

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?