且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将bindingRedirect添加到.Net标准库

更新时间:2023-12-02 22:09:22

绑定重定向是一种.NET Framework概念,.NET Standard和.NET Core上没有绑定重定向。

Binding redirects are a .NET framework concept, there are no binding redirects on .NET Standard and .NET Core.

但是,一个应用程序(实际的.NET Framework或.NET Core应用程序)需要解决要使用的文件。在.NET Core上,这是通过根据生成的输入生成 deps.json 文件和.NET Framework应用程序使用绑定重定向来完成的。

However, an application (the actual .NET Framework or .NET Core application) need to resolve the files to be used. On .NET Core, this is done by generating a deps.json file based on the build input and a .NET Framework application uses binding redirects.

如果必须进行绑定重定向,则必须将其添加到使用.NET Standard库的.NET Framework应用程序(或库)中。

If a binding redirect is necessary, they have to be added to the .NET Framework application (or library) that used the .NET Standard library.

可以将这些绑定重定向配置为在编译期间根据编译期间使用的程序集自动生成,请参见有关自动绑定重定向的文档。当使用NuGet使用新的 PackageReference 样式使用NuGet软件包时,此操作会自动完成。由于根据项目类型的不同,正确配置的方法有所不同,因此请参考 .NET Standard 2.0 with的问题。 NET框架和NuGet 详细说明。

These binding redirects can be configured to be automatically generated during build, based on the assemblies used during compilation, see the 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 for detailed descriptions.

确保使用正确的绑定重定向的最简单方法是确保.NET Framework应用程序或库设置这些属性(在csproj / vbproj内部。生成.exe可执行文件的项目不需要第二个,但单元测试项目则需要):

The simplest way to make sure that the correct binding redirects are used is to ensure the .NET Framework app or library sets these properties (inside the csproj/vbproj. The second one is not needed for projects that generate .exe executables but needed for unit test projects):

<PropertyGroup>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>