Deep Dive Into .NET Core

Introduction

As the title specifies, I will be sharing my learning experience; the intended focus is towards beginners. I will be using VS code and C# programming language for writing the demos.

Agenda

  • .NET is evolving
  • Introduction to .NET CORE
  • Running your first dot net core program
  • Visual Studio Code
  • Dotnet Watch

1.0 .NET is evolving

In case you are not aware of .NET framework, my honest suggestion would be to go to the link and learn what exactly NET Framework is. .NET framework's first beta version was released by late 2000, .NET 1.0. After that, .NET has evolved a lot and it has been a successful 16 year journey in which .NET has been accepted by the community to develop various kinds of applications; i.e., Windows desktop applications, Web applications, services, and mobile application. As it’s evolving, now a single .NET framework is matured enough and has a family of frameworks depending upon the OS platform that we want to target (Windows, Linux, iOS, Android).

.NET Family
Figure 1: NET Family Image source: https://blogs.msdn.microsoft.com

The following table shows the .NET version and release date.

.NET version release date
Figure 2: NET version and release date

In the current ecosystem of .NET frameworks, we have .NET framework, .NET Core, and Xamarin.

1.1 .NET Framework

.NET Framework is primarily developed for Windows. It is a matured framework and we have been using .NET for ages now. It ships with Windows OS and new versions get serviced using Windows updates or standalone downloads from the web. It’s a collection of base class libraries which are used to develop various kinds of applications (Windows client {application WPF, Windows forms}, Windows Store apps, Web app {ASP.NET Webforms, ASP.NET MVC}) and much more.

1.2 .NET CORE

.NET Core is a new framework which is written from scratch. The whole .NET Core concept is new and it is not as mature as our .NET framework.
.NET CORE
Figure 3: NET for all Image source: https://www.simicart.com

Microsoft was going with ASP.NET 5.0 as the name because 5 is greater than 4.6. The famous sentence said by Scott in one of the conference videos is: "life is complicated, the organization is complicated, technical decisions are complicated”. My honest suggestion, if you are curious enough to know why they changed the ASP.NET 5.0 to .NET CORE 1.0, go and watch the below video conference of Scott Hanselman, you will get all the insight planning about what they were thinking and how they sorted out all the design problems.

NDC Conference
Figure 4: NDC Conference

.NET CORE is an open source cross platform which means that we can run our .net application on Windows, MAC and some UNIXES.

The .NET CORE home repository is available on https://github.com/dotnet/core.

1.3 Xamarin

With iOS and Android rule the mobile world, mobile developers usually move towards Java, Objective C, Swift, Hybrid etc. to develop a mobile application. Microsoft seems to be lagging behind in engaging mobile developers to their set of technologies(.NET skills). The Mono open source project was launched on July 19, 2001, as a .NET linux version. In July 2011 Mono was licensed. On February 24, 2016 Microsoft acquired Xamarin. So now with the help of Xamarin a .NET developer can also easily develop a mobile application which can target a different platform.

1.4 Introduction to NET CORE

Before starting with the demo, let’s learn how to get .NET Core on your machine. In order to get the .NET core go to here.

install .net core
Figure 5: Choosing the framework

Click on .NET core. During my last visit to the website lots of things have changed. Anyhow, I am also learning. Click on Windows as I am using a windows machine. I will select Windows.

install .net core
Figure 6: Choosing OS

Once you click on Windows the below UI will appear.

install .net core
Figure 7: Choosing Environment

There are different flavors of environments that you can choose to run your .NET CORE programs. I will be choosing the command line/another environment as I will demonstrate the programs using command prompt and Visual studio code. Click on the command line and follow the instructions as discussed in the video tutorial.

We will be using C# as our primary programming language. If you are new to C# you can learn the basics to get started and unleash the power of C# from here. So I will be downloading .NET Core SDK in order to get started.

install .net core SDK
Figure 8: Download SDK.

I have a pre-installed .net core in my machine, so in order to check that I have .net in my machine I will just write the command dotnet as shown below.

dotnet command
Figure 9: Running dotnet.

