ASP.NET 5 Console Application

Yep, that's right! ASP.NET 5 is not only for web, but supports Console Applications

Strictly speaking, it is a simple Console Application that runs as a NuGet package in the new ASP.NET 5 world and thereby also could include the benefits of a .NET Core project:

  • Cross Platform
  • Cloud and IoT optimized
  • No Overhead
  • Modularization (project.json)

Many people get confused at this point so let's light it a bit up.

 

1. First let's create a new Console Application:

Note: It's located under "Web"

 

2. Now Let's write some code, how about Fibonacci:

Note: The context is not static (public void Main)

using System;
namespace ConsoleApp1
{
    public class Program
    {
        public void Main(string[] args)
        {
            Console.WriteLine("Fibonacci on ASP.NET 5:");
            for (int i = 0; i < 15; i++)
            {
                Console.WriteLine(Fibonacci(i));
            }
    
            // keep console open
            Console.ReadLine();
        }
        public int Fibonacci(int n)
        {
            int a = 0;
            int b = 1;
            for (int i = 0; i < n; i++)
            {
                int temp = a;
                a = b;
                b = temp + b;
            }
            return a;
        }
    }
}

 

3. Optional: Select  .NET Core:

 

4. Run and voila!

 

You can use this kind of Console Application under Windows, Linux and MacOS. It's well suited to handle backgound tasks, especially for IoT and in the cloud.

I'm using it together with hangfire.io to do some database commands aswell as fetching/updating data into my database for my Online Youtube Converter.

 

 

Additional:

 

1. If you want to run this through command line, have a look into your project.json:

 

2. Now start the application via dnx:

dnx . ConsoleApp1


Mahmut Jomaa is a Software Engineer from Germany.

Currently he attends university to gain more knowledge in Computer Science.


Security code Refresh