What is this Net Core everyone is talking about? Net Core is a cross-platform application framework for C#. With Visual Studio or the Net Core tool set, it’s not too hard to build server applications on any platform that can be deployed to any platform. This is remarkable considering the source code for C#/NET was proprietary code until Nov. 2014 when Net Core and parts of Net Framework were open sourced by Microsoft.
But, doesn’t Mono/Xamarin and Unity all offer a cross-platform application framework for C# as well?? While it’s true that Mono/Xamarin work cross platform, it was written from scratch. Mono was then extended to include bindings for Android, iOS, and other runtimes, and an API for UI common across selected platforms. Unity, as well, is a fork of Mono, with an API for graphics added. It too has differences with Net Framework that have emerged over time. It is expected that Net Standard, described below, will eventually supplant much of the functionality of these different cross-platform application frameworks.
Net Core has changed quite a bit in just the last few months let alone years. The file project.json, which specified the project dependencies, frameworks, targets, and runtimes, is now deprecated. Answers to questions about setting runtimes be found in Stackoverflow.com, but are confusing since they refer to the old project.json file.
First, a small side issue: WSL (Windows for Subsystem Linux)
If you are not interested in installing Visual Studio 2017 or Net Core on Windows, you can use WSL (Windows for Subsystem Linux) to easily develop a Linux from Windows 10.
After enabling developer mode for your Windows desktop, use “lxrun.exe /uninstall” and “lxrun.exe /install” to get a clean, fresh, up-to-date version of Ubuntu running on Windows. To run the shell, start up Powershell or Cmd, then type bash.
It’s also now possible to run X Windows applications in WSL with an X server installed on your Windows 10 desktop (https://sourceforge.net/projects/xming/). See Instructions1 and Instructions2. Some useful X Windows programs would be XTerm (sudo apt install xterm), gFTP (sudo apt install gftp)–just the tip of an iceberg of wonderful software.
Installing Net Core
For Ubuntu, see the instructions at https://www.microsoft.com/net/core#linuxubuntu to install Net Core in WSL.
You can install Visual Studio 2017 with the Net Core option, and develop Net Core applications from Visual Studio if you like.
Note, I’ve had problems getting the command line tool set working sometimes. This was because of multiple Net Core installations. Unfortunately, the newest Net Core package does not uninstall the older versions, at least on Windows. Beyond that, there shouldn’t be anything more to do to install. It all should find the dotnet executable, whichever platform you use.
Writing a Hello World program
To build a “Hello World” application, type the following in a WSL bash shell:
dotnet new console dotnet restore dotnet run
The first line creates a “Hello World” program. This may take a while while. The second command line performs essentially a “NuGet”, downloading and installing any additional packages. The third command line compiles and runs the program. Note, the product is a dotnet-dependent CIL/MSIL executable, and requires the “dotnet” executable to run, much like how Mono works.
To build a native OS executable for specific OS target, modify the CSPROJ file with a
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp1.1</TargetFramework> <RuntimeIdentifiers>ubuntu.16.04-x64;ubuntu.14.04-x64;win10-x64;osx.10.11-x64</RuntimeIdentifiers> </PropertyGroup> </Project>
dotnet restore/build with the
-r option to build the programs.
dotnet restore -r ubuntu-14.04-x64 dotnet build -r ubuntu-14.04-x64 dotnet restore -r ubuntu-16.04-x64 dotnet build -r ubuntu-16.04-x64 etc.
In the directory
bin/Debug/netcoreapp1.1/ubuntu.16.04-x64/ for example, you will find the executable
dn. However, the executable is dependent on the dotnet framework being installed on the deployed machine.
To build a self-containing app with the framework contained with the executable, type:
dotnet publish -r ubuntu-14.04.x64 dotnet publish -r ubuntu-16.04.x64 etc.
In the directory
bin/Debug/netcoreapp1.1/ubuntu.16.04-x64/publish you will find the executable dn with all the files needed to deploy the application.
Each time you change the target runtime list, use dotnet restore to install dependencies.
Relationship with Net Standard and other frameworks
Over the years, there have been many different implementations of the “NET Framework,” which resulted in different APIs. For example, in Mono/Xamarin/Unity, the Reflection API has several differences with the NET Framework Reflection API. Net Standard is a new base class layer that provides cross compatibility of different frameworks by providing a “shim” (or in my day, a “thunk”) that maps the functionality over the frameworks. The goal is to allow libraries written in one framework to be used in another framework.
|.NET Framework (with tooling 1.0)||4.5||4.5||4.5.1||4.6||4.6.1||4.6.2||vNext||4.6.1|
|.NET Framework (with tooling 2.0)||4.5||4.5||4.5.1||4.6||4.6.1||4.6.1||4.6.1||4.6.1|
|Universal Windows Platform||10||10||10||10||10||vNext||vNext||vNext|
|Windows Phone Silverlight||8|
Undoubtedly, you have probably seen the above figure showing the compatibility of the various NET Frameworks. But, this illustration simplifies what is actually required. To create a Net Standard library, you must compile for the appropriate Net Standard framework and provide a a list of dependencies for compatibility. See NetStandardLib and NetFrameworkApp in the example I provide.
I constructed a set of examples that check the cross compatibility of Net Core and Net Framework APIs with libraries in Net Core, Framework, and Standard. You can modify it to test out different scenarios.
Similarly, strict Net Standard 1.4 libraries can be called from Xamarin Forms libraries (Profile259), but no higher version number of Net Standard.
It is possible to compile your library to Net Framework, Xamarin, etc. while forcing your code to be Net Standard compliant by adding a <TargetFrameworks> tag to your .csproj file. For example, a <TargetFrameworks>netstandard1.4;net461</TargetFrameworks> tag will generate a Net Standard and Net Framework targets for your library. You could use a higher version number for Net Standard, but you would have to provide an additional target (
<TargetFrameworks>) in the CSPROJ file to target the PCL (e.g., http://codinggorilla.domemtech.com/?p=1521), which may lead to other problems.
Examples can be found at https://github.com/kaby76/NetStandardCoreFramworkExamples