Microsoft introduced the .NET Framework at the beginning of the 2000s. It was considered to be the successor of Windows COM programming and came with:
– several programming languages where C#.net and VB.NET are the two most used
– a web framework is known as ASP.NET in the alternative to the classical ASP
– a compiler and a new runtime environment named CLR
The .NET framework became popular towards companies around the world and a large community of developers quickly arose around it.
The framework has been improved over time. New features had been introduced, the .NET Framework has even been completely reworked at a certain stage in order to improve the framework performances and the developer experiences. We can list the introduction of the WPF for modern desktop applications, WCF for service-oriented applications, WWF for business workflows or the Entity Framework product introduced a new way for consuming data.
The .NET Framework has been designed to run on computers with a Microsoft Windows-based operating system. It is a non-free product with closed source code. In June 2004, Mono, an open-source project, has been initially released by an independent developer community in order to give an alternative to building and running .NET programs into other operating systems like Linux. The project was supported first by Novell and finally by Microsoft a few years later. However, Mono didn’t place itself as a direct competitor of the .NET Framework. It was considered by many developers to not be a real “serious” choice for their solutions. In the latest few years, technologies evolve very quickly. Due to the increasing number of platforms, the engineer’s vision is converging towards cross-platform products. Since the .NET Framework has a big lack of interoperability on non-Windows platforms, Microsoft then introduced the .NET Core which aims to give a cross-platform framework for developers. This is the fundamental idea behind the new .NET Core: being compatible with all devices running different operating systems. The .NET Core is considered by many as the new .NET Framework, this is not actually very fair because the .NET Core is not going to replace the .NET Framework – at least for the moment.
At the time of writing, the latest .NET framework version is 4.6.2, 4.6.2 for Mono too, and the .NET Core is at 1.1. Since things are growing and quickly change, some points that you’re going to read in this tutorial may become obsolete in the future.
In the rest of this tutorial, we will talk about the classical .NET framework, the .NET Core, and the Mono project. We essentially focus on the .NET Core, how to work with .NET Core since it is the most promising at the moment.
Here are the main parts of this article:
1) .NET Framework quick review
– How to determine what .NET Framework version is installed on your system
2) CLR and .NET Framework
3) Major components of .NET Framework
4) .NET Core
– Tips on .NET core
– Create .NET Core apps for Linux and Windows
5) Building apps for multi-targeting framework
– Running the application
– How to publish .Net Core apps to Linux platforms?
– Configure the publishing process
– Publishing .NET Core cross-platform app using CLI command dotnet
6) Building .NET Core app with Linux step by step
– The GAC in .NET Core
– Installing the .NET Core IDE
7) Create a .NET Core project from Visual Code
8) The Mono project
– Install Mono on linux Ubuntu 16.04
We’re not going to do a full detailed list of all .NET Framework features, a lot of words had been said about it. We just only give important keys to the framework.
The .NET Framework is often provided with Microsoft Windows. You can always download a specific version of the .NET Framework from Microsoft website. Almost the time, the executable has the name dotNetFx[Version] where [version] is the version number of the current download. Prior to any installation, it’s better a good practice to check the compatibility of your OS and the .NET Framework you’re going to install.
You can find the .NET framework installed at a specific location folder
C:\Windows\Microsoft.NET\Framework (for 32 bits) or C:\Windows\Microsoft.NET\Framework64 (for 64bits).
The figure below shows the major version of our system.
For 32bits folder
For 64 bits folder
The 32 bits folder contains a .NET version from 1.0 to 4.0. It means that the 32 bits versions of the .NET Framework from 1.0 to 4.0 are installed on our computer. Our system only contains the 64 bits version of .NET Framework from 2.0 to 4.0.
Our system has also .NET Framework 4.6.1 which is not displayed in a separate folder. This is true in the sense that versions after 4.0 had been built over the top of 4.0 itself. To check if the framework 4.6.1 is installed, we have to go to the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full in the Regedit tree
Tips: In the above figure, the NDP subkey lists all installed versions.
You then need to compare the value of the DWORD release key with the following table
Release DWORD |
version |
|
378389 | .NET Framework 4.5 | |
378675 |
.NET Framework 4.5.1 installed with Windows 8.1 or Windows Server 2012 R2 |
|
378758 |
|
|
379893 |
.NET Framework 4.5.2 |
|
393295 |
.NET Framework 4.6 installed with Windows 10 |
|
393297 |
|
|
394254 |
.NET Framework 4.6.1 installed on Windows 10 |
|
394271 |
.NET Framework 4.6.1 installed on all other Windows OS versions |
source : https://msdn.microsoft.com/fr-fr/library/hh925568(v=vs.110).aspx)
The release value on our system is 394271, so .NET Framework 4.6.1 is really installed on our system. This is the accurate way to determine which .NET Framework are installed on a given system.
.NET Framework is a set of assemblies while the Common Language Runtime is the fundamental component which manages and executes our codes (Garbage collector management, resources allocations, environment execution, threads priorities,… ). The CLR is contained in each .NET Framework version. The following grid shows each .NET Framework and the underlying CLR version.
.NET Framework version |
Major new features |
CLR Version |
1.0 |
1.0 |
|
1.1 |
ASP.NET, ADO.NET,… |
1.1 |
2.0 |
Generics,… |
2.0 |
3.0 |
WPF, WCF, WF,… |
2.0 |
3.5 |
Linq |
2.0 |
4 |
Expanded base class libraries
|
4 |
4.5 |
Support for Windows Store apps
|
4 |
4.5.1 |
Support for Windows Phone Store apps |
4 |
4.5.2 |
New APIs for transactional systems and ASP.NET |
4 |
4.6 |
Compilation using .NET Native
|
4 |
4.6.1 |
4 |
|
4.6.2 |
4 |
You can get more details at https://msdn.microsoft.com/fr-fr/library/bb822049(v=vs.110).aspx
.NET Framework is composed by 4 major components which are:
– ASP.NET for building and hosting web applications (IIS Webserver). At the earlier stage of .NET Framework, only Webform existed then came the MVC pattern and finally the WebAPI for RestFull architecture.
– Windows Presentation Framework (WPF) which is the technology to develop rich desktop client applications, the classical Windows forms application still be available in .NET Framework
– Windows Communication Foundation (WCF) is a new concept to expose and consume services. It is a complete framework to design a service oriented architecture with a bunch of supported protocols and tools to handle your data exchange through endpoints.
– Windows Workflow Foundation (WWF) is all about workflow and business process management. You can easily design a complex data flow and decision paths with WWF. Some major BPM Software is based on WWF like K2 BlackPearl products.
Visual Studio is the most famous IDE in .NET world. It has been improved along the years and a solid community supports it development. Nowadays you can download a free version of Visual Studio for your development purpose, some editions with enterprise-level functionalities are also available with charges. You can find a complete information at https://www.visualstudio.com/
The next expectation for many developers is definitely the ability to work and run their apps outside any Microsoft environment. It may be a Linux workstation, a OSX system, may be a Nintendo console or something else. That was impossible with the .NET Framework since it only supports Microsoft Windows platforms. This becomes a reality with the .NET Core. The .NET Core is designed to develop and run apps on any platforms.
Unlike .NET Framework, .NET Core is totally free and open-source, under MIT license. The full source code is available and can be downloaded. You can even recompile the entire framework if needed. However, we don’t need to rebuild it because there’s already a compiled and ready to use binary for all supported platforms.
These are some terms that you will often heard when dealing with the new .NET Core
.NET Core: is actually .NET Core runtime and framework
.NET Core SDK: is the whole .NET Core + Command Line Interface tools (see below).
.CLI tools: a set of utilities that allows interactions between the developer and the framework with the use of command lines. The program “dotnet” is one of its main component. We will see a concrete use case later.
: .Net Core Common Language Runtime is the runtime of .NET Core. The full source can be downloaded at https://github.com/dotnet/coreclr
.CoreFX: is a set of resources like base type, collections, threads,…You can read more at https://github.com/dotnet/coreclr
CoreCLR and CoreFX are inseparable things in order to run your program correctly. The first is the runtime which manages the execution environment, memory management, garbage collector,… and the second is where the first can find resources’ type – when the program needs to create an integer type, it can be found the integer structure inside the CoreFX, not in the CoreCLR.
As said so far, .NET Core is a cross-platform environment. Each supported platform has its own version of the coreCLR and CoreFX.
To understand how those things are used in real life, we’ll show you how to create your first .NET Core application, generate executable for both Windows and Linux platforms. We suppose that you already have Visual Studio 2015 Community
1. Create the project
Go to File > New > Project
In the New Project windows
The default solution looks like to the figure below
There are only two files in the solution:
– Program.cs: contains the main entry for our program.
using System; namespace MyFirstNetCoreApp { public class Program { public static void Main(string[] args) { Console .WriteLine("Hello .NET Core app"); Console .ReadLine(); } } }
Nothing’s particular in this file, it only displays a text to the console yet.
– project.json: is now the new file where we declare dependencies, versions etc…
{ "version": "1.0.0-*", "buildOptions": { "emitEntryPoint": true }, "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.1" } }, "frameworks": { "netcoreapp1.0": { "imports": "dnxcore50" } } }
Those kinds of stuff were before located in the file .csproj (or vbproj). They were in XML format and parsed with the MsBuild utility during the build time. With .NET Core, the engineer’s choice goes for JSON format.
Tips: The Visual Studio auto-completion still be available in the new JSON format.
Multi-targeting:
The actual project.json file is targeting the coreCLR with the lines:
"frameworks": { "netcoreapp1.0": { "imports": "dnxcore50" } }
The “dependencies” section is applicable at a global level, the modified file below shows a nested dependency which only applies for the .NET Core version. We also introduced two new targets which are the .NET framework 4.5.2 and the former .NET Framework 3.5
{ "version": "1.0.0-*", "buildOptions": { "emitEntryPoint": true }, "dependencies": {}, "frameworks": { "netcoreapp1.0": { "imports": "dnxcore50", "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.1" } } }, "net452": {}, "net35": {} } }
You can read more about target-frameworks at
https://docs.nuget.org/ndocs/schema/target-frameworks
Tips: Each time the project.json file is modified, Visual studio is calling the CLI tools “dotnet” to sync the dependencies according to the current content of the JSON file. It actually calls the command “dotnet restore” which may remove all unused dependencies or try to resolve newly added dependencies.
We can see that in the figure below
The output window also shows what operation was performed during the restore process.
Note: If you expand the project.json node, you will see an auto-generated file named project.lock.json.
You shouldn’t directly modify this file because it is used internally by the dotnet command utility.
The build process generates as many as executable there’s targeted frameworks
They can be found in the bin/Debug directory
If you look inside the net35 and net452 folders, the build process generates the application files which are the executable files. In .NET Core, there are no longer any .exe files. Instead, we have a .dll file extension.
Before you run the application, you have to define the default framework since there are no multiple choices yet.
In the above figure, we’ve selected .NET Core (.NETCoreApp, Version=v1.0). Once selected, we can fire by clicking the green arrow.
Running from the command line
Open a command line and go to the folder containing the .NET Core dll, then
dotnet MyFirstNetCoreApp.dll
One of the reasons we use .NET Core is the portability. Therefore we can publish a Linux binary of our app.
We need to do the following steps:
1- Modify the project.json
{ "version": "1.0.0-*", "buildOptions": { "emitEntryPoint": true }, "dependencies": {}, "frameworks": { "netcoreapp1.0": { "imports": "dnxcore50", "dependencies": { "Microsoft.NETCore.App": { // "type": "platform", "version": "1.0.1" } } }, "net452": {}, "net35": {} }, //Go to https://docs.microsoft.com/en-us/dotnet/articles/core/rid-catalog for all available RID // or here for the lastest version //https://github.com/dotnet/corefx/blob/master/pkg/Microsoft.NETCore.Platforms/runtime.json "runtimes": { "ubuntu.16.04-x64": {}, "win81-x64": {} } }
We remove first the directive type:platform and add the “runtimes” key where we enumerate all targeted platforms: ubuntu 16.04 (Xenial) and windows 8.1 64 bits in our case. By removing the key [type:platform], we instruct to the compiler that this will become a self-contained app – all needed components will be copied into a final package.
Right click the project > Publish
Select filesystem as publish target and give it a profile name
Select the Target location then Next
Select .NETCoreApp, Version=v1.0 as the target framework
Select ubuntu.16.04-x64 as the target runtime
You can notice that all runtimes configured in the project.json file are listed here
Click the Next button
Finally, you can publish the project.
If something goes wrong, you can always use the CLI to manually publish your app.
1-Open a console (a PowerShell console is better)
2-Make the app folder as the console working directory (at the same level as project.json file)
3-Run the following commands:
dotnet restore dotnet build dotnet publish -r ubuntu.16.04-x64
A published directory is now created at
C:\Users\dev\MyFirstNetCoreApp\bin\Debug\netcoreapp1.0\ubuntu.16.04-x64\publish
It contains all components to make self-contained apps, the size (~55MB) is quite high compared to any .NET Framework assembly.
You can copy this file to a Ubuntu workstation and the program can be run with using the following shell command (using the same CLI command)
dotnet MyFirstNetCoreApp.dll
The app executed on Ubuntu workstation
Note: In order to export to a specific platform, we should check if it exists at https://github.com/dotnet/corefx/blob/master/pkg/Microsoft.NETCore.Platforms/runtime.json
The platform name is case sensitive so beware of mistype.
Troubleshoots:
If you encounter an error like this
Error : Could not find a part of the path ‘C:\Users\dev\MyFirstNetCoreApp\bin\Realese\netcoreapp1.0\ubuntu.16.04-x64\MyFirstNetCoreApp.dll’, you can manually create the missing folder (eg. ubuntu.16.04-x64)
As we’ve seen so far .NET Core is designed to run on any platforms and will allow developing on any platforms too. That was impossible with .NET Framework since it is the only support Microsoft Windows platforms.
It’s now time for your developers to do your first step in the Linux environment. We’ll show you how to set up your development box step by step. We’ll choose the Ubuntu Linux distro 16.04 LTS. Don’t mind if you have a different distribution, the process is almost the same.
Installing .NET Core on Ubuntu
This process only involves a couple of steps
1- Update your source library by updating your /etc/apt/source.list
2- Install .NET Core package
3- Check the installation
Step 1: The official package is hosted at https://apt-mo.trafficmanager.net/repos/dotnet-release/
If you open to the previous URL you will see a folder named dists which contain three inner folders Trusty, Xenial, and Yakkety. Each of them corresponds to a specific Ubuntu version: Trusty is for Ubuntu 14.04, Xenial for Ubuntu 16.04, and Yakkety for Ubuntu 16.10. Future Ubuntu distribution may be added to this folder.
In our case, we have the Xenial version, so we copy and append the following text at the end of the /etc/apt/source.list
deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ xenial main
Note: Be sure you have permission to modify this file. If not, you have to use sudo command (see below)
You can also use the following command line:
sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ xenial main" >> /etc/apt/sources.list'
Update the package source repository by the command
sudo apt-get update
It may take a while to fetch all package’s metadata.
Step 2: Install the .NET Core
Let’s now list all dotnet available packages. Run the command.
sudo apt-cache search dotnet
At the time of writing, the stable version is dotnet-dev-1.0.0-preview2-1-003177
We install the .NET Core 1.1.0 SDK by the following command (CoreCLR)
sudo apt-get install dotnet-dev-1.0.0-preview2-1-003177 -y --allow-unauthenticated
This command downloads and installs the Dotnet package into our system.
Step 3:Checking the installation
On a command shell, enter:
dotnet --version
If everything goes well, you should have
Note: the dotnet command is officially named the driver of the .NET Core Command-Line interface (CLI). It is now the new utility to manage the .NET Core processing. The former utilities are:
-dnvm : utility that manages the .NET component version on the host
-dnu : Dotnet utility to resolve references, build and compile source code
-dnx : Dotnet execution environment, run the compiled source code ( creates the runtime environment, GC…)
Run the first app
You can run your first ‘Hello World’ app by the following command sequences:
You create the new working directory MyFirstDotNetCoreApp
mkdir ~/MyFirstDotNetCoreApp cd ~/MyFirstDotNetCoreApp/
Create minimal project files: Program.cs and project.json
dotnet new
The project.json file contains references and resolves dependencies ( in the same way that the file *.csproj did in the classical .NET app). Get all references mentioned in the file. This command should be executed after each modification on the file project.json
dotnet restore
Compile and run
dotnet run
The Global Assembly Cache is a well-known component for .NET developers. It is where all assemblies are located. On windows it is located at C:/windows/Assembly, assembly need to be loaded into the GAC in order to be callable and usable by a program. The GAC is very specific to windows so it can’t be used in a cross-platform environment. That’s why it’s no longer apply in the .NET Core world. Instead, the .NET Core created a hidden folder .dnx in the current user profile.
The next question coming into the developer’s mind is about IDE. Visual studio is only for windows computers. Hopefully, there’s now Visual Studio Code. It is free, cross-platform (run on Windows, Linux as well as on OSX), lightweight, open-source IDE that allows editing and debugging .NET Core program on any OS.
The first step is to go to https://www.visualstudio.com/
Then click to Visual Studio Code link to go to the download page.
Download a suitable package for your system. In this tutorial, we choose the .deb package because we’re using an Ubuntu distribution.
The installation process is pretty simple
After the installation finishes up, you can access the icon launch as follows
The default Visual Studio Code IDE
You can add extensions to Visual Studio Code IDE by clicking the icon extension or Ctrl+Shift+X shortcut.
We add the C# extension
Then click the reload button to activate the new extension
Tips: The extensions marketplace is at https://marketplace.visualstudio.com/vscode. You can find here a lot of tools that can improve your productivity.
You’ve got all the stuff you expect for an IDE: coloring, formatting, auto-completion
At the time of writing, Visual Code has only a few options in terms of project creation.
If you have already an existing .NET Core project, you can use the “Open Folder” button to load the whole project into the IDE. In our case, we’re going to start from scratch so we have to create an initial folder first – outside Visual Studio Code. This folder will contain the main file Program.cs and the project.json file which is required for any .NET Core apps. They are the minimum required files.
Hopefully, for us, the CLI “dotnet” utility can create a minimal default app.
Open a shell command and type
cd /tmp mkdir AppUbuntu cd AppUbuntu dotnet new ls dotnet restore ls
The initial project folder has been created, we can open it now from Visual Studio Code
The first time you open the project, you have to click on “Yes” to allow the auto-configure of your IDE.
You can now run the program
First of all, you need to enter debug mode. Then we need to select the runtime type. .NET Core Launch (console) is the right choice and we can finally launch by clicking the debug start button.
You’ve seen how to start a .NET Core app development under Linux using Visual Studio Code. At the time of writing, Visual Studio Code is fully functional but there’s a lot of improvements to be done.
In the following section, we’ll take a tour with Mono project, an alternative to build .NET apps primarily under Linux.
Mono is an open-source project intending to build cross-platform .NET Framework-based applications. It comes with its own C# compiler and a runtime environment under the MIT license. Mono offers some interesting features:
– Binary compatibility: you can import CLR compliant executables or libraries, just add them as a reference into your Mono project and directly use them without any modifications and vice-versa.
This is possible because Mono was built on the implementation of the ECMA’s Common Language Infrastructure.
– Microsoft Compatible API: major .NET Framework components (ASP.NET, ADO.NET, Windows Forms) can run without recompilation
– C# from 1.0 to 5.0 full feature-complete ( full support of Linq, dynamic,…)
– Mono SDK is available on Windows, Linux, OSX, BSD,…
– Available in x86, x64, ARM, power pc,… architectures
However, there are some compatibility limitations which are:
– Windows Presentation Foundation (WPF), Windows Workflow Foundation (WWF) are not supported at all
– Limited supports on Windows Communication Foundation (WCF)
– Limited supports on Asp.net 4.5 async task.
– If you’re using MVC4 or MVC5, some features aren’t working yet.
Before choosing Mono, be sure you read the compatibility pages at http://www.mono-project.com/docs/about-mono/compatibility/
The “mono-complete” package contains the complete runtime, development tools, and libraries. This process may take a while
apt-get install mono-complete
Mono-develop is a full IDE available on Linux
apt-get install mono-develop
Mono-develop is a complete IDE offering nice editing capabilities. An alternative is to use Xamarin studio.
The Mono-develop IDE is also very intuitive. If you’re ok with Visual Studio, working with Mono-develop is as well as simple too.
The chart below shows utilities to build and run .NET programs according the targeted version.
Tools |
.NET version |
||
.NET Framework |
.NET Core |
Mono |
|
Build/Compile |
csc |
dotnet build |
mcs |
Run |
cs |
dotnet |
mono |
We’ve seen along with this tutorial a quick tour on three independent technologies around Microsoft .NET :
– .NET Framework
– .NET Core
– the .NET Mono project
At the time of writing, .NET Framework is definitely the most stable, the most complete, the most used in enterprise solutions and will be for the next years. However, developers need to think on .NET Core possibilities when cross-platform applications are required, it is a good alternative to the .NET Framework. You can also consider the Mono project if you mostly work on the Linux workstation. Before making a definitive choice on the framework, it is always advised to stay tuned with the latest features. The .NET Core roadmap can be found at https://github.com/dotnet/core/blob/master/roadmap.md