How to access mscorelib classes in DNX Core 5 apps?
I am developing DNX Core 5.0 command line app and I am getting compilation errors for mscorelib classes (Math, Environment, ..)
In VS 2015 RC, I get a tip to add Microsoft.DotNet.CoreCLR but adding it to jason file is not helping. I even tried with dnx-coreclr-x64.
My project.json file
{
"version": "1.0.0-*",
"description": "",
"authors": [ "" ],
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": "",
"dependencies": {
"System.Diagnostics.TraceSource": "4.0.0-beta",
// "Microsoft.DotNet.CoreCLR": "1.0.2-prerelease",
// "Microsoft.DotNet.CoreCLR.Development": "1.0.2-prerelease",
"System.Threading.Thread": "4.0.0-beta",
"dnx-coreclr-win-x64": "1.0.0-beta4",
"Microsoft.DotNet.CoreCLR": "1.0.2-prerelease"
},
"commands": {
"ConsoleApp1": "ConsoleApp1"
},
"frameworks": {
// "dnx451": { },
"dnxcore50": {
"dependencies": {
"System.Console": "4.0.0-beta-22816",
"System.Collections": "4.0.10-beta-22816",
"System.Linq": "4.0.0-beta-22816",
"System.Threading": "4.0.10-beta-22816",
"Microsoft.CSharp": "4.0.0-beta-22816",
"System.Diagnostics.Debug": "4.0.10-beta",
"System.Xml.XmlDocument": "4.0.0-beta",
"System.Xml.XmlSerializer": "4.0.0-beta",
"System.Xml.ReaderWriter": "4.0.10-beta",
"System.Runtime.InteropServices": "4.0.20-beta",
"System.Collections.NonGeneric": "4.0.0-beta",
"System.IO": "4.0.10-beta"
}
}
}
}
How to access mscorelib classes in DNX Core 5 apps?
You don't. You never compile against it. You only use the System.* packages. mscorlib is an implementation detail that you're never supposed to use directly in this world.
In VS 2015 RC, I get a tip to add Microsoft.DotNet.CoreCLR but adding it to jason file is not helping. I even tried with dnx-coreclr-x64.
Those tops are guiding you down the wrong path (we'll file bugs for that). Also, adding a reference to dnx-* is always wrong so don't do it :smile:.
The project.json file looks pretty incorrect. I'd start over with it being empty. What exactly are you trying to do?
{
"dependencies": {
"System.Runtime": "4.0.20-beta-22816"
},
"frameworks": {
"dnxcore50": { }
}
}
Above is the bare minimum you need to "compile" a class library. If you're building a console application let me know. Also, if you're following some set of instructions, let me know which ones.
Thanks.,
What exactly are you trying to do?
I am trying to build my old console app for coreclr 5.
I started with your project.json and I had to add "System.Console": "4.0.0-beta-22816" for Console.Writeline("Hello").
But I still can not access Math.Round function. I get the same tool tip to add "Microsoft.DotNet.CoreCLR"
Great! Assuming you're using visual studio (otherwise you wouldn't get any package suggestions). I'm actually quite surprised it suggested that for System.Math, can you show me a screenshot of that?
System.Math is in "System.Runtime.Extensions": "4.0.10-beta-22816", the version number is what I got because I'm using the ones published to nuget.org, not the corefx myget feed.

