關於 .NET Core 動態鏈接庫的開發


上個月月底,VS2017RC版發布了,一個很大的特點就是將原來的xProj文件又改回了csproj了。
這樣一改,其實很多新的問題也暴露出來了,最嚴重的問題就是Net版本兼容性。

原來的Net體系大致是NetFramework,Net Core這樣的,雖然也有Net Standard 這樣的概念,但是很少有人會去關注。
但是,現在的VS將這三種體系都結合在一起了,傳統的Winform還是NetFramework體系,新的AspNet使用的是NetCore體系,但是動態連接庫使用的是NetStandard體系。
這三個體系是可以相互轉化的,通過試驗證明,在運行的層面,沒有什么問題,但是由於MSBuild還沒有跟上,所以VS里面報錯是密密麻麻,不忍直視。

新建的解決方案,一個是NetFrame的Winform,一個是Standard的動態鏈接庫。

下圖中就可以看到,動態鏈接庫是可以引入的(作為解決方案中的項目),但是存在警告。

同時可以看到最大的問題是VS里面,都是錯誤警告:

在原來的VS2015,使用project.json的時候,在上圖的左上角是可以選擇 NetFramework462,NetCore的,現在是無法選擇的。
暫時不知道VS2017的后續版本這么處理這個問題。

個人覺得這次NetCore的發展速度很快,但是思路卻有些混亂了,現在主要的問題是:
1.原本的庫,沒有辦法完整的移植到跨平台的環境,除了UI的庫之外,很多涉及到平台特性的庫,都是缺失的。
2.現在微軟的方向,即考慮到要通過NetStandard實現來作為標准,又要兼容之前的NetCore的命名方式。估計近期有會出現一股命名潮。
3.VS工具不成熟的前提下,硬推Mac版的VS,其實Mac版的VS沒有什么亮點,雞肋。還不如全力完善Win版的VS。

[更新]
如果編輯了csproj文件

  <PropertyGroup>
    <TargetFramework>netstandard1.6</TargetFramework>
    <TargetFramework>net462</TargetFramework>
  </PropertyGroup>

則發現,項目無法編譯成功(單個TargetFramework可以編譯成功)

    <PackageReference Include="System.Xml.XmlSerializer">
      <Version>4.3.0</Version>
    </PackageReference>

這個包,在兩個Framework的時候無法使用。不知道怎么修改。

在過去project.json的時候如下

{
  "version": "1.0.0-*",

  "dependencies": {
    "mongocsharpdriver": "2.3.0-rc1",
    "MongoDB.Driver": "2.3.0-rc1"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": "netcoreapp1.0",
      "dependencies": {
        "System.Xml.XmlSerializer": "4.0.11" 
      }
    },
    "net462": {
      "frameworkAssemblies": {
        "System.Xml": "4.0.0.0",
        "System.Xml.XmlSerializer": "4.0.10"
      }
    }
  }
}

以下問題不知道是不是因為兩個Framework產生的。
二義性問題,我看了一下定義,也發現兩個一模一樣的地方,按照道理來說,應該只有一處才對。不知道誰知道理由嗎。

[更新]關於二義性的問題:原來MongoUtilityStandard的工程,為了避免雙重管理, 引用的是普通MongoUtility工程的代碼,而不是其目錄上的代碼。

<Compile Include="..\MongoUtility\Aggregation\*.cs" />

現在如果將代碼直接復制一份,則該問題消失。

 <Compile Include="Aggregation\*.cs" />

該項目在AspNet中發生錯誤:
netcoreapp1.0 和 netframework462,standard1.6 之間兼容性不知道是否有文檔

[更新]為了兼容netcoreapp1.0,繼續增加條件

<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
  <PropertyGroup Label="Configuration">
    <RootNamespace>MongoUtility</RootNamespace>
  </PropertyGroup>
  <PropertyGroup>
    <TargetFramework>netstandard1.6</TargetFramework>
    <TargetFramework>net462</TargetFramework>
    <TargetFramework>netcoreapp1.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="..\MongoUtility\Aggregation\*.cs" />
    <Compile Include="..\MongoUtility\Basic\*.cs" />
    <Compile Include="..\MongoUtility\Command\*.cs" />
    <Compile Include="..\MongoUtility\Core\*.cs" />
    <Compile Include="..\MongoUtility\EventArgs\*.cs" />
    <Compile Include="..\MongoUtility\Security\*.cs" />
    <Compile Include="..\MongoUtility\ToolKit\*.cs" />
    <EmbeddedResource Include="**\*.resx" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="mongocsharpdriver">
      <Version>2.4.0-beta1</Version>
    </PackageReference>
    <PackageReference Include="MongoDB.Bson">
      <Version>2.4.0-beta1</Version>
    </PackageReference>
    <PackageReference Include="MongoDB.Driver">
      <Version>2.4.0-beta1</Version>
    </PackageReference>
    <PackageReference Include="MongoDB.Driver.Core">
      <Version>2.4.0-beta1</Version>
    </PackageReference>
    <PackageReference Include="NETStandard.Library">
      <Version>1.6.1</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.NET.Sdk">
      <Version>1.0.0-alpha-20161104-2</Version>
      <PrivateAssets>All</PrivateAssets>
    </PackageReference>
  </ItemGroup>
  <ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.6' ">
    <PackageReference Include="Microsoft.CSharp">
      <Version>4.3.0</Version>
    </PackageReference>
  </ItemGroup>
  <ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
    <Reference Include="System.Xml" />
    <Reference Include="System" />
    <Reference Include="Microsoft.CSharp" />
  </ItemGroup>
  <ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">
    <PackageReference Include="System.Xml.XmlSerializer">
      <Version>4.3.0</Version>
    </PackageReference>
    <PackageReference Include="System.Runtime.Serialization.Formatters">
      <Version>4.3.0</Version>
    </PackageReference>
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

