sendgrid-csharp icon indicating copy to clipboard operation
sendgrid-csharp copied to clipboard

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

Open hunglee opened this issue 10 years ago • 66 comments

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

hunglee avatar Jun 15 '16 06:06 hunglee

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

trinathm avatar Jun 15 '16 17:06 trinathm

@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 avatar Jun 15 '16 19:06 thinkingserious

@thinkingserious yes, the email was sent successfully. The response is also not returned with Web API and Classic Windows Form. Thanks

hunglee avatar Jun 16 '16 03:06 hunglee

@thinkingserious Yes, Email are being sent successfully, but after posting mail, its not returning response.

trinathm avatar Jun 16 '16 06:06 trinathm

same here...

ChrisRacki avatar Jun 16 '16 07:06 ChrisRacki

@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!

joantorres avatar Jun 16 '16 18:06 joantorres

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).

thinkingserious avatar Jun 16 '16 18:06 thinkingserious

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.

matthewbcline avatar Jun 16 '16 20:06 matthewbcline

Thanks @matthewbcline!

What does the code look like that calls SendEmail()?

Seems like we need to have .ConfigureAwait(false); here and here.

thinkingserious avatar Jun 16 '16 21:06 thinkingserious

@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:

matthewbcline avatar Jun 16 '16 21:06 matthewbcline

Agreed, I will try that fix and report back. Thanks!

thinkingserious avatar Jun 16 '16 21:06 thinkingserious

@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 avatar Jun 16 '16 21:06 alanhouck

@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] :)

thinkingserious avatar Jun 16 '16 21:06 thinkingserious

This issue is fixed in version 7.0.2: https://github.com/sendgrid/sendgrid-csharp/releases/tag/v7.0.2

thinkingserious avatar Jun 17 '16 04:06 thinkingserious

@thinkingserious This worked! Thanks a lot for the quick fix, and for the T-shirt ;-)

joantorres avatar Jun 17 '16 06:06 joantorres

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 avatar Jun 22 '16 16:06 PranKe01

@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

thinkingserious avatar Jun 24 '16 21:06 thinkingserious

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 avatar Jun 26 '16 15:06 PranKe01

@PranKe01 what is the exception you are receiving?

thinkingserious avatar Jun 26 '16 15:06 thinkingserious

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.

PranKe01 avatar Jun 26 '16 15:06 PranKe01