Friday, January 29, 2016

Consuming Static Resources in ASP.NET Core 1.0

Exclusive Amazon Deals
As discussed in my previous article, static files are those files which are served directly to clients. It includes HTML, CSS, 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. So, all the static files which you want to serve to clients must be placed at wwwroot location.  

My article focuses on how to access and expose such static resources to client. Now let’s proceed step-by-step to create awareness on how to use static files.

Step 1 - Create an empty ASP.NET 5 project
Open Visual Studio 2015, select an empty project template for creating new ASP.NET application as:

 Step 2 – Introduce Static File
Create a HTML file under wwwroot folder. Below is my sample HTML file named SampleHTML:


Step 3 - Configure Middleware
Middleware are small components that can be combined as part of application in order to handle HTTP request response pipeline. So in order to serve static files. Midddleware has to be configured by adding static files to the pipeline. And this can be achieved by using extension method named UseStaticFiles in Startup class. But before that, add the required dependency in project.json file as highlighted below:



Next is to update Configure method in Startup class as:


Step 4 - Build and run
Build your code. Now type address as http://<your app name>/SampleHTML.html, your page will be rendered as:



Hope you enjoyed playing with static files. Benefit of using static files in such a way is that you need not to put extra effort to save your sensitive static file because if your file is not available at wwwroot, it won’t be rendered to client. Enjoy learning !!!

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.

Thursday, January 21, 2016

Introducing ASP.NET Core 1.0 and .NET Core 1.0

Exclusive Amazon Deals
In one of the recent announcements, Microsoft has changed the name of ASP.Net 5 and .Net Core 5. Official blog post says:

However, naming the new, completely written from scratch ASP.NET framework "ASP.NET 5" was a bad idea for a one major reason: 5 > 4.6 makes it seem like ASP.NET 5 is bigger, better, and replaces ASP.NET 4.6. Not so. So we're changing the name and picking a better version number.

  • ASP.NET 5 is now ASP.NET Core 1.0.
  • NET Core 5 is now .NET Core 1.0.
  • Entity Framework 7 is now Entity Framework Core 1.0 or EF Core 1.0 colloquially.  

Saturday, January 16, 2016

Issues with VS2015.1.exe

Exclusive Amazon Deals
Few days back I downloaded Update 1 of Visual Studio 2015 using VS2015 Extensions. It ran quickly but in the end it reported that .Net Framework 4.6.1 RC had not been installed. On restart and installation, it was successfully installed.
But when I tried to open and run my already created ASP.NET application, I landed up with another issue. Visual Studio 2015 was not able to launch IIS Express. This was not an end.
I noticed another strange thing. Although my installation was successful, Extension's dialog was still showing that Update 1 is available. Now I was left with only one option – uninstall and reinstall Visual Studio 2015.
So, I went to control panel and selected Visual Studio Enterprise. As soon as I clicked on that, a notification popped up:
I clicked on Update and noticed that check box provided for VS 2015 Update 1 was still unchecked in the newly opened window as shown below:


Once update is completed, all my things are in place.

If you don’t want to suffer like me then my recommendation is to do update directly from Control Panel, rather than doing it using VS2015.1.exe. Hope you enjoyed troubleshooting.

Sunday, December 13, 2015

What is this Business Store ?

Setting context
Those who are using Windows 8 or later are already aware about apps available in Windows store. Those apps can be can be installed on Phone, Surface , PC, HoLoLens and other devices having Windows operation system. Few of them are free and remaining ones are paid. One needs an internet connection to get these apps on devices. Now question is, is just by having an internet connection brings an app to our devices? What if you are in some enterprise network? In that case, in spite of having an internet connection, you may not be able to download an app from Windows Store. And the very well-known reason for this is organization’s FIREWALL L.

As you are protected by your enterprise firewall, you may not be able to download the required app from Windows Store. Now question is how to get the required app. To get rid of this shortcoming, Microsoft came up with a new store called Business Store for Windows 10. Those who all have attended Build 2015 may already aware of it J

How to connect to Business Store?
In Windows 10, IT administrators can access a new web-based store portal precisely for enterprises and access the Business Store. In order to connect to this Business Store, you can associate your work identity to store. Hence you don’t need MSA. You can connect that to Azure Active Directory via internet.

