Share via


Exploring .NET Core | 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.

https://2.bp.blogspot.com/-ZEbGsBFOCsk/V-8y6rWy4gI/AAAAAAAAAPw/s1gRYpcGFVAvb-NnyZVQQR5ePboQ9CiTQCK4B/s640/CreateSolution.gif

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.

https://2.bp.blogspot.com/-8zNssqsGAXQ/V-8zAjLeHHI/AAAAAAAAAP4/CwT8n7ZJRYkyeYqA-dfieZYwUW-0rbRMgCK4B/s640/CreateApplication.gif

 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 choose any of following:

  • Press F5 or Debug Menu >> Start Debugging or Start Console Application Button on Toolbar to start the 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.

https://4.bp.blogspot.com/-YmYuPdI-mRE/V-8zCAgelpI/AAAAAAAAAQA/UjrBFCQOvOUtf6TjTCDkkmGiSrTl4g8qgCK4B/s640/RunApplication.gif

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 the application as dll, and in this mode, the 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 an application.

https://1.bp.blogspot.com/-mfktyRlVQVc/V_BmbI7mJPI/AAAAAAAAAQc/1g7j9b1wTH4w42kTiWMOvbC1hhSb9g29gCK4B/s640/FDD.gif

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

Self Contained Deployment

Self Contained Deployment allows to build and distribute the application as an exe, which is executable 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 the application.

https://1.bp.blogspot.com/-ccg5yBxIHwE/V_BrjFJuFEI/AAAAAAAAAQs/a8XoXDAhwu4IRp4Mqlnn2H12uF7RU4sagCK4B/s640/SCD.gif

 {    "version": "1.0.0-*",    "buildOptions": {  
   "emitEntryPoint": true    },  
  "dependencies": {  
    "version": "1.0.0"     "Microsoft.NETCore.App": {  
   }  
  },  
    "imports": "dnxcore50"    "frameworks": {  
   "netcoreapp1.0": {  
   }  
  },  
 }     "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.