Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 由于绑定重定向,无法加载DLL_C#_Asp.net Mvc_Assembly Binding Redirect - Fatal编程技术网

C# 由于绑定重定向,无法加载DLL

C# 由于绑定重定向,无法加载DLL,c#,asp.net-mvc,assembly-binding-redirect,C#,Asp.net Mvc,Assembly Binding Redirect,我有一个使用SendGrid Nuget包的DLL项目。SendGrid包需要Nuget包System.Net.Http 4.3.4。我将这个DLL项目打包成一个Nuget包 现在,我有一个ASP.NETMVC项目,其中包括我的Nuget包。由于安装时的依赖项要求,它将安装SendGrid、System.Net.Http和其他依赖项 现在,当我有我的web应用程序尝试发送电子邮件时,我得到了以下错误 System.IO.FileNotFoundException HResult=0x800700

我有一个使用SendGrid Nuget包的DLL项目。SendGrid包需要Nuget包System.Net.Http 4.3.4。我将这个DLL项目打包成一个Nuget包

现在,我有一个ASP.NETMVC项目,其中包括我的Nuget包。由于安装时的依赖项要求,它将安装SendGrid、System.Net.Http和其他依赖项

现在,当我有我的web应用程序尝试发送电子邮件时,我得到了以下错误

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
…
现在,我很困惑,因为我以为安装了4.3.4版。我在所有的项目中都检查了System.Net.Http的版本,它说它是4.2.0.0版本。好吧,我不明白为什么我以为我安装了4.3.4,却显示了4.2。然而,如果在我的项目中引用了4.2,它怎么会找不到呢

无论如何,接下来我查看了我的web.config文件,看到了这个

<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>
我对这些绑定重定向一无所知。但我决定去掉这个,一切都很好

所以我的问题是为什么

感谢有关绑定重定向的信息

  <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>
绑定重定向允许运行时选择特定程序集的替代版本。绑定重定向会很有帮助,因为它们降低了绑定到程序集特定版本的严格性。但是,它们会带来冲突

添加SendGrid后的绑定重定向

我注意到向SendGrid添加NUGET引用插入了以下绑定重定向

  <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>
创建使用SendGrid的NUGET包

我创建了一个引用SendGrid的简单类库项目,并制作了一个NUGET包。我将这个NUGET包放在我的私有提要文件夹中,然后从另一个框架项目引用了这个NUGET包。编译的.CONFIG文件具有以下绑定重定向

<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>
可能性

我的想法:

您在web.config中看到的绑定重定向可能是由于无意中引用了其他NUGET包而导致的 允许编译器在生成操作期间生成绑定重定向,而不是在Web.config或app.config中手动添加绑定重定向。应选中项目设置->应用程序选项卡->自动生成绑定重定向。
希望这有帮助。

奇怪。我所有的项目都有自动生成集。我认为这是在安装更新版本的Nuget软件包时发生的。也许其中一个装置不能正常工作。