Introduction 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 recommend you to follow our initial discussion How to install .NET Core 1.0.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 automatically 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.

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. o Console.WriteLine("Welcome to .NET Core Console Application"); prints message on console, o 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 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 application. { "version": "1.0.0-*", "buildOptions": { "emitEntryPoint": true }, "dependencies": { "Microsoft.NETCore.App": { "version": "1.0.0" } }, "frameworks": { "netcoreapp1.0": { "imports": "dnxcore50" } }, "runtimes": { "win10-x64": {} } } Please refer to .NET Core Application Deployment for more details. 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.

  1. public class Program   
  2. {   
  3. public static void Main(string[] args)   
  4. {   
  5. Console.WriteLine("Welcome to .NET Core Console Application");   
  6. Console.ReadKey();   
  7. }   
  8. }  
Run Application in Debug Mode

To run the application, we can chose any of the 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.

project

Application Deployment

.NET Core allows us two methodologies for compilation and deployment, as following. These methodologies can be configured 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 by 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.

project

  1. {   
  2. "version": "1.0.0-*",   
  3. "buildOptions": {   
  4. "emitEntryPoint": true   
  5. },   
  6. "dependencies": {   
  7. "Microsoft.NETCore.App": {   
  8. "type": "platform",   
  9. "version": "1.0.0"   
  10. }   
  11. },   
  12. "frameworks": {   
  13. "netcoreapp1.0": {   
  14. "imports": "dnxcore50"   
  15. }   
  16. }   
  17. }   
Self Contained Deployment

Self Contained Deployment allows us to build and distribute applications as an exe, which is executable directly. In this case, an OS dependent disreputable is created which contains all required runtime components for executing. We may have to do a few or more configuration changes in project.json in dependencies, runtimes, and others, as per our 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": { "type": "platform", "version": "1.0.0" } }, "frameworks": { "netcoreapp1.0": { "imports": "dnxcore50" } } }

  1. {   
  2. "version": "1.0.0-*",   
  3. "buildOptions": {   
  4. "emitEntryPoint": true   
  5. },   
  6. "dependencies": {   
  7. "Microsoft.NETCore.App": {   
  8. "version": "1.0.0"   
  9. }   
  10. },   
  11. "frameworks": {   
  12. "netcoreapp1.0": {   
  13. "imports": "dnxcore50"   
  14. }   
  15. },   
  16. "runtimes": {   
  17. "win10-x64": {}   
  18. }   
  19. }   
Please refer to .NET Core Application Deployment for more details.

Sample Source Code

I 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 Codeplex repository. 


Similar Articles