On a side note, I am also using the published one. In fact, I don't know how to use my corefx components. Appreciate if you could point me to some document on how to use local corefx components.
@ericstj Does that CoreCLR package contain mscorlib? That's likely why it's suggesting it...
The Microsoft.DotNet.* packages are a nuget v2 packages that are not meant for external consumption. They should go away in the future once we update the corefx build to stop using (and notify everyone downstream who happened to pick them up). The Microsoft.NetCore.Runtime.* packages are the replacement. They use a placeholder in ref to hide from compile and only contribute mscorlib.dll and other internal runtime assemblies as only runtime asset.
@ericstj how to add Microsoft.NetCore.Runtime.* package to my project? Looks like it's not on nuget yet.
Thanks
@davidfowl @ericstj
I am still stuck. I still can not build console app for dnxcore50. I've tried both VS RC15 and the latest DNX.
I found the same question on stackexchange.
I am wondering if I am missing some settings or this will be fixed in the next VS build.
What is the current problem you are hitting?
You don't actually need to access the NetCore package, nor mscorlib. The discussion around this was describing why the package suggestion was appearing.
As @davidfowl mentions you need to use System.Runtime.Extensions for the Math class.
@ericstj For the Console Application (package) project (code below), I am getting the following error message. I did try with System.Runtime.Extensions" and System.Runtime". And I still can not access Math in DNX Core 5.0.
Error CS0012 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, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'. ConsoleApp2.DNX Core 5.0 C:\temp\ConsoleApp2\Program.cs 12
project.json
{
"version": "1.0.0-*",
"description": "",
"authors": [ "" ],
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": "",
"dependencies": {
"System.Runtime.Extensions": "4.0.10-beta-22819",
"System.Console": "4.0.0-beta-22816",
"System.Collections": "4.0.10-beta-22816",
"System.Linq": "4.0.0-beta-22816",
"System.Threading": "4.0.10-beta-22816",
"Microsoft.CSharp": "4.0.0-beta-22816",
"System.Diagnostics.Debug": "4.0.10-beta-22819"
},
"commands": {
"ConsoleApp2" : "ConsoleApp2"
},
"frameworks" : {
"dnx451": { },
"dnxcore50" : {}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ConsoleApp2
{
public class Program
{
public void Main(string[] args)
{
System.Diagnostics.Debug.WriteLine("test");
}
}
}
So that error indicates that restore failed for some reason, since you aren't getting any reference assemblies. The source of the failure might be in the package manager output window or error list. Regardless this issue does not belong in the CoreCLR repo so I am closing it out.
If you are still having issues with restore please open an issue in http://github.com/aspnet/dnx.
We're receiving an error like this:
System.TypeLoadException: Could not load type 'System.ICloneable' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
We've tried adding "System" : "4.0.0.0" to our dependencies with no luck.
You can use .NET Framework dlls on .NET Core. You have to recompile
Could my issue be about the same?
I have this error:
The type 'DateTime' is defined in an assembly that is not referenced. You must add a assembly 'mscorlib.dll Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
What am I trying to do?
project.json
{
"version": "1.0.0-*",
"description": "WMI.ECM.PCMS.Services.Acomba Class Library",
"authors": [ "WILL" ],
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": "",
"dependencies": {
"Acomba.SDK": "2.0.0",
"WMI.ECM.Inventory.Models": "",
"WMI.ECM.Inventory.Services.Contracts": ""
},
"frameworks": {
"dnx451": { },
"dnxcore50": {
"dependencies": {
"Microsoft.CSharp": "4.0.1-beta-23516",
"System.Collections": "4.0.11-beta-23516",
"System.Linq": "4.0.1-beta-23516",
"System.Runtime": "4.0.21-beta-23516",
"System.Threading": "4.0.11-beta-23516"
}
}
}
}
For more details about my problem, I asked this on StackOverflow right here:
The type 'DateTime' is defined in an assembly that is not referenced?
.NET Core doesn't support COM interop completely so may run into trouble making this work. /cc @yizhang82
If you just wanted to get past the compile step to see if anything else will fail you could add a reference to Microsoft.NETCore.Portable.Compatibility and cross your fingers (this package maps the old mscorlib-based assemblies to System.Runtime assemblies). You may hit missing types if the interop assembly depended on something that wasn't available, but we have most primitves so I'd expect you to get further. Assuming you don't hit that, you may fail on Windows due to gaps in COM interop on CoreCLR. Much more likely on unix if you are using anything other than value types in that interop assembly since you won't have the backing implementation for any COM classes.
Isn't that the kind of assembly @davidfowl suggested to avoid on 2015.05.12?
Anyway, this works flawlessly!
Is it supposed to be a solution that will live long, or is it only a temporary workaround?
Most helpful comment
.NET Core doesn't support COM interop completely so may run into trouble making this work. /cc @yizhang82
If you just wanted to get past the compile step to see if anything else will fail you could add a reference to Microsoft.NETCore.Portable.Compatibility and cross your fingers (this package maps the old mscorlib-based assemblies to System.Runtime assemblies). You may hit missing types if the interop assembly depended on something that wasn't available, but we have most primitves so I'd expect you to get further. Assuming you don't hit that, you may fail on Windows due to gaps in COM interop on CoreCLR. Much more likely on unix if you are using anything other than value types in that interop assembly since you won't have the backing implementation for any COM classes.