So I have dotnet 1.0.1 installed on the machine from the last time I downloaded the SDK from the web. So in order to check the dotnet in Linux how will I know to which command to run? I had the curiosity to run .net code program in Linux but I didn’t have Ubuntu or a Mac machine. So I figured it out using a website called Codenvy.

Codenvy
Figure 10: Codenvy the saver.

Code envy is Developer workspaces to modify, run and debug code in the cloud or on-premises. I got a workspace to run .net on Ubuntu just to have felt that I ran the .net program on an Ubuntu machine. I will not be talking about how I get started on Codenvy, as it’s not within the context of this article.

Codenvy
Figure 10: Running .net core on Ubunuty using Codenvy services, running dotnet command to check the version of .net.

So now once you download .net core sdk from the web it will be around 45mb only.  Run the sdk.

Installing .NET Core
Figure 11:Installing Microsoft.NET Core 1.1.0

Once the installation gets completed you are good to go and can run your first .net core program.

So in order to create our first program let's go to the past and see how things have evolved to save time in teaching. I started my Computer Science career perusing Bachelor of Computer applications, and that was the first time I heard of .NET, VB.NET, C#. I remember my practical class where we used to run all our demos and play around with code. I remember this three guys when I was installing vs 2008.


Figure 12: Software engineer seems to be like these for me.

It took almost 1 hour to install and if something went wrong then you started again. So the whole day used to be gone on installing and making a machine ready to even just to write “Hello World”. While now it’s a different story; we didn’t install anything except dot.net sdk and it hardly took 2 minutes to download depending upon your wifi speed and we don’t need to install Visual Studio to run hello world. Even in the performance benchmark .net core has seen a huge improvement that is why it’s called a  lightning fast framework.

2.0 Running your first .net core program

So now in order to create our first program using .net core follow the simple steps as shown below.

  • Make a directory (md FirstDotnetProgram)
  • Go to directory (cd FirstDotnetProgram)
  • Write dotnet new

So as I have installed a new version of .net it’s setting up my packages as shown below.

dotnet new
Figure 13: Creating new C# console application

Once this step is complete we are ready to go. We check our files which got created when we run dotnet new as shown below.

dotnet new
Figure 14: Project files.

So now we have Program.cs which contains the actual C# program, project.json file consists of all the dependencies which our program is using in order to run.

So our first console application contains the following code.

.net core
Figure 15: Simple Hello world Program.

In our primary .net framework when we run our program the clr used to load some base class libraries which used to stay on .net framework folder in windows as shown below.

.NET Framework v 64
Figure 16: .NET Framework v 64

While in .net core all the bunch of libraries which we need gets installed on
C:\Program Files\dotnet\shared\Microsoft.NETCore.App\1.1.0

.NET core location
Figure 17: .NET core location.

So here there is no dependency with the Microsoft.net framework folder as in the case of .net framework. Here all the shared libraries are downloaded and those libraries come around using nuget packages which are a package manager to download the libraries via web.

Let’s have look on our other file Project.json.

Project.json file
Figure 18: Project.json file.

Here we can see it’s a json format and all the attributes are self-explanatory.

Microsoft.NETCore.App specifies we are trying to make a core application which will run cross platform.

  • Version specifies the version being used
  • Build option for building your app
  • Dependencies – The dependencies your project is using with .net

Now the first time we run our program we need to restore the package for project.json as shown below.

  • Dotnet restore

    dotnet restore
    Figure 19: Running dotnet restore.

We have logs called writing to lock the file, which means that .net creates a json file called project.lock.json which acts as lock file containing all the dependencies needed to run our application with the version.

Building our program

In order to build your program, you need to type the below command.

Dotnet build

This command will compile your program, as shown below.

dotnet build
Figure 20: Running dotnet build.

Running our Program

Dotnet run

This command will run your program, as shown below.

dotnet run
Figure 21: Running dotnet run to run the program.

3.0 Visual Studio Code

“Visual Studio Code is a code editor redefined and optimized for building and debugging modern web and cloud applications. Visual Studio Code is free and Open Source.”

Via- https://code.visualstudio.com/

You can download VS code from here.

