2

This issue first started cropping up when I tried to implement the SendGrid API in my application. I was getting null reference exceptions thrown up by mscorlib.dll anytime I tried to send a message.

In the end I tried to implement the functionality I needed using System.Net.Http.HttpClient resulting in the following test function:

    public async System.Threading.Tasks.Task mySendGridFunction(SendGridMessage message)
    {
        HttpClient client = new HttpClient();

        var requestContent = new FormUrlEncodedContent(new[]{
            new KeyValuePair<string, string>("api_user", "XXXXXXXXX"),
            new KeyValuePair<string, string>("api_key", "XXXXXXXX"),
            new KeyValuePair<string, string>("to", "shocklanced@gmail.com"),
            new KeyValuePair<string, string>("subject", "STEP-A Summary"),
            new KeyValuePair<string, string>("text", "Test Mail"),
            new KeyValuePair<string, string>("from", "davidrtye@gmail.com"),
            new KeyValuePair<string, string>("fromname", "STEP-A Automated Message"),
        });

        HttpResponseMessage response = await client.PostAsync("https://api.sendgrid.com/api/mail.send.xml", requestContent);

        HttpContent responseContent = response.Content; /*For Debugging purposes*/
    }

When I call the PostAsync() function I get a null exception thrown in System.Web.dll followed by mscorlib.dll if I try to continue.

I can't find any other instances of this happening, so I assume there must be something happening on my end that I'm missing, but I can't work out what.

Edit As requested, here are the screenshots for the Error, and stack trace Error and Disassembly

Stack Trace

Edit 2 The code appears to be working, but the response from the sendgrid service is somehow generating a null reference exception deep in mscorlib.dll. I don't have time to really dig into this for now, so I will just try to manage the issue as best I can without fixing it.

Sometimes it is actually able to pass a response out of the call to PostAsync, but not often

Shocklanced
  • 123
  • 1
  • 1
  • 8
  • I run this sample code, and it works fine. The result returned is a xml content: `errorBad username / password` – Taher Rahgooy Aug 12 '15 at 04:48
  • So it seems to be a weird issue on my end. Not sure what settings might cause it though – Shocklanced Aug 12 '15 at 05:28
  • Can you please post the exception with stacktrace? – Deepak Bhatia Aug 13 '15 at 02:15
  • There isn't a `System.NullException`. There's a `System.NullReferenceException` and there's a handy guide to debugging these available under [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Damien_The_Unbeliever Aug 13 '15 at 06:32
  • Hmm, I hadn't noticed that there was actually two errors cropping up. I'll try to find the trace and exception for the Null Exception – Shocklanced Aug 14 '15 at 04:19
  • Looks like (as far as I can tell) the mscorlib is not handling the response from the Sendgrid webapi. I fixed a typo in the password, and it now sends emails, however the response never comes back without a null reference exception being generated ( and it's deep in the c# source so unless you have a handy guide to debugging that .... ) – Shocklanced Aug 24 '15 at 03:02

0 Answers0