DLL Couldn't Be Loading Because of Binding Redirect

2

I have a DLL project that uses the SendGrid Nuget package. The SendGrid package requires the Nuget package System.Net.Http 4.3.4. This DLL project I've then packaged into a Nuget package.

Now, I have a ASP.NET MVC project that includes my Nuget package. Because of dependency requirements installing it installs SendGrid, System.Net.Http, and other dependencies.

Now, when I have my web app try to send an email I got the following error.

System.IO.FileNotFoundException
HResult=0x80070002
Message=Could not load file or assembly 'System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
Source=Bc3.Mail
StackTrace:
  at Bc3.Mail.MailHelper.SendMessage(SendGridMessage message) in C:\Code\Shared Libraries\Bc3.Mail\Bc3.Mail\MailHelper.cs:line 166
  at Bc3.Mail.MailHelper.SendMail(MailMessage mail) in C:\Code\Shared Libraries\Bc3.Mail\Bc3.Mail\MailHelper.cs:line 128
…

Now, I'm confused since I thought version 4.3.4 was installed. I checked the version of System.Net.Http in all my projects and it says it's version 4.2.0.0. Ok, I'm confused why it's showing 4.2 when I thought I installed 4.3.4. However, if 4.2 is referenced in my project how could it not find it?

Anyway, next I looked at my web.config file and saw this

<dependentAssembly>
    <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
    <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0"/>
</dependentAssembly>

I don't know anything about these binding redirect. But I decided to remove this and everything worked fine.

So my question is why?

Thanks

c#
asp.net-mvc
assembly-binding-redirect
asked on Stack Overflow Oct 15, 2019 by Skye MacMaster • edited Oct 15, 2019 by Abolfazl

1 Answer

1

About binding redirects

Binding redirects allow the runtime to select alternative versions of a specific assembly. Binding redirects can be helpful because they reduce the strictness for binding to a specific version of an assembly. But, they can bring about conflicts.

https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/redirect-assembly-versions

Binding redirect after adding SendGrid

I noticed that adding a NUGET reference to SendGrid inserted the following binding redirects

  <dependentAssembly>
    <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.1.2" newVersion="4.1.1.2" />
  </dependentAssembly>

Creating a NUGET package that uses SendGrid

I created a simple class library project which references SendGrid and made a NUGET package. I placed this NUGET package in my private feeds folder and then referenced this NUGET package from another Framework project. The compiled .CONFIG file had the following binding redirect

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
      </dependentAssembly>

Possibility

My thoughts:

  1. The binding redirect that you are seeing in the web.config might have been brought about by an inadvertent reference to some other NUGET package
  2. Allow the compiler to generate binding redirects during the Build operation as opposed to manually adding binding redirects in the Web.config or app.config . The Project setting --> Application tab --> Auto-generate binding redirects should be checked.

Hope this helps.

answered on Stack Overflow Oct 16, 2019 by Sau001 • edited Oct 16, 2019 by Sau001

User contributions licensed under CC BY-SA 3.0