Sendgrid-csharp: Exception when creating SendGridAPIClient

Created on 23 May 2017  路  22Comments  路  Source: sendgrid/sendgrid-csharp

Issue Summary

I get the following exception when attempting to use SendGrid:

An unhandled exception occurred while processing the request.

FileLoadException: Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
System.Net.Http.WinHttpHandler..cctor()

TypeInitializationException: The type initializer for 'System.Net.Http.WinHttpHandler' threw an exception.
System.Net.Http.WinHttpHandler..ctor()

Steps to Reproduce

var apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY"); var client = new SendGridClient(apiKey); var from = new EmailAddress("[email protected]", "Example User"); var subject = "Sending with SendGrid is Fun"; var to = new EmailAddress("[email protected]", "Example User"); var plainTextContent = "and easy to do anywhere, even with C#"; var htmlContent = "<strong>and easy to do anywhere, even with C#</strong>"; var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent); var response = await client.SendEmailAsync(msg);

Technical details:

This is from a .net core web application targeting the full framework as follows:
net461

Below are the full package references:

<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Routing" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.0">
      <PrivateAssets>All</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.FileProviders.Abstractions" Version="1.1.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="1.0.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.0" />
    <PackageReference Include="AutoMapper" Version="6.0.2" />
    <PackageReference Include="Sendgrid" Version="9.4.1" />
    <PackageReference Include="WindowsAzure.Storage" Version="8.1.1" />
    <PackageReference Include="Microsoft.Extensions.Primitives" Version="1.1.0" />
  </ItemGroup>

  <ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
    <Reference Include="System.Device" />
    <Reference Include="System.Drawing" />
    <Reference Include="System" />
    <Reference Include="Microsoft.CSharp" />
  </ItemGroup>
medium help wanted help wanted bug up for grabs up-for-grabs

Most helpful comment

@dannythomas13,

That is the correct approach; however, it looks like we have to fix some dependency issues for VS 2017 Core 1.1.* projects.

All 22 comments

Hello @dannythomas13,

What version of Visual Studio are you using and on what OS?

With Best Regards,

Elmer

@thinkingserious thanks for getting back to me, I am using VS2017 Professional on Windows 7 64 bit

I have same problem, but I am using VS2017 Enterprise on Windows 10 64 bit

Thanks for adding your voice to this issue. I've added your vote to this issue in our backlog to help increase its priority.

@shurikev I desperately need to be able to send emails from an azure app for a current project if you find another solution before this is resolved can you let me know please?

@dannythomas13 have you tried using the SendGrid addon within Azure?

@dannythomas13 to work around the problem you are experiencing you could also consider StrongGrid

@Jericho, do have an idea on what the problem could be?

@thinkingserious just a wild guess: the problem seems to be regarding a missing assembly called System.Diagnostics.DiagnosticSource, so maybe add it to your package?

Thanks @Jericho!

@dannythomas13,

Could you please try adding that assembly to your project to see if that resolves the issue?

Also, I could not replicate this on Visual Studio 2015, so I will get 2017 going to try and replicate there.

Thanks!

@Jericho @thinkingserious difficult for me as when i add reference to 4.0.0 i get lots of warning that i am downgrading from 4.3.0 which is required from all of the 1.1.* asp core libraries

@thinkingserious regarding the azure addon i assumed this is what i had but still needed to code to send messages? I was following this guide:
https://docs.microsoft.com/en-us/azure/app-service-web/sendgrid-dotnet-how-to-send-email
Is this not the correct appraoch?

Thanks @Jericho!

It looks like the fix is to reference System.Net.Http 4.3.1. I'll give it a try.

@dannythomas13,

That is the correct approach; however, it looks like we have to fix some dependency issues for VS 2017 Core 1.1.* projects.

@thinkingserious is this something that could be done within the next few weeks? If not is there a workaround?

Hi @dannythomas13,

It could be done within the next few weeks if we get a significant amount of people voting on this issue or best if someone submits a PR. Those things help push the item up our backlog queue.

@thinkingserious Did this issue make the next release? :)

Hi @dannythomas13,

Unfortunately, this issue is still fairly low on our backlog. It can gain momentum with additional votes or a PR.

Thanks for checking in!

With Best Regards,

Elmer

Hi Elmer, do you have a public facing backlog so I can keep an eye on the priority of this issue please?

Hi @dannythomas13,

We do not right now, but I'm going to discuss this with my product manager today and see if we can come up with a better way to inform on our progress.

With Best Regards,

Elmer

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

clairernovotny picture clairernovotny  路  4Comments

lebamb2 picture lebamb2  路  4Comments

eat-sleep-code picture eat-sleep-code  路  3Comments

bogacg picture bogacg  路  3Comments

shiglet picture shiglet  路  3Comments