Benefit of this new store?
Using this store, employers cannot only customize the apps their employees see but can also privately add internal LoB apps using various distribution options. IT administrators can dispense apps to people in the organization, who will be directed to download via an emailed link. This store provide the same experience as consumer store, but it will accept business payment methods. More details on setting up the account for Business Store can be found here.

Important: As of now, all the apps purchased from Business Store will work only on Windows 10 devices.

Hope you find this brief introduction useful.

Thursday, December 10, 2015

Recent announcement on .Net Fx

In one of the recently made announcements, Microsoft decided to end support for
few of the .Net frameworks .Net 4, 4.5 and 4.5.1 on January 12, 2016. Apart
from these, rest of the frameworks will still be supported till further notice.
If your application is using any of these mentioned versions then you can run
those applications on the later .Net framework because those have compatibility
using quirking. More information of .Net framework migration can be found here.

Saturday, January 3, 2015

MVP award for consecutive 2nd time

I received a very wonderful gift from C# corner on the occasion of New Year :)
Yes, I received yet another MVP award for the consecutive 2nd time. More

Saturday, December 20, 2014

When to use void and Task as return type for Async method

As most of us are aware that there can be 3 return types of the method marked as Async.
  • Task
  • Task<T>
  • void
When any Async method has return type as void, it means not to return anything. In case of synchronous, this is fine. But situations become difficult when it comes to asynchronous operations.

While dealing with asynchronous operations, void behaves like ‘fire and forget’. It means whatever happens, but do not let caller know anything. So, it means, caller will not at all aware about the operation stage, whether it is successful or unsuccessful. As data type is void, method will not even propagate exceptions, which can be very harmful in some cases.

So, always use the word void with caution. If you don’t want your Async method to return anything, then also one should prefer to use return type as Task so that at least exceptions will be propagated to the caller.

Thursday, November 27, 2014

What and Why of CDN

What is CDN?
CDN is short for Content Delivery Network. A CDN in simple terms is a collection of servers that are spread across the globe. In other words, a CDN is a network of servers in which each request will go to the closest server.

Why Do We Need CDN?
For any web application, data can be categorized into either static or dynamic content. Dynamic content is the one which generally comes from a database. Static content is the one like CSS, images, JavaScript, flash files, video files, etc.

Now one may ask, how are requests served when a user enters an URL in the browser? Interesting… let’s take a look at it. Before knowing a CDN and its usage, it is very important to understand this process.

How is a Web Page Served?
When a client requests any URL, let’s say in the browser, there is a server configured to listen to any request that comes in for A server in turn performs some computations and sends raw HTML back to the client.

Raw HTML contains lots of links like CSS, JavaScript, etc. Now for each and every link (CSS, JavaScript, etc.), the client will make more requests to other servers, which means for each and every static resource, the client will make a request to the server. Now one can easily imagine the number of requests sent for a single web page. You can view this by using any of the debugging tools.

In case of a small website, all these requests are served from a single server. So, a single client is having many connections with the same server.

For a server to be serving millions and millions of requests, it may run out of resources. Can’t we optimize something here?

If you will look at the figure shown above, the points marked in red are the intermediate nodes which serve free of cost and nodes at both ends fall in the paid category. We may have very good bandwidth at the first and last node but those intermediate nodes hardly guarantee for any performance or throughput. In essence, the further the user is from our infrastructure, the more he will notice these performance issues.

A CDN has been designed to solve all these shortcomings. There are many CDN providers available in the market today (i.e. Akamai, Interwoven). One of the CDNs that I worked with is KeyCDN.

KeyCDN has points of presence (POPs) all around the world. So, whenever a request for a webpage is submitted by a client, it will go to the closest server available. In this manner, your request doesn’t need to go a long distance plus there won’t be many number of requests between the actual server and the client. KeyCDN also provides various ways to control your settings including delivery status and reporting. It also has a very intuitive dashboard and the most impressive thing is, it is affordable!

A CDN will deliver all the static content. So, actual dynamic content is kept on company’s server and the static content is redirected to CDN provider. So, there are multiple advantages of doing this.

Advantages of using CDN
  • Client request will go to the closest POP to get the static content rather than going all the way to the web server.
  • The actual web server will have more bandwidth available as the CDN takes a lot of load.
  • Failover: In case a server goes down, the CDN will automatically reroute the traffic to the next available server
  • The biggest advantage is caching. Whenever a request comes in for the first time, the content will be cached on an edge server. When the next request comes in, it will read the data directly from its cache.
Enjoy learning!!!