Saturday, 1 October 2016

Welcome to .NET Core 1.0 Console Application

Let’s create our first .NET Core Console Application. We assume that we already have Visual Studio 2015 Update 3 and .NET Core 1.0.0 - VS 2015 Tooling Preview 2. If you have not installed .NET Core yet then I may recommend you to follow our initial discussion How to install .NET Core 1.0. Alternatively you can follow Microsoft Official Site directly.

Create a new Solution

We are going to create a new solution. If we don't perform this task then Visual Studio aromatically creates a solution for a project:
  • Open Visual Studio 2015.
  • Open New Project Screen through menu File >> New >> Project.
  • Select Blank Solution through Installed >> Templates >> Other Projects >> Visual Studio Solutions.
  • Name solution as “DotNetCore”. Set suitable location as “C:\ASP.NET Core\Welcome To .NET Core 1.0\”.
  • Click OK Button.
  • It will create a new solution.

Create new Console Application .NET Core

We are going to add a new Console Application.
  • Open Add New Project Screen through Solution Context Menu >> Add >> New Project or File >> New >> Project.
  • Select Class Console Application (.NET Core) through Installed >> Templates >> Visual C# >> .NET Core.
  • Name project as “DotNetCore.ConsoleApplication”.
  • Set suitable location as “C:\ASP.NET Core\Welcome To .NET Core 1.0\DotNetCore\” (selected by default to solution root).
  • Click OK Button.
  • Add Write greetings and ReadKey in Program.cs.
    •  Console.WriteLine("Welcome to .NET Core Console Application"); prints message on console,
    • Console.ReadKey(); holds the program from termination until a key is pressed.

 public class Program  
 {  
   public static void Main(string[] args)  
   {  
     Console.WriteLine("Welcome to .NET Core Console Application");  
     Console.ReadKey();  
   }  
 }  

Run Application in Debug Mode

To run application, we can chose any of following:
  • Press F5 or Debug Menu >> Start Debugging or Start Console Application Button on Toolbar to start application in debugging mode. It will start application console in debug mode.
  • Make project self contained and it will generate exe file which is directly executable. Please refer to Self Contained Deployment for more details.

Application Deployment

.NET Core allows us two methodologies for compilation and deployment as following, these methodologies can be configured and through project.json:
  • Framework Dependent Deployment
  • Self Contained Deployment

Framework Dependent Deployment

Framework Dependent Deployment allows to build and run application as dll, and in this mode application is executed through dotnet.exe. In this case a single distributable is created which is executed system provided runtime components. By default, Visual Studio 2015 uses this mode. If we observe build directory, then it contains dlls and config files only.
To execute application in this mode:
  • Open  command line prompt or Power Shell.
  • Go to output folder containing ConsoleApplication.NetCore.dll. In our example we have path as: C:\ASP.NET Core\Welcome To .NET Core 1.0\DotNetCore\ConsoleApplication.NetCore\bin\Debug\netcoreapp1.0.
  • Execute command dotnet ConsoleApplication.NetCore.dll
  • It will execute application.


 {   
  "version": "1.0.0-*",   
  "buildOptions": {   
   "emitEntryPoint": true   
  },   
  "dependencies": {   
   "Microsoft.NETCore.App": {   
   "type": "platform",   
   "version": "1.0.0"   
   }   
  },   
  "frameworks": {   
   "netcoreapp1.0": {   
   "imports": "dnxcore50"   
   }   
  }   
  }  

Self Contained Deployment

Self Contained Deployment allows to build and distribute application as an exe, which is execuatable directly. In this case an OS dependent disreputable is created which is executed contains all required runtime components. We may have to do few or more configuration changes in project.json in dependencies, runtimes and others as per requirements. In our case we have to just remove "type": "platform" from dependencies and to add runtimes section.  If we observe build directory, then it contains exe along with dlls and config files only.
To execute application in this mode
  • Open  command line prompt or Power Shell and enter ConsoleApplication.NetCore.exe command
  • Or directly double click ConsoleApplication.NetCore.exe in Windows Explorer.
  • It will execute application.

 {  
  "version": "1.0.0-*",  
  "buildOptions": {  
   "emitEntryPoint": true  
  },  
  "dependencies": {  
   "Microsoft.NETCore.App": {  
    "version": "1.0.0"  
   }  
  },  
  "frameworks": {  
   "netcoreapp1.0": {  
    "imports": "dnxcore50"  
   }  
  },  
  "runtimes": {  
   "win81-x64": {}  
  }  
 }   

Please refer to .NET Core Application Deployment for more details. Few important runtimes are:

  • win7-x86
  • win7-x64
  • win81-x86
  • win81-x64
  • win10-x86
  • win10-x64
  • osx.10.10-x64

Sample Source Code

We have placed sample code for this session in "Welcome To .NET Core Console Application_Code.zip" and "Welcome To .NET Core Console Application_SelfContained_Code.zip" in https://aspdotnetcore.codeplex.com/SourceControl/latest CodePlex repository.



4 comments:

  1. Nice information about .NET Core 1.0 Console Application my sincere thanks for sharing this post Please Continue to share this post
    Dot Net Training in Chennai

    ReplyDelete
  2. nice blog has been shared by you. before i read this blog i didn't have any knowledge about this but now i got some knowledge. so keep on sharing such kind of an interesting blogs.
    dotnet training in chennai

    ReplyDelete
  3. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete
  4. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete