2017-10-11 61 views
1

我剛剛開始使用.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文件。但我想知道以下內容:

  1. 我做錯了什麼?我很難相信添加所有這些綁定重定向對於.NET Framework 4.6.1應用程序來說是引用.NET標準庫的預期。

  2. 爲什麼這一切都需要綁定重定向?爲什麼我的Web應用程序不能使用.NET標準庫在不綁定重定向的情況下使用的相同程序集?爲什麼我的Web應用程序默認使用這些程序集的舊版本?

  3. 可能與此問題有關的是我的應用程序可以啓動(有或沒有綁定重定向),但在嘗試使用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

+2

GitHub https://github.com/dotnet/standard/issues/481上的熱門帖子已經討論了幕後的細節。所以對你來說,只要按照提示。 –

回答

1

由於由Lex李評論,我被帶到了這個頁面: https://github.com/dotnet/standard/issues/481

我想提取某些部分這在回答我的子問題時特別有用:

  1. 以下聲明回答了我的子問題#1,其中提到了添加大量綁定重定向的正確性。

的Web應用程序和網站不支持自動綁定 重定向產生。爲了解決綁定衝突,您需要 雙擊錯誤列表中的警告,並且Visual Studio將 添加到您的web.config文件中。

  • 下面的語句回答我的子問題#2,關於爲什麼這一切是必要的,爲什麼它似乎很未了:
  • 相信我我的團隊中沒有人熱衷於net461 支持故事展現的方式。從某種意義上說,您所要求的已經實現了: :.NET Framework 4.7.1將支持.NET標準2.0的完整內置 。我們面臨的挑戰是(1)尚未發貨 ,(2)我們的許多客戶將無法立即升級 ,因爲目標環境不在其控制範圍之內。 選擇是盡我們所能或完全支持它。 是的,這不幸的是需要跳過箍環,但至少有 應用程序開發人員可以做的事情。如果您完全控制了 環境,請務必跳轉到.NET Framework 4.7.1。

    1. 以下聲明回答了我的子問題#3,說明爲什麼找不到組件。

    的SDK式項目的唯一支持的模式(.NET核心/ .NET 標準)是PackageReference。這意味着引用.NET標準項目的.NET Framework 項目最終會跨越兩個不同NuGet模型之間的 流。當.NET標準 項目引用的NuGet包.NET Framework項目 未引用時,應用程序最終會丟失來自這些包的所有來自 的二進制文件。

    0

    ,如果您是在.NET 4.6中,您只能消耗建(最多).NET 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

    +0

    對不起。我終於離開了.1。我的應用程序是一個4.6.1應用程序。這是一個重要的細節,我很遺憾我省略了它。我已經解決了我的問題。 – user1325179

    +0

    @ user1325179您是否還安裝了NET Core 2.0 SDK? –

    +0

    是的。我用我的最新發現更新了我的問題。具體而言,如果我的庫使用.NET Standard 1.4或更早版本,則不會顯示警告。另外,如果我使用NuGet來包含System.Data.SqlClient並向我的Web.config文件添加綁定重定向,則可以執行SQL操作。 – user1325179

    相關問題