编译需要bindingRedirect的C#库项目

我正在引用第三方dll,它需要app.config文件中的bindingRedirect,如下所示:

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

我试图在C#库项目中引用它.该库将在WCF项目和MVC项目之间共享.
但是我的图书馆再也找不到参考文献了.我收到以下警告:

The primary reference “ThirdParty” could not be resolved because it
has an indirect dependency on the framework assembly “System.Net.Http,
Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”
which could not be resolved in the currently targeted framework.
“.NETFramework,Version=v4.0”. To resolve this problem, either remove
the reference “ThirdParty” or retarget your application to a framework
version which contains “System.Net.Http, Version=1.5.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”

我已经使用所需的bindingRedirect将app.config文件添加到库中.但是,此配置将被忽略.

我也尝试将库转换为控制台应用程序.在这种情况下,它编译完美并且有效.

如何编译需要assemblyBinding来执行bindingRedirect的库项目?

最佳答案 您收到编译时错误,因为第三方DLL公开System.Net.Http中的类型.您只需要添加对System.Net.Http程序集的引用即可解决它.

使用Nuget这样做.你想要Microsoft HTTP Client Libraries package.

点赞