简体   繁体   English

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

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

I'm just getting started with .NET Standard, and I'm trying to grasp best practices in this new scenario. 我刚刚开始使用.NET Standard,我正在努力掌握这种新方案中的最佳实践。 I've transitioned a few of my key libraries to .NET Standard 2.0. 我已经将一些我的密钥库转换为.NET Standard 2.0。 When I make a new Web application (ASP.NET 4.6.1, 4.6.2, or 4.7) project, add my new libraries as references, and then build, I get lots of warnings. 当我创建一个新的Web应用程序(ASP.NET 4.6.1,4.6.2或4.7)项目时,添加我的新库作为引用,然后构建,我收到很多警告。 None of these warnings are shown if the library targets .NET Standard 1.4 or earlier, but I need .NET Standard 2.0. 如果库以.NET Standard 1.4或更早版本为目标,则不会显示这些警告,但我需要.NET Standard 2.0。 This is a portion of those warnings: 这是这些警告的一部分:

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.

The very last warning I get is this: 我得到的最后一个警告是:

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>

If I double-click that warning, it adds the appropriate binding redirects to my Web.config file. 如果我双击该警告,它会将适当的绑定重定向添加到我的Web.config文件中。 But I'm wondering the following: 但我想知道以下内容:

  1. Am I doing something wrong? 难道我做错了什么? It's hard for me to believe that adding all these binding redirects is expected for a .NET Framework 4.6.1 application to reference a .NET Standard library. 我很难相信,.NET Framework 4.6.1应用程序需要添加所有这些绑定重定向来引用.NET标准库。

  2. Why is this all this binding redirection necessary? 为什么所有这些绑定重定向都是必需的? Why can't my Web application just use the same assemblies the .NET Standard library is using without binding redirects? 为什么我的Web应用程序不能使用.NET标准库使用的相同程序集而不绑定重定向? Why does my Web application default to using old versions of these assemblies? 为什么我的Web应用程序默认使用这些程序集的旧版本?

  3. Possibly related to this issue is that my application can start (with or without binding redirects) but quickly fails when I try to use SqlClient: "Could not load file or assembly 'System.Data.SqlClient, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified." 可能与此问题相关的是我的应用程序可以启动(有或没有绑定重定向)但在我尝试使用SqlClient时很快失败:“无法加载文件或程序集'System.Data.SqlClient,Version = 4.2.0.0,Culture =中性,PublicKeyToken = b03f5f7f11d50a3a'或其中一个依赖项。系统找不到指定的文件。“ This exception is thrown within a library that is using System.Data.SqlClient 4.4. 在使用System.Data.SqlClient 4.4的库中抛出此异常。 I tried to add this binding redirect, but it hasn't helped: <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> I later discovered that if I intentionally add the System.Data.SqlClient NuGet package to the ASP.NET project and add a binding redirect to the ASP.NET project's Web.config, the SQL operation can execute. 我试图添加这个绑定重定向,但它没有帮助: <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操作可以执行。 (The binding redirect looks like this: <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> Even though I added the 4.4 NuGet package, when I build, 4.2 is output to the bin directory. I don't know why it would do that, but if it would output 4.4, I don't think the binding redirect would be necessary.) (绑定重定向如下所示: <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,我不要认为绑定重定向是必要的。)

Thanks to a comment by Lex Li, I was led to this page: https://github.com/dotnet/standard/issues/481 感谢Lex Li的评论,我被引导到了这个页面: https//github.com/dotnet/standard/issues/481

I want to extract some portions that were especially useful in answering my sub-questions: 我想提取一些在回答我的子问题时特别有用的部分:

  1. The following statement answered my sub-question #1, regarding the correctness of adding tons of binding redirects. 以下声明回答了我的子问题#1,关于添加大量绑定重定向的正确性。

Web applications and web sites don't support automatic binding redirect generation. Web应用程序和Web站点不支持自动绑定重定向生成。 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. 要解决绑定冲突,您需要双击错误列表中的警告,Visual Studio会将它们添加到您的web.config文件中。

  1. The following statement answered my sub-question #2, regarding why all of this was necessary and why it seems so unfinished: 以下陈述回答了我的子问题#2,关于为什么所有这些都是必要的以及为什么它看起来如此未完成:

Believe me, no one on my team is keen on on the way the net461 support story has unfolded. 相信我,我的团队中没有人热衷于net461支持故事展开的方式。 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. 从某种意义上说,您所要求的内容已经实现:.NET Framework 4.7.1将提供对.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. 挑战在于:(1)尚未发货;(2)由于目标环境不在他们的控制之下,我们的许多客户将无法立即升级。 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. 如果您完全控制环境,请跳转到.NET Framework 4.7.1。

  1. The following statement answered my sub-question #3, regarding why assemblies couldn't be found. 以下陈述回答了我的子问题#3,关于无法找到装配的原因。

The only supported mode for SDK-style projects (.NET Core/.NET Standard) is PackageReference . SDK样式项目(.NET Core / .NET Standard)唯一支持的模式是PackageReference。 This means that a .NET Framework project referencing a .NET Standard project ends up crossing the streams between two different NuGet models. 这意味着引用.NET Standard项目的.NET Framework项目最终会跨越两个不同NuGet模型之间的流。 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. 当.NET Standard项目引用.NET Framework项目未引用的NuGet包时,应用程序最终会丢失来自这些包的所有二进制文件。

Based on this table of supported platforms , if you are on .NET 4.6, you can only consume libraries built on (up to) .NET Standard 1.3. 基于此受支持平台表 ,如果您使用的是.NET 4.6,则只能使用基于(最高).NET Standard 1.3构建的库。 You cannot use libraries built on .NET Standard 2.0 unless you move up to .NET 4.6.1 (coupled with .NET Core 2.0 SDK) or higher. 除非您升级到.NET 4.6.1(与.NET Core 2.0 SDK一起使用)或更高版本,否则不能使用基于.NET Standard 2.0构建的库。

The above also links to the following, which provides more info: https://docs.microsoft.com/en-gb/dotnet/standard/net-standard#net-platforms-support 以上还链接到以下内容,它提供了更多信息: 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