sendgrid-csharp: Async broken in library, causing deadlocks and responses not returning in non-console apps

Hi, I’m using the example code to send email on MVC web app but it not response on the line dynamic response = sg.client.mail.send.post(requestBody: mail.Get()); even it has been sent successfully. Am I missing something?

String apiKey = "APIKEY";
dynamic sg = new SendGrid.SendGridAPIClient(apiKey);

Email from = new Email("test@example.com");
String subject = "Hello World from the SendGrid CSharp Library";
Email to = new Email("test@example.com");
Content content = new Content("text/plain", "Textual content");
Mail mail = new Mail(from, subject, to, content);

 dynamic response = sg.client.mail.send.post(requestBody: mail.Get());

Note: the example code work fine on console app only (response after post)

Thanks

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 1
  • Comments: 66 (31 by maintainers)

Commits related to this issue

Most upvoted comments

@alanhouck thanks for confirming!!!

I will push out an update and I’d like to swag out everyone on this thread. Please send your T-shirt size and mailing address to dx@sendgrid.com 😃

Please try:

  1. Uncomment return response
  2. Replace SendAsync(email).Wait(); with
SendAsync(email).Result;
Response result = response;
var statusCode = result.StatusCode.ToString(); // log this value appropriately so that you can read the result
  1. Replace public async Task SendAsync(Common.Emailing.Email email) with public static async Task<Response> SendAsync(Common.Emailing.Email email)

Also, when you say "The only email that gets delivered is the one sent to “tawani@gmail.com”. Do you mean that when you try your code with that email it works fine, but when you try again with any other email it does not work? Could you try sending to dx@sendgrid.com?

Hello @tawani,

I have tried to reproduce, but I am not getting any errors, the button gets updated with the string “Accepted” and the email gets delivered with the following code:

using System;
using SendGrid;
using SendGrid.Helpers.Mail;
using System.Threading.Tasks;

namespace TestingWebForms
{

	public partial class Default : System.Web.UI.Page
	{
		public void button1Clicked(object sender, EventArgs args)
		{
			var response = Execute().Result;
			Response result = response;
                        button1.Text = result.StatusCode.ToString();
		}

		public static async Task<Response> Execute()
		{
			var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
			var client = new SendGridClient(apiKey);
			var from = new EmailAddress("dx@sendgrid.com", "DX ");
			var subject = "Hello World from the SendGrid CSharp SDK!";
			var to = new EmailAddress("example@example.com", "Elmer Thomas");
			var plainTextContent = "Hello, Email!";
			var htmlContent = "<strong>Hello, Email!</strong>";
			var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
                        var response = await client.SendEmailAsync(msg);
			return response;
		}
	}
}

Please take a look and let me know what you think.

An exception of type ‘Microsoft.CSharp.RuntimeBinder.RuntimeBinderException’ occurred in System.Web.Mvc.dll but was not handled in user code

Additional information: SendGrid.CSharp.HTTP.Client.Response enthält keine Definition für GetAwaiter.

@thinkingserious I was experimenting with the ConfigureAwait(false) earlier today and had no joy. Thankfully I checked this thread and tried adding it to the exact spots in the code you mentioned and it worked for me. Thanks!

@thinkingserious SendEmail() is a Web API method; it’s triggered by an HTTP POST. So it’s called by the Web API routing framework.

I had a sneaking suspicion that the solution would involve ConfigureAwait(false). 😉

I can reliably reproduce this in a Web API project. Here is my method:

[Route("sendEmail"), HttpPost, AllowAnonymous]
public virtual async Task<IHttpActionResult> SendEmail()
{
    var apiKey = "my api key";
    var from = new Email("from@example.com");
    var subject = "Subject";
    var to = new Email("to@example.com");
    var textContent = new Content("text/plain", "Content");
    var mail = new Mail(from, subject, to, textContent);

    dynamic sendGridClient = new SendGridAPIClient(apiKey);
    // All three of these calls never return. The email message is sent, however.
    dynamic response = sendGridClient.client.mail.send.post(requestBody: mail.Get());
    dynamic response = await sendGridClient.client.mail.send.post(requestBody: mail.Get());
    dynamic response = await (sendGridClient.client.mail.send.post(requestBody: mail.Get())).ConfigureAwait(false);

    // This line is never executed.
    return this.Ok();
}

I cannot use the client library at all until this is resolved. I’ll be making “raw” HTTP requests instead using HttpClient.

I am also facing same issue. Its working fine with console app. but when integrated with MVC application, its not returning response.