use*_*179 5 .net c# asp.net .net-standard
我刚刚开始使用 .NET Standard,我正在尝试掌握这个新场景中的最佳实践。我已将一些关键库转换为 .NET Standard 2.0。当我创建一个新的 Web 应用程序(ASP.NET 4.6.1、4.6.2 或 4.7)项目,添加新库作为引用,然后进行构建时,我收到很多警告。如果库面向 .NET Standard 1.4 或更早版本,则不会显示这些警告,但我需要 .NET Standard 2.0。这是这些警告的一部分:
Consider app.config remapping of assembly "System.Runtime.Serialization.Xml, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" from Version "4.0.10.0" [C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\Facades\System.Runtime.Serialization.Xml.dll] to Version "4.1.3.0" [C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\\net461\ref\System.Runtime.Serialization.Xml.dll] to solve conflict and get rid of warning.
... [About 50 similar lines] ...
Consider app.config remapping of assembly "System.Runtime.InteropServices.RuntimeInformation, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" from Version "0.0.0.0" [] to Version "4.0.2.0" [C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\\net461\ref\System.Runtime.InteropServices.RuntimeInformation.dll] to solve conflict and get rid of warning.
Run Code Online (Sandbox Code Playgroud)
我收到的最后一个警告是这样的:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(1987,5): warning MSB3247: Found conflicts between different versions of the same dependent assembly. In Visual Studio, double-click this warning (or select it and press Enter) to fix the conflicts; otherwise, add the following binding redirects to the "runtime" node in the application configuration file:
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Runtime.Serialization.Xml" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" /><bindingRedirect oldVersion="0.0.0.0-4.1.3.0" newVersion="4.1.3.0" /></dependentAssembly></assemblyBinding>
... [About 50 similar lines] ...
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Runtime.InteropServices.RuntimeInformation" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" /><bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" /></dependentAssembly></assemblyBinding>
Run Code Online (Sandbox Code Playgroud)
如果我双击该警告,它会将适当的绑定重定向添加到我的 Web.config 文件中。但我想知道以下几点:
难道我做错了什么?我很难相信 .NET Framework 4.6.1 应用程序需要添加所有这些绑定重定向来引用 .NET 标准库。
为什么所有这些绑定重定向都是必要的?为什么我的 Web 应用程序不能仅使用 .NET 标准库正在使用的相同程序集而不绑定重定向?为什么我的 Web 应用程序默认使用这些程序集的旧版本?
可能与此问题相关的是,我的应用程序可以启动(有或没有绑定重定向),但当我尝试使用 SqlClient 时很快就会失败:“无法加载文件或程序集 'System.Data.SqlClient,Version=4.2.0.0,Culture=中性,PublicKeyToken=b03f5f7f11d50a3a'或其依赖项之一。系统找不到指定的文件。” 使用 System.Data.SqlClient 4.4 的库中引发此异常。我尝试添加这个绑定重定向,但没有帮助: <dependentAssembly><assemblyIdentity name="System.Data.SqlClient" publicKeyToken="B03F5F7F11D50A3A" culture="neutral"/><bindingRedirect oldVersion="0.0.0.0-4.4.0.0" newVersion="4.4.0.0"/></dependentAssembly> 我后来发现,如果我故意将System.Data.SqlClient NuGet包添加到ASP.NET项目中,并将绑定重定向添加到ASP.NET项目的Web中。 config后,SQL操作就可以执行了。(绑定重定向看起来像这样: <dependentAssembly><assemblyIdentity name="System.Data.SqlClient" publicKeyToken="B03F5F7F11D50A3A" culture="neutral"/><bindingRedirect oldVersion="0.0.0.0-4.4.0.0" newVersion="4.2.0.0"/></dependentAssembly> 即使我添加了 4.4 NuGet 包,当我构建时,4.2 也会输出到 bin 目录。我不知道为什么它会这样做,但如果它会输出 4.4,我不知道我认为绑定重定向是必要的。)
感谢 Lex Li 的评论,我被引导到这个页面: https://github.com/dotnet/standard/issues/481
我想提取一些对于回答我的子问题特别有用的部分:
Web 应用程序和网站不支持自动绑定重定向生成。为了解决绑定冲突,您需要双击错误列表中的警告,Visual Studio 会将它们添加到您的 web.config 文件中。
相信我,我的团队中没有人热衷于 net461 支持故事的展开方式。从某种意义上说,您所要求的已经实现:.NET Framework 4.7.1 将附带 .NET Standard 2.0 的完整内置支持。挑战在于(1)它尚未发货,(2)我们的许多客户将无法立即升级,因为目标环境超出了他们的控制范围。我们的选择是尽我们所能或完全支持它。是的,不幸的是,这需要克服困难,但至少应用程序开发人员可以做一些事情。如果您完全控制环境,请务必跳转到 .NET Framework 4.7.1。
SDK 样式项目(.NET Core/.NET Standard)唯一支持的模式是 PackageReference 。这意味着引用 .NET Standard 项目的 .NET Framework 项目最终会跨越两个不同 NuGet 模型之间的流。当 .NET Standard 项目引用 .NET Framework 项目未引用的 NuGet 包时,应用程序最终会丢失来自这些包的所有二进制文件。