Abstract— Hey, folks, I have been playing around with.NET Core for 1 month I would and there still a lot to cover. Though considering.NET CORE as a sea which is then further divided to lagoon, lakes, ponds and river in form of ASP.NET MVC, Entity Framework Core, ASP.NET CORE etc. I have no hesitation saying that I have just had few drops of rains over my mind and I felt really good to feel the monsoon coming for the.NET developers where we can see the lots of new features coming to.NET stack cross-platform, with performance benchmark, open source.NET technologies which make the Microsoft open to the world. I am a just normal developer who had no knowledge of open source before as I never heard from colleagues or I would say I was not involved with the community. With .NET being open source and now we have.NET core which is built from scratch with running cross platform, cheap deployment on a a Linux machine which was not possible before (Note*- Windows servers are costly as compared to Linux servers). So let’s get started with digging to the Core.
If you want to get started with Dot Net core with steps for downloading the SDK my honest suggestion is to go to my previous blog as mentioned below
- Introduction
So I believe that you have followed each and every step as mentioned on the blog to get start using.NET core and you are happy to run your basic set of “HelloWorld” program which just runs fine on Windows, Linux or Mac. So going forward let talk about how.NET and.NET Core is different from each other.
.NET Core is Nuget based which means all the dependencies are in form of NuGet packages that allows app local deployment. While in.NET framework we have to be a dependent upon.NET framework. Every time the new feature is added in the component of.NET framework Microsoft team has to ship the new framework for the same which results in waiting until a given.NET Framework version is widely adopted. With the help of nuget based approach application deploy the new version without waiting for a .net framework to be widely accepted resulting in the easy upgrade to components.
- Command line Tooling:
Command line Tooling gives us command line features to run and create the ASP.NET or.NET CORE application with the help of any Text editor, we don’t need our blown Visual studio to create and publish web applications.
All the compiler, runtime and command line tooling are installed in a single go with a single install of SDK.
Below is some.NET Core related command:
- Dotnet: Once you download the SDK dotnet command will show you the version and help regarding all the related commands as shown below:
Fig.1. An example of a dotnet version.
- dotnet new: When you install the SDK the dotnet command line tools is referenced to the path. So in order to create a simple.NET CORE app we can use dotnet new command to scaffold a simple hello world program in (figure 2.)
Fig.2. Creating Console Application using command line
using System; namespace ConsoleApplication { public class Program { public static void Main(string[] args) { Console.WriteLine("Hello Saillesh!"); } } }
- dotnet restore: Restores all the dependency defined in the project.json file
E:\poop>dotnet restore log : Restoring packages for E:\poop\project.json... log : Installing runtime.win7.System.Private.Uri 4.3.0. log : Installing runtime.win7-x64.runtime.native.System.IO.Compression 4.3.0. log : Installing runtime.win.System.IO.FileSystem 4.3.0. log : Installing runtime.win.System.Net.Primitives 4.3.0. log : Installing runtime.win.System.Net.Sockets 4.3.0. log : Installing runtime.win.Microsoft.Win32.Primitives 4.3.0. log : Installing runtime.win.System.Console 4.3.0. log : Installing runtime.win.System.Diagnostics.Debug 4.3.0. log : Installing runtime.win.System.Runtime.Extensions 4.3.0. log : Installing runtime.ubuntu.14.04-x64.runtime.native.System.IO.Compression 4.3.0 log : Installing runtime.ubuntu.14.04-x64.runtime.native.System.Net.Http 4.3.0. log : Installing runtime.any.System.Reflection.Extensions 4.3.0. log : Installing runtime.ubuntu.14.04-x64.runtime.native.System.Net.Security 4.3.0. log : Writing lock file to disk. Path: E:\poop\project.lock.json log : E:\poop\project.json log : Restore completed in 23336ms.
It than creates a project.lock.json which contains final list of dependencies which is relevant to the project.
- dotnet build: to build your application or compile your application for error checking.
- dotnet run: to run you application when you are done checking or compilling you program.
- dotnet publish: Once you complete your application you deploy them to the web server to be hosted and serve the HTTP requests.
Dotnet publish –framework netcoreapp1.1 –output “E:\poopDel\publish” –configuration debug –no-build
So once I create my console application and build the same below are the file I get inside bin-debug folder
Fig.3. Project binaries in .NET Core.
Previously in.NET Framework we used to have our executable file EXE. In .net core, we are not running any exe in order to run our program we simply running dotnet run in a folder that contains the project.json file because of project.json files contain all the instruction and dependencies details for the application. The code of our c# application is compiled into .dll this dll is than loaded by dotnet runtime loader that hosts the dll when we run dotnet run command.
There is no mainline exe for the same. We can create a wrapper exe for frameworks by targeting the runtime as specified below:
Set the runtime in Project.json file and run dotnet restore
"runtimes": { "win10-x64": {}, "win7-x64": {}, },
And when we build the application run the below command:
dotnet build -r win10-x64
This will generate the wrapper exe file which will in turn run via dotnet runtime. We need to ensure that we remove the platform attribute from the project.json file as shown below:
"dependencies": { "Microsoft.NETCore.App": { "version": "1.1.0" } },
This is done because I am not going to use .NET core platform that’s installed, I will use local version.
So here is my Project.json with runtimes which I am referring:
{ "version": "1.0.0-*", "buildOptions": { "debugType": "portable", "emitEntryPoint": true }, "runtimes": { "win7-x64":{}, "win10-x64":{}, "osx.10.10-x64": {}, "ubuntu.14.04-x64": {} }, "dependencies": {}, "frameworks": { "netcoreapp1.1": { "dependencies": { "Microsoft.NETCore.App": { "version": "1.1.0" } }, "imports": "dnxcore50" } } }
So now once I run the dotnet build command with the respective runtime it will create a folder for the same with respective file. Here we are talking about the context to generate the EXE file so I will target win7-x64.
So once our progam gets compiled let’s check our executables.
Fig.4. Wrapper exe generated targetting the specific framework.
Now if I try to run the exe file it will work as it’s used to work with .NET framework. This exe isn’t an app but it’s just a wrapper around dotnet.exe to run it on a click rather running dotnet run.
Fig.5 Example Hello World.
- Where is my ASP.NET?
So I have been talking about console application but the majority of our enterprise application are Web based. ASP.NET Core project are console applications which will run the server execution environment and then process our http requests. We will look into the granular level so that we would be aware how actually the things are implmeneted in ASP.NET CORE.
So in order to evolve our console application to web application, I will be using Visual studio code to show this demo so that we will aware of how visual studio code can help us building .NET Core application cross platform (*Note: We can use other IDE as well). But with incremental updates of VS Code, it’s one of the best text editor tools to build web based application easily.
So we will create a new project and open the project in Visual studio code using code . as shown below:
Fig.6. Example Opening .NET core project in VS code.
Once we are done opening the project in Vs Code below screen will appear
\
Fig.7. Setting up Visual studio code task and launch.json
Click on yes and Restore.
Restore will run the dotnet restore command for you and download all the necessary dependency in order to run the program. Once we allow Vs Code do all the manual we were doing earlier on. Now all things will be taken care by VsCode and the respective Extensions.Vs code smart enough to know the kind of project, it determines this is .net core project and create the task and launch.json file for the project to run and build the same easily with the task.
Fig.8.Example VS code creating the tasks for the projec to run.
- Extensions – Here are the plugins which help us in the development of the application, the Extensions can vary accordingly which will help you to be more productive and help you with easy shortcuts and intellisense just like the Visual studio.
Restoring Packages:
In order to find the extensions go to the left menu bar of the vs code and click on the last menu Extensions as shown in Figure 7.
You can download the respective extensions as per your requirement. For basic C# I have installed C#, C# extensions, html extension for creating html pages.
So once we have completed the package restore we should build our program, earlier on we were running dotnet build for building our program, but with installed extension and taks all building and running of the application has been configured with the simple shortcut:
Ctrl+Shift+b for BUILD
F5 for running
Let have a look on compiling the program in Figure 8:
In order to run the program, we can simply press f5. So now in order to create our Web application, we need to the web server which will server our request and response the respective result to us.
In ASP.NET we have our old giant IIS web server which has been there since ages and it matured enough to handle traffic websites. But in ASP.NET Core we have a new superpower baby server known as Kestrel.
Kestrel is a light weighted web server for hosting ASP.NET Core web application on any platform. It uses the library the is also used by node.js. I am not a node.js developer but one of my friends gave me a simple demo for running the webserver. I found node.js quiet similar to kestrel server.
So in order to get the web server we need to add the respective dependency in Project.json along with our Http dependency which will help us with Http power as shown in Figure 9:
Now we are done with Downloading Dependency we have the web server and configure our webserver and than run it to handle the http Requests.
In order to configure the server we need to add a startupClass
//This namespace will help us build our web application using Microsoft.AspNetCore.Builder; //This namespace will hold our application and host it using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; namespace asp.netCore { public class StartUp { //this method will be called bu the runtime public void Configure(IApplicationBuilder app) { app.Run(context=> { return context.Response.WriteAsync("HellO ASP.NET CORE"); });}}}
Now in program.cs we need to configure our web server and run it.
using System; using asp.netCore; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; namespace ConsoleApplication { public class Program { public static void Main(string[] args) { var host=new WebHostBuilder() //creating the webHostbuilder instance .UseKestrel() //use kestrel webserver to run our application .UseStartup() //use startup class contains the startup method .Build();//builds the server and host the application //run the Web application host.Run(); } }}
So now let run our program and see the power of kestrel, I know this might be confusing how all things are working. I will explain all once we are done launching our first asp.net to the browser. We need to first build our program and then run it with F5. I hope our first launch of ASP.NET is successful just like ISRO launches the satellites.
Ready Steady Go
We have successfully launched a small web application with simple hello world. I hope this article will be helpful to you to dig into the pipelines of.NET CORE. Keep reading keep sharing.
2 responses to “Learning Awesome Open Source.NET Core Deep Down that will blow your mind.”
[…] read my previous articles https://sailleshpawar.wordpress.com/2016/12/10/introduction-to-net-core/ https://sailleshpawar.wordpress.com/2017/02/20/learning-net-core-deep-down/ where I have talked about .NET CORE using VS code deep down. In case if you still face any problem […]
LikeLike
[…] https://sailleshpawar.wordpress.com/2017/02/20/learning-net-core-deep-down/ […]
LikeLike