簡體   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