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

标签 c# asp.net .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.

我收到的最后一个警告是这样的:

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>

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

  1. 我做错了什么吗?我很难相信为 .NET Framework 4.6.1 应用程序添加所有这些绑定(bind)重定向是为了引用 .NET Standard 库。

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

  3. 可能与此问题相关的是我的应用程序可以启动(使用或不使用绑定(bind)重定向)但当我尝试使用 SqlClient 时很快失败:“无法加载文件或程序集‘System.Data.SqlClient,Version= 4.2.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a' 或其依赖项之一。系统找不到指定的文件。”此异常是在使用 System.Data.SqlClient 4.4 的库中引发的。我试图添加此绑定(bind)重定向,但没有帮助:<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>后来发现,如果我特意在ASP.NET项目中添加System.Data.SqlClient NuGet包,并在ASP.NET项目的Web.config中添加绑定(bind)重定向,SQL操作就可以执行了。 (绑定(bind)重定向看起来是这样的:<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,我不认为绑定(bind)重定向是必要的。)

最佳答案

感谢 Lex Li 的评论,我被带到了这个页面: https://github.com/dotnet/standard/issues/481

我想摘录一些对回答我的子问题特别有用的部分:

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

Web applications and web sites don't support automatic binding redirect generation. In order to resolve binding conflicts, you need to double click the warning in the error list and Visual Studio will add them to your web.config file.

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

Believe me, no one on my team is keen on on the way the net461 support story has unfolded. In a sense, what you're asking has already been implemented: .NET Framework 4.7.1 will ship with full built-in support of .NET Standard 2.0. The challenge is that (1) it hasn't been shipped yet and (2) many of our customers will not be able to upgrade immediately because the target environment is outside their control. The option was to support it the best we can or can it altogether. Yes, this unfortunately requires jumping through hoops but at least there is something app developers can do. If you fully control the environment, by all means, jump to .NET Framework 4.7.1.

  1. 以下陈述回答了我的子问题 #3,即为什么找不到程序集。

The only supported mode for SDK-style projects (.NET Core/.NET Standard) is PackageReference . This means that a .NET Framework project referencing a .NET Standard project ends up crossing the streams between two different NuGet models. When the .NET Standard project references NuGet packages that the .NET Framework project doesn't reference, the application ends up missing all binaries coming from those packages.

关于c# - 使用 .NET 标准库的 ASP.NET Web 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46693548/

相关文章:

c# - Web 请求/上传在最后失败

c# - 如何找到相关讲座?

css - 将按钮的可见属性分配给静态方法结果

c# - MVC4 中使用 SignalR 的提醒模型

c# - 如何检查是基类对象还是派生对象?

asp.net - 如何在两个网站中访问同一个Session?

c# - MVC 配置种子方法未插入指定的主键

javascript - 从 JavaScript 访问 C# 列表

asp.net - 为什么页面处理的 AspNetSessionData 阶段会延迟我的页面 20 秒以上?

c# - Web 应用程序中基于文化的 ToString() 方法