Showing posts with label Web Application. Show all posts
Showing posts with label Web Application. Show all posts

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.

Thursday, January 28, 2016

Fixing ‘DNX design time process error in VS 2015’

Exclusive Amazon Deals

Today I thought to create my first demo using Visual Studio 2015 for ASP.NET Core 1.0. So, I opened VS2015 and chose Web Application template under ASP.NET 5 Templates. Visual Studio started creating a project for me. Although my project was created but at the same time some DNX error was thrown as shown below:

I tried various options including restarting machine, re-installing Visual Studio, etc.
But Alas ! Nothing worked. But on surfing for few hours, I found a solution which worked for me.
You can run any of the below commands:
dnvm update-self OR dnvm upgrade
Open Visual Studio 2015 and do dnvm update using command line as:


Once this command was executed, I didn’t get that error again. Hope reading this blog will save your few minutes of troubleshooting. Enjoy learning.

Wednesday, January 27, 2016

Features of ASP.NET Core 1.0

Exclusive Amazon Deals
ASP.NET Core 1.0 is a new open-source framework for building modern Web applications. As compared to previous versions of ASP.NET there are many major changes happened. I’ll try to capture most of the changes in this blog post.

ASP.NET Core 1.0 was initially named as ASP.NET 5. I’m dedicating this blog post to showcase the major features of ASP.NET Core 1.0.

Single aligned web stack
ASP.NET Core 1.0 is a single aligned web stack on top of ASP.NET for Web API and Web UI as shown in below figure (image taken from mva):

Above figure clearly depicts that there is very less sharing between these three stacks. If you will see from ASP.NET template view, you will notice that all the three options are dimmed out as shown below:

Much leaner framework with reduced surface area
ASP.NET Core 1.0 is no longer based on System.Web.dll. As per dzone, typical HTTPContext object graph takes 30K in memory while new implementation takes around 2K.It means now you can take only those NuGet packages which are really required. It strictly follows pay-for-what-you-use model.

project.json file
A new JSON file named project.json is introduced which takes care of dependencies and versions. Below is the sample project.json file:

A very good thing is VS2015 provides the IntelliSense support for all the available NuGet packages and versions.

global.json file
This file is used to configure solution as a whole. By default, in includes below data:

Options for storing configuration settings
Unlike earlier ASP.NET verisons, Web.config is not the only place for storing the configuration settings. ASP.NET Core 1.0 allows you to read configuration values from range of sources like XML, JSON, environment variables. All the configuration sources can be set in a class file named Startup as shown below:

In cloud environment value of environment variables will automatically be used rather than local configuration values.

Client Side Dependency Management
Dependencies folder contains two subfolders corresponds to two package managers named Bower and npn. These folder tells which client side dependencies are managed by which tool.

Server Side Dependency Management
In ASP.NET Core 1.0, server side references are placed in References folder with corresponding target frameworks

More Options for Hosting
Cross platform support by .Net Core 1.0 opened up the new hosting options. Now user need not to rely only on IIS for web application hosting. This initiative was taken because IIS is not available on other platforms like Mac and Linux. On such platforms, ASP.NET applications can be self-hosted or kestrel web server can be used.

Inbuilt Dependency Injection Support
ASP.NET Core 1.0 includes a simple built-in inversion of control (IoC) container that supports constructor injection by default, but this can be replaced with any other container. Services are made available through dependency injection and can be setup in ConfigureServices method of StartUp class. I’ll emphasis more on this in my next blog.

Static files at wwwroot
Static files are those files which are served directly to clients. It includes HTML, image and JavaScript files. In ASP.NET Core 1.0, default location of static files is wwwroot of project and location of this wwwroot is defined in project.json. In other words, static files which are not located to wwwroot will not be accessible. Introducing this feature enhances the security capability of application and one need not to impose any exceptional guidelines to block access to delicate files.

Target Frameworks
In order to support cross platform development, applications built using ASP.NET Core 1.0 can target full .Net and .Net Core framework. It means you can run your application either on Windows or on Mac or Linux.

ASP.NET Core 1.0 features at a glance[reference – scott’s weblogs]:
  • Build and run cross-platform ASP.NET apps on Windows, Mac and Linux  
  • Built on .NET Core, which supports true side-by-side app versioning   
  • New tooling that simplifies modern Web development 
  • Single aligned web stack for Web UI and Web APIs   
  • Cloud-ready environment-based configuration  
  • Integrated support for creating and using NuGet packages  
  • Built-in support for dependency injection        
  • Ability to host on IIS or self-host in your own process    
                Basically this new ASP.NET(MVC + Web API + Web Pages = ASP.NET Core 1.0) is much more tuned for modern web development.