Introduction to .NET CORE

Introduction: As the title specifies “Introduction to .NET CORE“. I will be sharing my learning experience, intended focus is towards beginners. I will be using VS code and C# programming language as 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. .NET is evolving

In case you are not aware of.NET framework my honest suggestion would go to the link and learn what exactly is NET Framework. .NET framework first beta version was released by late 2000.NET 1.0. After that.NET has evolved a lot and it has been 16 successful year journey where.NET has been accepted by the community to develop various kind of application i.e. Windows desktop application, Web application, services, 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).

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

The following table shows the.NET version and 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. .NET Framework:

.NET Framework is primarily developed for windows. It is a matured framework and we have been using .NET since ages now. It ships with windows os and new versions get serviced using windows update or standalone download 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. .NET CORE

.NET Core is a new framework which is written from scratch, the whole .NET CORE concept is new and .The net core is not as matured enough as ours .NET framework.

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 things ” 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 too .NET CORE 1.0, go and watch the below video conference of Scott Hanselman, you will get all the insight planning what they were thinking and how they sort out all the design problems.

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

With iOS and Android ruling the mobile world. The mobile developers usually moving towards Java, Objective C, Swift, Hybrid etc. to develop a mobile application. Microsoft seems to be lacking 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 to a different platform.

  1. 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 https://www.microsoft.com/net/download

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.

Figure 6: Choosing OS

Once you click on Windows below UI will appear:

Figure 7: Choosing Environment

There are different flavors of environment that you can choose to run yours.NET CORE programs. I will be choosing to 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 https://www.microsoft.com/net/tutorials/csharp/getting-started

So I will be downloading.NET Core SDK in order to get started.

Figure 8: Download SDK.

I have a preinstalled .net core in my machine, So in order to check that I have dotnet on my machine I will just write the command dotnet as shown below:

Figure 9: Running dotnet.

So I have dotnet 1.0.1 installed on the machine the last time I downloaded the SDK from the web. So in order to check the don’t 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 mac machine. So I figured it out using a website called 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 in the context of this article.

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

So now once you download dotnet core SDK from the web which will be around 45mb only. See how small it’s .net is not old .net things are changing for good. Run the SDK

Figure 11:Installing Microsoft.NET Core 1.1.0

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

So in order to create our first program let go to the past and see how things have evolved as saving time in teaching. I started my Computer science perusing Bachelors of Computer application that was the first time I heard .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 goes wrong then again. So the whole one day used to be gone on installing and making machine ready to even just write “Hello World”. While now it’s a different story we didn’t do install anything except dot.net SDK and it hardly took 2 min to download depending upon your wifi speed and we don’t need to install Visual studio to run hello world. Even in the performance bench mark .net core has seen a huge improvement that is why it’s called lightning fast framework.

  1. Running your first dotnet core program

So now in order to create our first program using dotnet core follows 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 dotnet it’s setting up my packages as shown below:

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:

Figure 14: Project files.

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

So our first console application contains the following code:

Figure 15: Simple Hello world Program.

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

Figure 16: NET Framework v 64

While in dotnet core all the bunch of libraries which we need gets installed on

C:\Program Files\dotnet\shared\Microsoft.NETCore.App\1.1.0


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 do download the libraries via web.

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


Figure 18: Project.json file.

Here we can see it’s a json format with all the attributes 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 dotnet

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

  • Dotnet restore

Figure 19: Running dotnet restore.

We have logs called writing to lock file it means that dotnet creates a json file called project.lock.json which act as lock file containing all the dependencies need to run our application with the version.

Building our program:

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

Dotnet build

This command will compile your program, as shown below:

Figure 20: Running dotnet build.

Running our Program

Dotnet run

This command will run your program, as shown below:

Figure 21: Running dotnet run to run the program.

4.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 https://code.visualstudio.com/

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 already created project shown above in demos.
  • Select explorer window and select the folder where your project exists.

Figure 22: Explorer window of VS code.

Figure 23: Select the project folder.

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 command prompt

We will write

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

This is how VS Code looks like after that.

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.

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

So now the question arises how I am gonna build, 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:

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


Figure 29: Building your C# application using VS code.

For running your application in Debug mode.

F5


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.


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 working on cmd from VS code.

  1. Dotnet Watch

As a C# sharp developer I have been using the Visual studio, sometime if we want to change something 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 it would be we have a feature where we can make a change and hit refresh. So there should some tool who 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 there any change it code I will recompile the code.

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


Figure 33: Dot net watch.

Select dotnet-watch and follow the readme section.

“dotnet-watch is a file watcher for dotnet 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 going to add

“Microsoft.DotNet.Watcher.Tools”:
“1.0.0-preview4-final”

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

Go to project.json and in the tool section:

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

Restore the dotnet.

dotnet restore

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

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 are an application as shown below:


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

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

Acknowledgements

Hey, guys you can start your journey as a professional. NET core developer by starting it from here.

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 is a great tool for developers who uses mac or UNIX to unleash the power off.NET core or to checkout VS code as a text editor. I hope this article will be useful to start with basics of .net Core. Stay tuned for Asp.net MVC 6 using Vs code.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.