将 bindingRedirect 添加到 .Net 标准库
Adding a bindingRedirect to a .Net Standard library
我有一个 .Net Standard 库,在尝试使用其中一个依赖库时遇到错误,我认为这是版本冲突造成的。在旧式 .Net Class 库中,我可能会添加如下内容:
<?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-10.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
但是,我显然不能在 Net Standard 库中这样做;所以,我的问题是,在 .Net Standard 世界中解决此类问题的策略是什么?
绑定重定向是 .NET 框架的概念,.NET Standard 和 .NET Core 上没有绑定重定向。
但是,应用程序(实际的.NET Framework 或.NET Core 应用程序)需要解析要使用的文件。在 .NET Core 上,这是通过基于构建输入生成一个 deps.json
文件来完成的,并且 .NET Framework 应用程序使用绑定重定向。
如果需要绑定重定向,则必须将它们添加到使用 .NET Standard 库的 .NET Framework 应用程序(或库)。
这些绑定重定向可以配置为在构建期间自动生成,基于编译期间使用的程序集,请参阅 documentation on automatic binding redirects. When using NuGet's new PackageReference
style of using NuGet packages, this is done automatically. Since configuring this correctly varies based on the project type, refer to the announcement Issues with .NET Standard 2.0 with .NET Framework & NuGet 了解详细说明。
确保使用正确的绑定重定向的最简单方法是确保 .NET Framework 应用程序或库设置这些属性(在 csproj/vbproj 内)。生成的项目不需要第二个.exe 可执行文件但单元测试项目需要):
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
我有一个 .Net Standard 库,在尝试使用其中一个依赖库时遇到错误,我认为这是版本冲突造成的。在旧式 .Net Class 库中,我可能会添加如下内容:
<?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-10.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
但是,我显然不能在 Net Standard 库中这样做;所以,我的问题是,在 .Net Standard 世界中解决此类问题的策略是什么?
绑定重定向是 .NET 框架的概念,.NET Standard 和 .NET Core 上没有绑定重定向。
但是,应用程序(实际的.NET Framework 或.NET Core 应用程序)需要解析要使用的文件。在 .NET Core 上,这是通过基于构建输入生成一个 deps.json
文件来完成的,并且 .NET Framework 应用程序使用绑定重定向。
如果需要绑定重定向,则必须将它们添加到使用 .NET Standard 库的 .NET Framework 应用程序(或库)。
这些绑定重定向可以配置为在构建期间自动生成,基于编译期间使用的程序集,请参阅 documentation on automatic binding redirects. When using NuGet's new PackageReference
style of using NuGet packages, this is done automatically. Since configuring this correctly varies based on the project type, refer to the announcement Issues with .NET Standard 2.0 with .NET Framework & NuGet 了解详细说明。
确保使用正确的绑定重定向的最简单方法是确保 .NET Framework 应用程序或库设置这些属性(在 csproj/vbproj 内)。生成的项目不需要第二个.exe 可执行文件但单元测试项目需要):
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>