Click here to Skip to main content
15,881,709 members
Articles / Programming Languages / C#

.NET Core: Compile Once, Run Everywhere

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
18 Jul 2016CPOL7 min read 25.7K   147   16   2
PoC - a small trip to see how .NET Core IS multiplatform

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...

VS Code on Fedora 23/Editing C# code

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:

  • Open VS Code
  • Open the built-in terminal (CTRL+`, or from the View menu)
  • Run these commands:
    1. mkdir hwapp
    2. cd hwapp
    3. dotnet new
    4. dotnet restore
    5. dotnet run

    (The origin of these lines is Microsoft, from here: .NET Core - Initialize some code)

    Terminal commands to create a .NET Core project on Fedora 23/Built-in terminal of VS Code

  • Now open the project (VS Code works with folder based workspaces so you actually have to open the containing folder), using File/Open Folder... menu.

Upgrade the Code

C#
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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Israel Israel
Born in Hungary, got my first computer at age 12 (C64 with tape and joystick). Also got a book with it about 6502 assembly, that on its back has a motto, said 'Try yourself!'. I believe this is my beginning...

Started to learn - formally - in connection to mathematics an physics, by writing basic and assembly programs demoing theorems and experiments.

After moving to Israel learned two years in college and got a software engineering degree, I still have somewhere...

Since 1997 I do development for living. I used 286 assembly, COBOL, C/C++, Magic, Pascal, Visual Basic, C#, JavaScript, HTML, CSS, PHP, ASP, ASP.NET, C# and some more buzzes.

Since 2005 I have to find spare time after kids go bed, which means can't sleep to much, but much happier this way...

Free tools I've created for you...



Comments and Discussions

 
QuestionUnfortunately.. Pin
2374118-Jul-16 6:36
2374118-Jul-16 6:36 
AnswerRe: Unfortunately.. Pin
Kornfeld Eliyahu Peter18-Jul-16 7:01
professionalKornfeld Eliyahu Peter18-Jul-16 7:01 
My main machine - not for development, but in whole - is Fedora for the last 8+ years. I'm running VS and other Windows related tools on VMs. I'm looking for a reliable development environment since then, but didn't left .NET behind as all the other alternatives (PHP, Python) where much worse (I'm a 99% web developer), even considering the free/open nature of those...
Now - ever first .NET is open source and free. The environment is not perfect yet, but I can have more control over it and I can set up an enterprise development environment at the cost of the hardware...
Granted that Microsoft done a lot of bad, threatened developers and enterprise (not only) users as dung sometimes, but now we can have the good part and use it - Microsoft has no exclusive control over it anymore...
Skipper: We'll fix it.
Alex: Fix it? How you gonna fix this?
Skipper: Grit, spit and a whole lotta duct tape.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.