After working so many years on .NET Framework, now it’s time for .NET Core. What has changed and why has it changed ? Let’s understand it in this article.
Content
- What is .NET Core?
- What is new about this framework?
- Getting started with .NET Core.
- Developing sample web application, using CLI.
What is .NET Core?
It is a new framework which was developed from scratch parallel to .NET 4.6 (Visual Studio 2015). Previously, it was named as .NET 5.0. Because of this, there was a confusion among developers that this framework is a successor of .NET 4.6, but that was not the truth. It is totally a new and different framework.
Image source: https://blogs.msdn.microsoft.com
You can see that in this release of new framework, a lot of new things got added, such as CoreCLR & CoreFX, replacing our traditional framework CLR & FCL which were bound to Windows platform. CoreCLR basically includes the garbage collector, JIT compiler, base .NET data types, and many low-level classes. On top of .NET Core, we have ASP.NET Core 1.0 and then, ASP.NET Core MVC.
Lots of buzz was going on in the market regarding this framework and given below is what we get.
What is new about this framework?
- Cross platform
We know .NET framework runs only on Windows platform. But, what about the developers working on other platforms? They were using Mono Framework, an open source framework compatible with .NET. This was not the product of Microsoft but supported this framework. With growing developers among different platforms, it became a must to make .NET Framework work with cross-platforms.
Developers of Windows were able to develop their application using the most powerful IDE i.e.- Visual Studio. But what about the Linux or Mac developers who don’t have such IDE like Visual Studio? Good news with this framework is that no more Visual Studio is needed for development. You can use any editor for development and can execute it using console.
To make it work on a cross platform, Microsoft built up a new set of runtime & libraries called CoreCLR and CoreFX which they compiled for every platform and generated builds.
With making .Net Core a Cross Platrom, Microsoft removed the tightly IIS bound "System.Web" and made it "Microsoft.AspNetCore" which will work for all platform hosting environment.
- Open source
With the world moving towards open source, Microsoft made this framework totally an open source. Also, the source code is available on GIT. You can now easily customize this framework according to your need.
- Optimized .NET runtime & libraries
With these changes, Microsoft also made changes to its libraries which were available in our GAC on installation of .NET Framework.
One example that I can think of, is “System” library.
The “System” library consists of many logical libraries, such as System.IO, System.Net, System.Configuration, etc. which getsloaded into memory every time you use “System” library. But, do you think there is a need to load the whole library when only a portion is needed? Suppose I need only System.IO, then my project should only refer System.IO instead of System as a whole. To make it a light weight and optimized, .NET Core now supports nuGet packages for each of this logical library and they will no more refer from GAC (which was available only on Windows platform)
- Introduction to CLI
.NET Core also introduces a Command line application called as dotnet.exe. This application allows us to,
- create an application
- execute the application
- run the Intermediate language
- host the CLR
for any platform.
- Completely modular
Every time when there was a new feature added in any component of .NET framework, a new version was released by Microsoft. For example – with ASP.NET MVC, there was a routing concept introduced untl ASP.NET MVC 4 which was there in Visual Studio 2012 (.NET framework 4.5). But with ASP.NET MVC 5, they introduced something called as Attribute Level Routing which was added in Visual Studio 2013 (.NET Framework 4.5.1). This kind of change in components may lead to introducing new release of framework version. But now, that is all gone with .NET Core, where everything is nuGet package. It became very easy to upgrade the component as it will introduce the release of nuGet package and not the whole framework. This makes everything modular.
- Cloud ready environment
With .NET Core, we can build cloud based internet connected applications, like Web Apps, IOT apps & mobile backend.
Getting Started
Go to http://www.dot.net
Download & follow the installation steps of .NET Core for every platform.
This framework doesn’t come in handy with Visual Studio by default. It is added as a separate installation and requires VS 2015 update 3 or you can download .NET Core SD separately. But in future versions, it could be integrated with Visual Studio setup.
Developing Sample Web application using CLI
Prior to “dotnet” command line application, in .NET Core RC1 & ASP.NET Core RC1, we had dnx tool for developing & launching the application. This dnx tool consists of 3 pieces,
- dnvm - Dotnet version manager – Installer to get dnx version
- dnu – Dotnet utility - Tool for managing dependencies, building & publishing the application
- dnx -Dotnet execution runtime – Used to execute the code.
But later, these tools were integrated within the single .NET Core Command Line Application i.e. – dotnet.
Let’s try to understand step by step how the “dotnet” tool helps us in developing the app.
Initial Setup
Once the installation of .NET Core is done, test it by opening the command prompt and typing dotnet.
Step 1- Open Command Prompt and go to the directory where you want to keep your project related files.
Step 2- Type command.
For developing console application,
>dotnet new –lang C#
Explanation
- New command is used to create project. By default, it will add a Program.cs & project.json file.
- C# specifies the programming language to be used. (Other option available is F#. VB is not yet available).
For developing web application,
>dotnet new –lang C# -t web
Note: One point to remember - With .NET Core, we can develop Console or web application. As of today, we cannot create Windows application.
Output
The folder will contain all the files.
Step 3- I will not make any changes in code and will use command to add all the dependencies in the project.
>dotnet restore
This command will restore all the dependencies added in project.json file. i.e. – Unpack all the dependent libraries within the nuGet package & restore.
This command then creates a new file project.lock.json containing unpacked dependent library names and version.
Step 4- Build the application using command.
>dotnet build
Step 5- Run the application.
>dotnet run
This command will host the server on console and provide us the port to run the application. We can now open our browser & type http://localhost:5000
And the website will start running.
Output
Step 6- Publish the code.
>dotnet publish
This command will publish the web app.
Conclusion
In this article, I tried to explain to you what has changed with .NET Core & why it has changed. In the next article, I will explain about ASP.NET Core MVC structural changes & added features. I hope you liked this small article. Please do comment whether it’s good or bad. Sharing is valuable no matter what. Thank you.
There is a nice video by Shivprasad Koirala which explains .NET Core in details. Note: (Some portions in this video are changed now. So, kindly check that in my article).