The complete error is:
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, ...'
I try to instantiate a MongoClient which is still in a .Net 4.5 assembly, like this:
var client = new MongoDB.Driver.MongoClient(@"mongodb://localhost:27017/");
if (client == null)
{
return;
}
The build error is on the client == null
line.
My project.json is as follows:
{
"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"
}
}
}
My VM has Win10 as OS, so I have only .Net 46x installed.
I have removed the import of dnxcore50 and replaced it with the full net46 import. Am I doing something wrong?
I solved it by removing the netstandard1.6 framework and replaced it with "net46". I was under the impression that with a netstandard1.6, I could import the .Net 4.6 framework and remove the dnxcore50 import and then it should run with the full library, as mentioned here: https://github.com/dotnet/corefx/blob/master/Documentation/architecture/net-platform-standard.md#mapping-the-net-platform-standard-to-platforms
For the time being I have changed my projects to target only the full .Net framework. Once the .Net Core Mongo driver is available, I can target it as netcoreapp.
My project.json is now:
{
"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": { }
}
}
Thanks @Nick Acosta for pointing me to: A common class library consumed by both .NET Core and .Net 4.5.2
Update
I have got a response from Eric Mellino on the CoreFX Repo: 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.