关于 .NET Core 动态链接库的开发

上个月月底,VS2017RC版发布了,一个很大的特点就是将原来的xProj文件又改回了csproj了。
这样一改,其实很多新的问题也暴露出来了,最严重的问题就是Net版本兼容性。

原来的Net体系大致是NetFramework,Net Core这样的,虽然也有Net Standard 这样的概念,但是很少有人会去关注。
但是,现在的VS将这三种体系都结合在一起了,传统的Winform还是NetFramework体系,新的AspNet使用的是NetCore体系,但是动态连接库使用的是NetStandard体系。
这三个体系是可以相互转化的,通过试验证明,在运行的层面,没有什么问题,但是由于MSBuild还没有跟上,所以VS里面报错是密密麻麻,不忍直视。

新建的解决方案,一个是NetFrame的Winform,一个是Standard的动态链接库。

75847-20161201111912599-49913178.png

下图中就可以看到,动态链接库是可以引入的(作为解决方案中的项目),但是存在警告。

75847-20161201112111709-1937972536.png

同时可以看到最大的问题是VS里面,都是错误警告:
75847-20161201112233193-1191803503.png

在原来的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产生的。
二义性问题,我看了一下定义,也发现两个一模一样的地方,按照道理来说,应该只有一处才对。不知道谁知道理由吗。

75847-20161201135502224-1771687555.png

[更新]关于二义性的问题:原来MongoUtilityStandard的工程,为了避免双重管理, 引用的是普通MongoUtility工程的代码,而不是其目录上的代码。

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

现在如果将代码直接复制一份,则该问题消失。

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

该项目在AspNet中发生错误:
netcoreapp1.0 和 netframework462,standard1.6 之间兼容性不知道是否有文档

75847-20161201140637849-869952442.png

[更新]为了兼容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吧。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值