Sendgrid-csharp: Upgraded to latest JSON.Net and now SendGrid throws an exception when sending mail

Created on 27 Apr 2017  路  6Comments  路  Source: sendgrid/sendgrid-csharp

Issue Summary

Cannot send mail with SendGrid API if you upgrade to JSON.Net version 10. The SendGrid API is targeting JSON.Net 9.0, but it appears from the exception that it is expecting a specific JSON.Net version (in particular the one that is packaged with it in NuGet). This creates a problem when you have a large code base that is using other NuGet packages and you make the upgrade. This is not a compile time exception, this occurs when the code runs.

Steps to Reproduce

Just build a simple console app in VS. Use NuGet package manager to add JSON.Net and then add SendGrid. Since the SendGrid package is marked JSON.Net >= 9,0.1 there is no issue with the install. Run this code and you will get this exception.

var client = new SendGridClient(SendGridApiKey);
var message = new SendGridMessage()
{
    From = new EmailAddress("[email protected]"),
    PlainTextContent = "This is a test message",
    Subject = "Testing New Send Grid Api"
};
message.AddTo(new EmailAddress("[email protected]"));
var response = await client.SendEmailAsync(message);

I consider this a bug because the package claims to be able to work with JSON.Net >= 9.0.1. I don't believe there were any breaking changes between JSON.Net 9.0 and 10.0. If there was a change of that nature then I would not consider this a bug, just a need to upgrade the package (of course that would have failed differently with a missing method or signature or interface type of exception)

I have not built NuGet packages so I don't know how you go about doing that, but it seems that if when building your DLL, you added Json.Net via NuGet you would end up with a tag like this in the CSPROJ file, I believe that is the piece that is missing and causing this to fail.
False

Technical details:

System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
at SendGrid.Helpers.Mail.SendGridMessage.Serialize()
at SendGrid.SendGridClient.d__21.MoveNext() in C:\Users\elmer\Documents\GitHub\sendgrid-csharp\src\SendGrid\SendGridClient.cs:line 263

  • sendgrid-csharp Version: SendGrid 9.1.1 (installed via NuGet)
  • .NET Version: 4.5.2
easy help wanted help wanted community enhancement up for grabs up-for-grabs

All 6 comments

Thank you for the detailed report @jshergal!

I agree, it looks like something is broken in the packaging of the .dll. I've added this to our backlog for further investigation and fix.

With Best Regards,

Elmer

I was just working on an update that targeted json.net v10 but a quick test shows that sendgrid v9 works with this version. It just requires a binding redirect, which NuGet should have added automatically. Check your app.config or web.config and make sure you have the following entry in it and if you don't add it and try again. If you don't have a config you'll need to add one.

diff --git a/src/Website/Web.config b/src/Website/Web.config
index ebef20b..30ef992 100644
--- a/src/Website/Web.config
+++ b/src/Website/Web.config
@@ -128,7 +128,7 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
             <dependentAssembly>
                 <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
-                <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
+                <bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
             </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
This is still an issue for me. I am using Newtonsoft.Json v10.0.0

Thank you for the feedback @sambupraveen! I have added your vote to this issue.

It is working fine now, my mistake i took wrong api key.

Since there has been no activity on this issue since March 1, 2020, we are closing this issue. Please feel free to reopen or create a new issue if you still require assistance. Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

loganwasif005 picture loganwasif005  路  3Comments

ivivanov picture ivivanov  路  3Comments

AndrewTziAnChan picture AndrewTziAnChan  路  3Comments

CalvinFengDatacom picture CalvinFengDatacom  路  4Comments

kquinbar picture kquinbar  路  4Comments