VS code has proven itself to be best text editor available in the market with support from Microsoft and getting support for various languages. It has a good extension for developers to speed up the development time for developer eg C#, C# extension for VS Code, MONO debug etc.

  • After installing Vs code open Vs code as a new window
  • You can create a new directory or open the already created project shown above in demos.
  • Select explorer window and select the folder where your project exists.
VS code
Figure 22: Explorer window of VS code.

VS project folder
Figure 23: Select the project folder.

VS code project structure
Figure 24: Project structure in Vs Code.

Now you can create your own folder or files as per your project requirement

  • In order to open vs code from the current project from the command prompt

We will write,

Opening project in VS code using CMD
Figure 25: An Opening project in VS code using CMD.

This is how VS Code looks after that.

VS Code project structure
Figure 26: Displaying the project structure.

Once it figures out the type of application you are running it will download the extension required for speeding up development workflow.

VS Code add new assets
Figure 27: VS Code prompting to add new assets to the project.

So now the question arises how I am gonna build and run my application. Download the below extension for easy and rapid development in Vs Code as shown below.

  • C#
  • MONO debug
  • C# extensions

Go to extension and search for the required extension and then install the same. As shown below,

Install extension in VS code
Figure 28: Installing new extension in VS code.

Once you install the above mentioned extension, in order to compile your application, you will type

Ctrl+shift+b

Ctrl+shift+b
Figure 29: Building your C# application using VS code.

For running your application in Debug mode.

F5

Debugging C# application in VS code
Figure 30: Debugging C# application in VS code.

Running without debugging

Ctrl+f5


How about if we want to run cmd commands using VS code:

We can also run cmd in VS code with simple tricks as shown below and run your application via cmd

  • Type ctrl+~ In VS Code window
  • The Cmd terminal will open.

Figure 31: Opening CMD in VSCODE.

Now, you can write your cmd command to run your app as we did earlier.

CLI command
Figure 32: Running CLI commands dotnet from VS code.

We can run any cmd command to perform the task you want to perform. Isn’t is useful and fascinating? I love this part about working on cmd from VS code.

4.0 Dotnet Watch

As a C# developer, I have been using Visual Studio sometimes if we want to change something like our code in our view or C#. We just make a change, build the project, and refresh the window to check the updated changes in our code. How great would it be if we had a feature where we can make a change and hit refresh. So there should some tool that would watch all of that code that we are changing there when Dotnet watch comes into the picture. Dotnet watch watches the code and if it finds any change in the code it will recompile the code.

So in order to use the same we will search dotnet watch or goto https://github.com/aspnet/DotNetTools

Dotnet watch
Figure 33: Dot net watch.

Select dotnet-watch and follow the readme section.

“dotnet-watch is a file watcher for .net that restarts the specified application when changes in the source code are detected. “

Via: https://github.com/aspnet/DotNetTools/tree/dev/src/Microsoft.DotNet.Watcher.Tools

So we are going to add,

"Microsoft.DotNet.Watcher.Tools": "1.0.0-preview4-final"

In our project.json file restore the package using dotnet restore as shown below.

Go to project.json and in the tool section.

Adding dotnet watch configuration
Figure 34: Adding dotnet watch configuration in Project.json .

Restore the dotnet.

dotnet restore

So now in order to check the same we will run our application using

dotnet watch run

dotnet watch run
Figure 35: Dotnet watch waiting forthe file to get changed.

Once we run our application using dotnet watch run, dotnet watch will watch for the file to change and rebuild an application as shown below.

Dotnet watch
Figure 36: Dotnet watch restarting dotnet automatically once the file gets changed.

So if we will change anything in our code, .NET watch will trace that file and rebuild our program to execute it again. See how great this tool is and how useful it will be when we are working with web technologies, changing the code, and hitting refresh?

Acknowledgments

We found how easy it is to write your simple application using the command line or Vs code. Vs code is improving day by day. It’s a great tool for developers who use Mac or Unix to unleash the power of .NET core or to check out VS code as a text editor. I hope this article will be useful to start with the basics of .net Core. Stay tuned for Asp.net MVC 6 using Vs code.