类型 'Object' 在未被 .Net Core 项目引用的程序集中定义

The type 'Object' is defined in an assembly that is not referenced with .Net Core project

完整的错误是:

The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, version=4.0.0.0, ...'

我尝试实例化一个仍在 .Net 4.5 程序集中的 MongoClient,如下所示:

var client = new MongoDB.Driver.MongoClient(@"mongodb://localhost:27017/");
if (client == null)
{
    return;
}

构建错误在 client == null 行。

我的project.json如下:

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "MongoDB.Driver": "2.2.4",
    "MongoDB.Driver.Core": "2.2.4",
    "MongoDB.Bson": "2.2.4"
  },

  "frameworks": {
    "netstandard1.6": {
      "imports": "net46"
    }
  }
}

我的虚拟机 OS 是 Win10,所以我只安装了 .Net 46x。

我删除了 dnxcore50 的导入并用完整的 net46 导入替换它。我做错了什么吗?

我通过删除netstandard1.6框架并用"net46"替换它来解决它。我的印象是,使用 netstandard1.6,我可以导入 .Net 4.6 框架并删除 dnxcore50 导入,然后它应该 运行 包含完整的库,如此处所述:https://github.com/dotnet/corefx/blob/master/Documentation/architecture/net-platform-standard.md#mapping-the-net-platform-standard-to-platforms

目前我已经将我的项目更改为仅针对完整的 .Net 框架。一旦 .Net Core Mongo 驱动程序可用,我就可以将其定位为 netcoreapp。

我的 project.json 现在是:

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "MongoDB.Driver": "2.2.4",
    "MongoDB.Driver.Core": "2.2.4",
    "MongoDB.Bson": "2.2.4"
  },

  "frameworks": {
    "net46": { }
  }
}

感谢@Nick Acosta 指点我:

更新

我在 CoreFX Repo 上收到了 Eric Mellino 的回复:https://github.com/dotnet/corefx/issues/9885#issuecomment-231194545

Your first version:

"frameworks": { "netstandard1.6": { "imports": "net46" } }

is basically saying: "Build me a library targetting netstandard1.6, but also let me reference stuff built for net46, even if it isn't compatible." It turns out that the assembly isn't compatible, so you can't compile. The problem is that MongoClient references a System.Object type which resides in mscorlib.dll. NETStandard.App, when targetting netstandard1.6, is going to pull in a System.Runtime.dll which references a System.Object type which resides in System.Runtime.dll. There is no mscorlib facade which could reconcile this discrepancy, so you get compilation errors.

If you're building for .NET Framework, use your second appoach, i.e.

"frameworks": { "net46": { }, }.

If you want to build for .NET Core, you will need a version of MongoClient which is compatible with netstandard. This could then be used from a .NET Framework application.