我剛剛開始使用.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。這是這些警告的一部分:使用.NET標準庫的ASP.NET Web應用程序
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文件。但我想知道以下內容:
我做錯了什麼?我很難相信添加所有這些綁定重定向對於.NET Framework 4.6.1應用程序來說是引用.NET標準庫的預期。
爲什麼這一切都需要綁定重定向?爲什麼我的Web應用程序不能使用.NET標準庫在不綁定重定向的情況下使用的相同程序集?爲什麼我的Web應用程序默認使用這些程序集的舊版本?
可能與此問題有關的是我的應用程序可以啓動(有或沒有綁定重定向),但在嘗試使用SqlClient時很快失敗:「無法加載文件或程序集」System.Data.SqlClient,Version = 4.2 .0.0,Culture = neutral,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,我不會「T認爲綁定重定向是必要的。)基於this table of supported platforms
GitHub https://github.com/dotnet/standard/issues/481上的熱門帖子已經討論了幕後的細節。所以對你來說,只要按照提示。 –