這樣的代碼構成和版本問題,我不知道是我學得不夠深入,還是NetCore還處於未完成狀態。
既然從project.json換回csproj,那么圖形界面也應該准備好,還有就是csproj的兼容性,MSBuild的兼容性。不知道2017正式版能否改掉這些問題。

版本的大坑:

    ResourceLib -> E:\WorkSpace\MongoCola\ResourceLib\bin\Debug\ResourceLib.dll
    Common -> E:\WorkSpace\MongoCola\Common\bin\Debug\Common.dll
E:\WorkSpace\MongoCola\MongoUtility\Core\RuntimeMongoDBContext.cs(26,20,26,49): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已過時:“Use the new API instead.” 
E:\WorkSpace\MongoCola\MongoUtility\Core\ConnectionInfo.cs(77,27,77,45): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已過時:“Use the new API instead.” 
E:\WorkSpace\MongoCola\MongoUtility\Core\RuntimeMongoDBContext.cs(646,60,646,78): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已過時:“Use the new API instead.” 
E:\WorkSpace\MongoCola\MongoUtility\Command\DataBaseCommand.cs(112,37,112,89): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已過時:“Use the new API instead.” 
E:\WorkSpace\MongoCola\MongoUtility\Command\DataBaseCommand.cs(136,30,136,73): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已過時:“Use the new API instead.” 
    MongoUtilityStandard -> E:\WorkSpace\MongoCola\MongoUtilityStandard\bin\Debug\netcoreapp1.0\MongoUtilityStandard.dll
E:\WorkSpace\MongoCola\MongoGUICtl\ClientTree\FillDataBaseInfoToTreeNode.cs(45,17,45,35): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已過時:“Use the new API instead.” 
E:\WorkSpace\MongoCola\MongoGUICtl\ClientTree\FillDataBaseInfoToTreeNode.cs(57,17,57,35): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已過時:“Use the new API instead.” 
E:\WorkSpace\MongoCola\MongoGUICtl\ClientTree\FillDataBaseInfoToTreeNode.cs(78,17,78,35): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已過時:“Use the new API instead.” 
E:\WorkSpace\MongoCola\MongoGUICtl\ClientTree\UIHelper.cs(169,21,169,44): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已過時:“Use the new API instead.” 
E:\WorkSpace\MongoCola\MongoGUICtl\ClientTree\UIHelper.cs(373,17,373,35): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已過時:“Use the new API instead.” 
E:\WorkSpace\MongoCola\MongoGUICtl\ClientTree\UIHelper.cs(375,43,375,61): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已過時:“Use the new API instead.” 
E:\WorkSpace\MongoCola\MongoGUICtl\ClientTree\UIHelper.cs(382,25,382,43): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已過時:“Use the new API instead.” 
    MongoGUICtl -> E:\WorkSpace\MongoCola\MongoGUICtl\bin\Debug\MongoGUICtl.dll
    MongoGUIView -> E:\WorkSpace\MongoCola\MongoGUIView\bin\Debug\MongoGUIView.dll
    FunctionForm -> E:\WorkSpace\MongoCola\FunctionForm\bin\Debug\FunctionForm.dll
    PlugInPrj -> E:\WorkSpace\MongoCola\PlugInPrj\bin\Debug\PlugInPrj.dll
    無法解決“System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”與“System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”之間的沖突。正在隨意選擇“System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”。
    請考慮使用 app.config 將程序集“System.Runtime, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”從版本“4.0.20.0”[C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.2\Facades\System.Runtime.dll]重新映射到版本“4.1.0.0”[],以解決沖突並消除警告。
D:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(1909,5): warning MSB3276: 發現同一依賴程序集的不同版本間存在沖突。請將項目文件中的“AutoGenerateBindingRedirects”屬性設置為 true。有關詳細信息,請參閱 http://go.microsoft.com/fwlink/?LinkId=294190。 
    MongoCola -> E:\WorkSpace\MongoCola\MongoCola\bin\Debug\MongoCola.exe
========== 全部重新生成: 成功 8 個,失敗 0 個,跳過 0 個 ==========


[更新]System.Runtime直接在Nuget包中選擇版本 4.1.0.0,保證輸出目錄包含4.1.0.0的動態鏈接庫

新問題:

System.Linq,一定要 4.1.0.0版本的,但是Nuget上只有 4.1.0 的,死活認為這兩個版本不同,我也是醉了。
[更新] 手動將 packages\System.Linq\4.1.0\lib\net463 下面的包放到輸出目錄下面。。。。。
雖然不能選擇463(我不知道哪里可以下載463),但是463下面的直接用就可以了。。。。。
或許Standard1.6 === net463吧。。。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM