关于 .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="..MongoUtilityAggregation*.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="..MongoUtilityAggregation*.cs" />
    <Compile Include="..MongoUtilityBasic*.cs" />
    <Compile Include="..MongoUtilityCommand*.cs" />
    <Compile Include="..MongoUtilityCore*.cs" />
    <Compile Include="..MongoUtilityEventArgs*.cs" />
    <Compile Include="..MongoUtilitySecurity*.cs" />
    <Compile Include="..MongoUtilityToolKit*.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:WorkSpaceMongoColaResourceLibinDebugResourceLib.dll
    Common -> E:WorkSpaceMongoColaCommoninDebugCommon.dll
E:WorkSpaceMongoColaMongoUtilityCoreRuntimeMongoDBContext.cs(26,20,26,49): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已过时:“Use the new API instead.” 
E:WorkSpaceMongoColaMongoUtilityCoreConnectionInfo.cs(77,27,77,45): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已过时:“Use the new API instead.” 
E:WorkSpaceMongoColaMongoUtilityCoreRuntimeMongoDBContext.cs(646,60,646,78): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已过时:“Use the new API instead.” 
E:WorkSpaceMongoColaMongoUtilityCommandDataBaseCommand.cs(112,37,112,89): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已过时:“Use the new API instead.” 
E:WorkSpaceMongoColaMongoUtilityCommandDataBaseCommand.cs(136,30,136,73): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已过时:“Use the new API instead.” 
    MongoUtilityStandard -> E:WorkSpaceMongoColaMongoUtilityStandardinDebug
etcoreapp1.0MongoUtilityStandard.dll
E:WorkSpaceMongoColaMongoGUICtlClientTreeFillDataBaseInfoToTreeNode.cs(45,17,45,35): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已过时:“Use the new API instead.” 
E:WorkSpaceMongoColaMongoGUICtlClientTreeFillDataBaseInfoToTreeNode.cs(57,17,57,35): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已过时:“Use the new API instead.” 
E:WorkSpaceMongoColaMongoGUICtlClientTreeFillDataBaseInfoToTreeNode.cs(78,17,78,35): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已过时:“Use the new API instead.” 
E:WorkSpaceMongoColaMongoGUICtlClientTreeUIHelper.cs(169,21,169,44): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已过时:“Use the new API instead.” 
E:WorkSpaceMongoColaMongoGUICtlClientTreeUIHelper.cs(373,17,373,35): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已过时:“Use the new API instead.” 
E:WorkSpaceMongoColaMongoGUICtlClientTreeUIHelper.cs(375,43,375,61): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已过时:“Use the new API instead.” 
E:WorkSpaceMongoColaMongoGUICtlClientTreeUIHelper.cs(382,25,382,43): warning CS0618: “MongoClientExtensions.GetServer(MongoClient)”已过时:“Use the new API instead.” 
    MongoGUICtl -> E:WorkSpaceMongoColaMongoGUICtlinDebugMongoGUICtl.dll
    MongoGUIView -> E:WorkSpaceMongoColaMongoGUIViewinDebugMongoGUIView.dll
    FunctionForm -> E:WorkSpaceMongoColaFunctionForminDebugFunctionForm.dll
    PlugInPrj -> E:WorkSpaceMongoColaPlugInPrjinDebugPlugInPrj.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 AssembliesMicrosoftFramework.NETFrameworkv4.6.2FacadesSystem.Runtime.dll]重新映射到版本“4.1.0.0”[],以解决冲突并消除警告。
D:Program Files (x86)Microsoft Visual Studio2017CommunityMSBuild15.0BinMicrosoft.Common.CurrentVersion.targets(1909,5): warning MSB3276: 发现同一依赖程序集的不同版本间存在冲突。请将项目文件中的“AutoGenerateBindingRedirects”属性设置为 true。有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=294190。 
    MongoCola -> E:WorkSpaceMongoColaMongoColainDebugMongoCola.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 的,死活认为这两个版本不同,我也是醉了。
[更新] 手动将 packagesSystem.Linq4.1.0lib et463 下面的包放到输出目录下面。。。。。
虽然不能选择463(我不知道哪里可以下载463),但是463下面的直接用就可以了。。。。。
或许Standard1.6 === net463吧。。。。

原文地址:https://www.cnblogs.com/TextEditor/p/6121207.html