Sendgrid-csharp: response.Body or StatusCode cannot be defined

Created on 23 Aug 2016  路  10Comments  路  Source: sendgrid/sendgrid-csharp

Issue Summary

I am running two projects one with an older version of the library (7.0.5) and one with the latest (8.0.3) - when adding a recipient to the list in the old version I can retrieve the Body or StatusCode from the dynamic response.

However with the latest build this results in the error:

[RuntimeBinderException: 'System.Runtime.CompilerServices.ConfiguredTaskAwaitable' does not contain a definition for 'StatusCode']
CallSite.Target(Closure , CallSite , Object ) +141
System.Dynamic.UpdateDelegates.UpdateAndExecute1(CallSite site, T0 arg0) +517

Steps to Reproduce

The code is as below:

SendGridRecipient recipient = new SendGridRecipient(email, first_name, last_name);
string data = @"[" + JsonConvert.SerializeObject(recipient) + "]";
dynamic response = sg.client.contactdb.recipients.post(requestBody: data);
return response.StatusCode;

*note that the recipient is SUCCESSFULLY added to the SendGrid contact list - I just cannot read the response.

help wanted question

All 10 comments

Hello @nigelwong78,

Could you please give me the value of data before it's passed into the post function so that I can try and reproduce?

Thanks!

Same issue here
maybe its because of the synchronous call :
dynamic response = sg.client.contactdb.recipients.post(requestBody: data);

in the documentation examples there is an "await" that tells the call should be in an async task
but we should be able to make a synchronous call
dynamic response = await sg.client.contactdb.recipients.post(requestBody: data);

I think you are correct @Benraay.

@nigelwong78

You can do sg.client.contactdb.recipients.post(requestBody: data).Result or sg.client.contactdb.recipients.post(requestBody: data).Wait().

does not work I got
'System.Runtime.CompilerServices.ConfiguredTaskAwaitable<SendGrid.CSharp.HTTP.Client.Response> does not contain a definition for 'Wait'

I reverted to 7.1.1 in that version all works great without any need for "Reply" or "Wait()" method
maybe something important changed in 8.X.X version on async calls

Can you try:

sg.client.contactdb.recipients.post(requestBody: data).GetAwaiter().GetResult()

Thanks!

@thinkingserious I was also unable to print status due to dynamic response and got same exception as OP. I wanted to let you know that your last suggestion worked. This is what I did to be able to print response information in debug mode.

dynamic response = sg.client.mail.send.post(requestBody: mail.Get()).GetAwaiter().GetResult(); string statusCode = ((object)response.StatusCode).ToString(); string headerString = response.Headers.ToString() as string;

It took me quite a while to land on this post. Just a suggestion, that you may want to update the documentation or examples.

Cheers!

Thank you @abhijitparalikar!

I will leave this ticket open until we get the documentation updated.

I got same error when update from v7.0.01 to v8 also
Adding .GetAwaiter().GetResult();

Did now it does not accepts the json payload that has been sent.
Have them in unit test and just get this error back

{\"errors\":[{\"message\":\"Bad Request\",\"field\":null,\"help\":null}]

So has the json format changes also?

This is payload that Im sending (I changed email address but format is same)
And payload is valid, so does v8 change payload also ?

{'personalizations': [ {'to': [ {'email': '[email protected]'} ],'subject': 'Unit Test'} ],'from': {'email': '[email protected]','name': 'ME'},'content': [ {'type': 'text/html','value': 'Test body'}]}

//// THIS IS CODE THAT USED IN C# ///////
dynamic response = sendGridApi.client.mail.send.post(requestBody: jsonPayload).GetAwaiter().GetResult();
var statusCode = response.StatusCode?.ToString();
var result = response.Body.ReadAsStringAsync().Result?.ToString();

@abhijitparalikar and @Benraay after you got the result tag to work
Did you have to change how you created the payload ?

As I just get errors when trying to send data now (as above)

Have found the issue with the payload
Prior version allowed ' around key and value --> {'personalizations':
Now it has to have " so it becomes {"personalizations":

So that must have been a breaking change between 7.0.0.1 and current

Was this page helpful?
0 / 5 - 0 ratings