将 bindingRedirect 添加到 .Net Standard 库

编程入门 行业动态 更新时间:2024-10-27 05:36:46
本文介绍了将 bindingRedirect 添加到 .Net Standard 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 .Net Standard 库,但在尝试使用其中一个依赖库时出现错误,我认为这归结为版本冲突.在旧式 .Net 类库中,我可能会添加如下内容:

I have a .Net Standard library, and I'm getting an error when trying to use one of the dependant libraries, which I believe is down to a version conflict. In an old style .Net Class library, I might add something like this:

<?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 世界中解决此类问题的策略是什么?

But, I obviously can't do that in a Net Standard library; so, my question is, what is the strategy for addressing such issues in the .Net Standard world?

推荐答案

绑定重定向是 .NET 框架的概念,.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 包时,这是自动完成的.由于正确的配置因项目类型而异,请参阅公告 Issues with .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>

更多推荐

将 bindingRedirect 添加到 .Net Standard 库

本文发布于:2023-11-03 12:18:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555113.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:bindingRedirect   Net   Standard

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!