繁体   English   中英

使用.NET标准库的ASP.NET Web应用程序

[英]ASP.NET Web Application Using .NET Standard Library

我刚刚开始使用.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.

我得到的最后一个警告是:

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>

如果我双击该警告,它会将适当的绑定重定向添加到我的Web.config文件中。 但我想知道以下内容:

  1. 难道我做错了什么? 我很难相信,.NET Framework 4.6.1应用程序需要添加所有这些绑定重定向来引用.NET标准库。

  2. 为什么所有这些绑定重定向都是必需的? 为什么我的Web应用程序不能使用.NET标准库使用的相同程序集而不绑定重定向? 为什么我的Web应用程序默认使用这些程序集的旧版本?

  3. 可能与此问题相关的是我的应用程序可以启动(有或没有绑定重定向)但在我尝试使用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

我想提取一些在回答我的子问题时特别有用的部分:

  1. 以下声明回答了我的子问题#1,关于添加大量绑定重定向的正确性。

Web应用程序和Web站点不支持自动绑定重定向生成。 要解决绑定冲突,您需要双击错误列表中的警告,Visual Studio会将它们添加到您的web.config文件中。

  1. 以下陈述回答了我的子问题#2,关于为什么所有这些都是必要的以及为什么它看起来如此未完成:

相信我,我的团队中没有人热衷于net461支持故事展开的方式。 从某种意义上说,您所要求的内容已经实现:.NET Framework 4.7.1将提供对.NET Standard 2.0的完全内置支持。 挑战在于:(1)尚未发货;(2)由于目标环境不在他们的控制之下,我们的许多客户将无法立即升级。 选择是尽我们所能或者完全支持它。 是的,不幸的是,这需要跳过篮球,但至少应用程序开发人员可以做些什么。 如果您完全控制环境,请跳转到.NET Framework 4.7.1。

  1. 以下陈述回答了我的子问题#3,关于无法找到装配的原因。

SDK样式项目(.NET Core / .NET Standard)唯一支持的模式是PackageReference。 这意味着引用.NET Standard项目的.NET Framework项目最终会跨越两个不同NuGet模型之间的流。 当.NET Standard项目引用.NET Framework项目未引用的NuGet包时,应用程序最终会丢失来自这些包的所有二进制文件。

基于此受支持平台表 ,如果您使用的是.NET 4.6,则只能使用基于(最高).NET Standard 1.3构建的库。 除非您升级到.NET 4.6.1(与.NET Core 2.0 SDK一起使用)或更高版本,否则不能使用基于.NET Standard 2.0构建的库。

以上还链接到以下内容,它提供了更多信息: https//docs.microsoft.com/en-gb/dotnet/standard/net-standard#net-platforms-support

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM