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("[email protected]");
String subject = "Hello World from the SendGrid CSharp Library";
Email to = new Email("[email protected]");
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
I am also facing same issue. Its working fine with console app. but when integrated with MVC application, its not returning response.
@hunglee thanks for letting us know. We will be digging deeper to figure out what is going wrong.
Any additional details are appreciated.
@hunglee @trinathm Even though the response is not returned, does the email get sent?
@thinkingserious yes, the email was sent successfully. The response is also not returned with Web API and Classic Windows Form. Thanks
@thinkingserious Yes, Email are being sent successfully, but after posting mail, its not returning response.
same here...
@thinkingserious Any estimation on when this bug will be fixed?? I was just integrating this library into a new project and it doesn't work at all, but it's quite urgent and if you don't have a hot fix by tomorrow I will have to go find another alternative. I tried to use the previous version 6.3.4 but it does not support templates, so it doesn't work for me. Thanks for the help!
Hello Everyone,
@hunglee @trinathm @ChrisRacki @joantorres @jahmai @walmon
First, thanks for your feedback and patience.
I've done some digging and I can't reproduce in the console app (as mentioned, it seems to work there) so here is what I'm thinking (I would love to get some feedback):
The API call happens here which calls this function.
So perhaps this call dynamic response = sg.client.mail.send.post(requestBody: mail.Get()); should be dynamic response = await sg.client.mail.send.post(requestBody: mail.Get());
Thoughts?
I still have more digging to do, as I don't have an environment where I see this error reproduced (if anyone can help with that, I would be greatly appreciative).
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("[email protected]");
var subject = "Subject";
var to = new Email("[email protected]");
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.
Thanks @matthewbcline!
What does the code look like that calls SendEmail()?
Seems like we need to have .ConfigureAwait(false); here and here.
@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). :wink:
Agreed, I will try that fix and report back. Thanks!
@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!
@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 [email protected] :)
This issue is fixed in version 7.0.2: https://github.com/sendgrid/sendgrid-csharp/releases/tag/v7.0.2
@thinkingserious This worked! Thanks a lot for the quick fix, and for the T-shirt ;-)
Now, which one is the correct call?
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);
@PranKe01 whether or not you include the await keyword depends on your desired behavior, please see: https://msdn.microsoft.com/en-us/library/hh156528.aspx
When using dynamic response = await sendGridApi.client.mail.send.post(requestBody: mail.Get()); instead of dynamic response = sendGridApi.client.mail.send.post(requestBody: mail.Get()); I get an exception.
@PranKe01 what is the exception you are receiving?
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.