Monday, January 1, 2018

Invoking web application from console application (.Net Core) via command prompt

In this article, I’ll be giving a walkthrough on how to create a console application and changing that into a web application. Or in other words, invoking a web application from a .Net core console application. And that too completely from command prompt. If you are a command prompt lover, you may love it. So, let’s gear up and proceed step-by-step.

Verify .Net Core
If you are creating a .Net Core application for the very first time, then it is good to verify whether it is installed on the system or not and this can be done by typing a simple command dotnet --version as shown below: 

Create Console application
Now we are sure that required setup is present on our machine, we can proceed and create our first Console application using command dotnet new console as shown below: 

On successful execution of above command, you will see that Program file and project file is created in your local directory and same can be verified by opening Program.cs in notepad using below command:

Building and Running Console application
Next step would be to see the output from the console application and that can be done by run command as shown below:

In above screen shot you can see that output “Hello World!” is shown on the screen which means both compilation and execution will be done using single command.
Console app into a Web app
For any web application, first we have to add dependency packages. So, let’s go ahead and add reference of AspNetCore library from Nuget and that too via command line as shown below:

In above screenshot, you can see that dependent package is added to .csproj file. Run your application and you would still be able to see the console application output as we didn’t change our app. If you are facing any reference related errors then run the restore command as dotnet restore, and things will be alright.
Add Startup file for Web application
As a practice, usually any web application starts with a Startup.cs file, we will also go ahead and add a Startup.cs file in our project with basic skeleton and namespaces added as shown below:

Next is to fill in the missing parts of Startup class file to make our code functional. Let’s quickly add the code to the Startup class:
  1. using System;  
  2. using Microsoft.AspNetCore.Builder;  
  3. using Microsoft.AspNetCore.Hosting;  
  4. using Microsoft.AspNetCore.Http;  
  6. namespace SampleCore  
  7. {  
  8.    public class Startup  
  9.    {  
  10.     public void Configure(IApplicationBuilder builder)  
  11.         {  
  12.         builder.Run(appContext =>   
  13.          {  
  14.         return appContext.Response.WriteAsync("Hey, I'm from Web!");  
  15.          });  
  16.         }  
  17.    }  
  18. }  

Hooking up the web application in console
At this point, if you will run the application, you will still get the output which is mentioned in Program.cs because we have not told the Main() about our Startup class.

So, let’s quickly go ahead and plug our web application into console application. Below is the code to do so:
  1. static void Main(string[] args)  
  2.         {  
  3.             var hostBuilder = new WebHostBuilder()  
  4.             .UseKestrel() //tiny web server. It can be replaced with any web server  
  5.             .UseStartup<Startup>()  
  6.             .Build();  
  8.         hostBuilder.Run();  
  9.         }  
Now we are all set to run our application.
Launching an application 
Go back to command prompt and fire dotnet run and you will see that your application is now an web application with web server up and running.

Hope you like this. Enjoy reading.