Introduction
The discussion about bringing the .NET Framework to platforms other than Windows was there since the beginning (after all the great rival is everywhere). About two years talking turned to action and Microsoft started to shape what today we call .NET Core - a "cross-platform, open source, and modular .NET platform for creating modern web apps, microservices, libraries and console applications (.NET Blog - 27, June 2016)".
I'm a big fan of open source and also of .NET so I played with Mono from the beginning and with Xamarin later on, and eventually got excited with the news about .NET Core.
Here, I want to show the very first step into the world of cross-platform with .NET using .NET Core.
Background
It is very important to understand that .NET Core is not a remake of .NET Framework, but a new - parallel - framework, implemented along the same guidelines than the original framework. It re-implements all that can be done cross-platform from the .NET Framework. This re-implementation follows the .NET Standard Library (Standards ECMA-335) and therefore .NET Core is compatible to .NET Framework.
What is not implemented from the original Framework is the UI related libraries (UI is highly platform/device dependent) so the only existing form of UI with .NET Core is console. However, that exactly makes it perfectly good for libraries and services that can be the backbone of any UI - desktop or web - regardless of the device.
With all this praise of .NET Core, it is important to understand that it is a new beginning and not anything perfect and smooth already as with the more mature .NET framework, but IMHO it can be a huge opening for those who want to open up new targets to their .NET applications...
Setting Up the Environment
To see the miracle happening, I created two virtual machines - one with Windows 7 and the other with Fedora 23.
To see which Linux versions are supported, and what the current status, you can check the 'Build Status' table here: .NET Core Runtime.
At this point, do not try to be smart and use a newer distro - it probably will not work (.NET Core has strict dependencies and refuses to work even with newer versions of some of the libraries).
Windows
To install .NET Core, follow one of these links:
These are common installers, that will put .NET Core and a supporting SDK on your machine...
To check the installation, open a command window and enter dotnet --version
in it... The answer should be something like 1.0.0-preview2-003121...
Linux
Installing on Linux can be a bit more complicated (depending on the distro), so Microsoft put together a series of pages to help you, here: .NET Core Install.
Pick your distro and follow the instructions in the first section only for now...
To be sure, you have to have a certain level of understanding, how Linux terminal/install/upgrade works, but only a certain...
To check the installation, you have to run the same command - dotnet --version
- here too (terminal). The answer should be the same too.
If you have an error at this point, you are a bit on your own to resolve it for now, but I can offer is this command (terminal) to help find some of the possible problems:
find /opt/dotnet -name '*.so' -type f -print | xargs ldd | grep 'not found'
This will list all the missing libraries, and you can run an install/update command fits your distro (the /opt/dotnet path, is where you installed the .NET Core package - if it differs, change it accordingly).
Bonus - An Editor You May Use
If you are on Windows, I cannot offer you better than Visual Studio (probably Community 2015). Also if you are a hardcore Linux developer, it will be hard to move you from your favorite editor (vim, emacs, gedit and so), but for the cases between the two, I would like to introduce you to Visual Studio Code (Visual Studio Code)...
VS Code is a browser based (embedded) code editor, enclosed within a wrapper capable of handling projects and extensions, have a build in debugger platform, Git support and intellisense.
VS Code actually the first step toward a cross-platform Visual Studio (according to the roadmap, it will support standard Visual Studio projects instead of the current folder based workspaces with json project files with the next release - expected Q4 2016/Q1 2017). It is lightweight and simple, yet powerful in debugging on all supported platforms...
As I will show you C# code... VS Code installs only support for node.js, JavaScript and TypeScript by default, so you will have to install the C# extension for this sample... Use the documentation to see how...
As a very last bit, just to make you dribble... As with Visual Studio, you too can write extensions for VS Code!
Now give it a try...
The True Magic
This part is actually very simple - as I do not mean to rewrite some 3D graphics engine with .NET Core. What I will show you is these:
- Create a simple console (
Hello World
) application - Add the well known
Process
class from .NET to print loaded modules - Compile, run - and occasionally debug - it on the Fedora box
- Move the compiled assembly to the Window box and run it - AS IS
Create the Application
At this point, you can easily create a project (defined by a Json file) using the command line. As mentioned, in the future, the well known Visual Studio project format will be supported and more options for creating will be added (there is the manual option too, but...).
To create a simple 'Hello World
' application, follow these steps:
Upgrade the Code
using System;
using System.Diagnostics;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
Process oProcess = Process.GetCurrentProcess( );
Console.WriteLine( string.Format( "Process: {0} ({1})",
oProcess.ProcessName, oProcess.Id ) );
Console.WriteLine( );
Console.WriteLine( "MODULES:" );
Console.WriteLine( "________" );
foreach ( ProcessModule oModule in oProcess.Modules )
{
Console.WriteLine( string.Format( "\t{0} ({1})",
oModule.ModuleName, oModule.FileName ) );
}
}
}
}
As promised, a very simple code change. What you can however notice is that there is nothing that gives away that it is code for .NET Core. This code - AS IS - can compile under the full flagged .NET Framework too (did I mention .NET Core is compatible with .NET Framework?).
What you have to notice also are the notifications at the top of the editing window - read them and respond accordingly, they are mostly about missing things, like debug definitions, and errors...
To build the application, you can use the terminal again (dotnet build
) or the pre-defined build task, that can be invoked by CTRL+SHIFT+B.
At this point, the application is ready to run, you can do it using the debugger - to see how it works, the build-in terminal or the external terminal. To run it from the terminal, navigate to the folder containing the application and type-in dotnet run
. You will get a list of modules loaded by your application...
Run Everywhere
So we are here. Now we will move the application - the compiled version - to the Windows box and see how it runs there too...
On the Fedora (Linux) box, you will find the binary at the 'bin\Debug\netcoreapp1.0' folder. What you have to copy is the DLL (Yes! Under .NET Core, even a console application compiles to DLL and not EXE!) and the 'runtimeconfig.json' file... Put both in the same folder of your choice on the Windows box.
Open a command window and navigate to the folder you stored the binaries in.
Type in the magic command 'dotnet run
' and enjoy!
(It is an interesting thing to compare the list from the Fedora (Linux) and the Windows box.)
Points of Interest
For me, it is really exiting to be able to compile my code (and debug and test and write) once, on my box only, but to be able to distribute it to a good range of operating systems after that.
The sample I presented here is totally dumb, but turn to your imagination and you will see the possibilities... Also remember that .NET Core and the tools around are open source and you can make a difference!