Hey, folks, I have been playing around with.NET Core for 1 month and there is still a lot to cover. Though by considering.NET CORE as a sea, which is then further divided into lagoon, lakes, ponds and rivers in the form of ASP.NET MVC, Entity Framework Core, ASP.NET CORE etc., I have no hesitation saying that I just had a few drops of rain over my mind and I felt really good to feel the monsoon coming for .NET developers, where we can see lots of new features coming to the .NET stack cross-platform, with performance benchmarks and open source.NET technologies, which makes Microsoft open to the world. I am a just normal developer, who had no knowledge of open source before, as I never heard from the colleagues or I should say, I was not involved with the community. With .NET being open source now, we have .NET core, which is built from scratch by running cross platforms, and cheap deployment on a a Linux machine, which was not possible before (Note*- Windows Servers are costly compared to Linux Servers). Thus, let's get started by digging into Core. If you want to get started with .NET Core with the steps for downloading SDK my honest suggestion is to go to my previous blog, as mentioned below.
Introduction
I believe that if you have followed each and every step as mentioned in the blog to start using .NET Core and you are happy to run your basic set of "HelloWorld" programs, which run just fine on Windows, Linux or Mac. Going forward, let's talk about how .NET and .NET Core are different from each other. .NET Core is NuGet based, which means all the dependencies are in the form of NuGet packages, which allow a local app deployment while in.NET Framework, and we have to be dependent upon .NET framework. Every time the new feature is added in the component of .NET Framework, the 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 a NuGet based approach, an Application deploys the new version without waiting for a .NET Framework to be widely accepted resulting in the easy upgrade to the 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 own Visual Studio to create and publish Web Applications. All the compiler, runtime and command line toolings are installed in a single go with a single install of SDK. Some .NET Core related commands are given below.
- Dotnet
Once you download the SDK, .NET 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, .NET command line tools are referenced to the path. In order to create a simple .NET CORE app, we can use .NET 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 dependencies 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 then creates a project.lock.json, which contains a final list of dependencies, which is relevant to the project.
- dotnet build
It is used to build your Application or compile your Application for an error checking.
- dotnet run
It is used to run your Application when you are done checking or compiling your program.
- dotnet publish
Once you complete your applications, you deploy them to the Web Server to be hosted and serve HTTP requests.
Dotnet publish --framework netcoreapp1.1 --output "E:\poopDel\publish" --configuration debug --no-build. Once I create my console Application and build the same, below are the files 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 run .NET run in a folder that contains the project.json file because 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 then loaded by .NET runtime loader, which hosts the DLL, when we run .NET 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 .NET restore.
- "runtimes": {
- "win10-x64": {},
- "win7-x64": {},
- },
When we build the Application, run the command, given below.
dotnet build -r win10-x64
This will generate the wrapper exe file which will in turn run via .NET 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, which has been installed and I will use local version. Here is my Project.json with runtimes, to 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"
- }
- }
- }
Once I run .NET build command with the respective runtime, it will create a folder for the same with the respective files. Here, we are talking about the context to generate the EXE file, so I will target win7-x64.
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 EXE file, it will work as it 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 .NET run.
Fig.5 Example Hello World.
Where is my ASP.NET?
I have been talking about console Application but the majority of our enterprise applications are Web based. ASP.NET Core projects 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 will be aware of how the things are implmeneted in ASP.NET Core. 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 be aware of how Visual Studio code can help us building .NET Core Application cross platform (*Note: We can use other IDE as well). With incremental updates of VS Code, it's one of the best text editor tools to build the Web based Application easily. Thus, 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 are done opening the project in VS Code, the screen given below will appear.
Fig.7. Setting up Visual studio code task and launch.json
Click Yes and Restore
Restore will run the .NET restore command for you and download all the necessary dependencies in order to run the program. Once we allow VS code, do all the manual things, which we did earlier, now, all the things will be taken care of by VS code and the respective Extensions.Vs code is smart enough to know the kind of project. It determines this is .NET Core project, creates the task and launches a .json file for the project to run and builds 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 the easy shortcuts and intellisense just like Visual Studio.
Restoring Packages
In order to find the extensions, go to the left menu bar of VS code and click 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. Once we have completed the package restore, we should build our program. Earlier, we were running .NET build for building our program, but with installed extension all the building and running of the Application has been configured with the simple shortcut: Ctrl+Shift+B for Build F5 for running. Let's have a look at compiling the program in Figure 8:
In order to run the program, we can simply press f5. In order to create our Web Application, we need the Web Server, which will serve our request and respond with the respective results to us. In ASP.NET, we have our old giant IIS Web Server, which has been there for ages and it matured enough to handle the traffic of the Websites but in ASP.NET Core, we have a new superpowered baby Server known as Kestrel. Kestrel is a light weighted Web Server for hosting ASP.NET Core Web Applications on any platform. It uses the library, which 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 Web Server. I found node.js quite similar to a kestrel Server. 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, and we have the Web Server and we configure our Web Server and then run it to handle the http requests. In order to configure the Server, we need to add a startupClass.
-
-
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Http;
- namespace asp.netCore {
- public class StartUp {
-
- public void Configure(IApplicationBuilder app) {
- app.Run(context => {
- return context.Response.WriteAsync("HellO ASP.NET CORE");
- });
- }
- }
- }
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()
-
-
-
-
- }
- }
- }
Now, let's run our program and see the power of kestrel. I know this might be confusing regarding how all the things are working. I will explain it 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 a simple hello world application. I hope, this article will be helpful to you to dig into the pipelines of .NET CORE. Keep reading and keep sharing.