Wednesday, 4 September 2013

Async and Await in .Net Framework 4.5

Asynchronous processes can improve the performance of application and make your application more responsive. It can be difficult to debug and maintain the asynchronous application.

But the latest .Net framework (4.5) provided Asyc & Await keyword which can make it easy to implement, debug & maintain asynchronous application with minimal required efforts.

Difference between Synchronous and Asynchronous Methods

Asynchronous processes does not depends on each other's output and they do run on different threads simultaneously, while synchronous processes are opposite kind of thing which wait for the previous process to be complete before starting the next process.



When Asynchronous should use

Asynchronous is essentially to use in application where there are chances for process to be blocked for such operation to be complete. In the case where your application access resources from web server and sometimes server response are slow or delayed, it increases the possibility of application to get blocked. In synchronous application when server respond is weak and its taking time then whole application has to wait for the respond and at that time user might think that application is blocked and he/she try to rerun it.

For same scenario in asynchronous application, the application can continue with other process which doesn't depend on the web resource until the blocking tasks get finished.

It is more effective to use Asynchronous methods in UI related application, because all UI related process share one thread and due to that single UI related process block can make whole application paralyze.

In this case when you use asynchronous methods, the application continues to respond UI related process. You can close the application if you don't want to wait for it to finish.

How to write Asynchronous method

The Async and Await keywords in Visual Basic and the async and await keywords used in C#  async programming. By using those two keywords, you can use resources in the .NET Framework. This method is as easily as you create synchronous method. To refer asynchronous method you just have to define it by using async and await.

Get Nuget Package for Asynchronous programming feature in .Net Framework


Initially Microsoft has released asynchronous programming feature for .Net framework 4.5 only. And recently they have provided Nuget package for .Net Framework 4.0 as well.

Run the following command in the Package Manager Console to install Async for .NET Framework 4, Silverlight 4 and 5, and Windows Phone 7.5 and 8,.

Install-Package Microsoft.Bcl.Async

This package is not supported in Visual Studio 2010 or below.

Monday, 5 August 2013

Cascading DropdownList feature in MVC4 and Razor

Models which are used in our example.
A) Subjects
B) Students

A controller method for filter is as shown below.
Here it will take the selected value of Subjects .DropdownList as argument and Return filtered in Json format.

On the other side in View page I have something like below.

Parent DropdownList:

Child DropdownList:

Jquery Call  to get Filer Data and bind Child DropdownList
 
Below are the Pron and Cons of the approach.  

Pron:
A) Because of Ajax, it won’t load whole page instead it will load that particular DropdownList only.

B) Light weight.  

Cons:
A) Bit complex than normal binding of DropdownList.

B) We cant have intelligence for the value return in Jason format.

Friday, 22 February 2013

.NET Framework

The .NET Framework is a Microsoft platform for building, deploying, developing application with rich UI and running in a Microsoft environment. The Microsoft .NET Framework is a managed code programming model for building applications on Windows clients, servers, and mobile or embedded devices. You can use the .NET Framework to develop the following types of applications and services:
  • Console applications.
  • Windows GUI applications (Windows Forms).
  • ASP.NET applications (Web Application).
  • XML Web services.
  • WCF (Windows Communication Foundation).
  • WPF (Windows Presentation Foundation).
  • WF (Windows Workflow Foundation).
  • Silverlight.
  • Database Application.
  • Mobile Application.
  • Windows Services.
  

.NET Framework Application Execution

Execution Steps:
1. Programmer can write application code into any programming language like c#, VB.NET or J# (around 40 languages are provided by .net framework).
 

2. Each language has its own compiler Ex. C# has its own c# compiler, so code written in c# will be compiled by c# compiler similarly VB.NET code will be compiled by VB.NET complier. Single application can be coded into multiple languages and will be compiled by respective compiler. The compiled code will be provided to Intermediate language which is called as MSIL (Microsoft Intermediate Language).
 

3. MSIL is responsible for converting different compiled code into “Byte Code”
 

4. The “Byte Code” converted by MSIL will be given to CLR to convert the “Byte Code”  into “Native Code” -- The native code is machine readable code which will be compiled by “Just in time compiler” into executable code.
 

5. JIT -- Is responsible for compiling the code into CPU executable code. There are mainly three types of JIT which are listed below
    a. Econo-JIT
-- It compiles only those methods which are called at run time. These compiled methods are removed when they are not required. It mainly takes less compile time for startup.
    b. Pre-JIT - Compiles the complete source code into the native code in a single completion cycle. It is done at the time of deployment
    c. Normal-JIT -- It compiles only those methods which are called at run time, these methods are only compiled once when they are called and are stored into cache.


Base Class Library

Base Class is a part of .NET framework library which provides facility to implement common functionalities into the application like drawing tool, reading and writing file and so many. For example you need to add “System.Data” library to perform ADO.NET operations. BCL is a collection for reusable types that tightly integrate with the common language runtime.

BCL provides built in CLI data types, collection, formatting, security attributes, I/O stream and string manipulation.

Common Language Runtime

The common language runtime (CLR) is responsible for converting human language code written in different programming languages like c#, VB.NET and J# (many more supported by .net framework) into machine executable code with the help of Just in time complier (JIT) which is part of CLR. CLR provides various services, like memory management, type safety, thread management & exception handling. It works as a layer between OS (Microsoft) and application written into .NET.

Memory Management: To execute any .NET program memory management plays a very important role.  Programmer did not need to worry about memory allocation / freeing up for their application objects. CLR – provided facility to automatically allocate application object into memory for the operation to be performed same way when objects are no longer in use CLR using Garbage Collector releases such unused memory which can be reallocated for other program to allocate and execute. Garbage Collector keeps on running recursively to check whether the assigned object on heap are still encapsulated (enclosed) by other object using CLI (Common Language Infrastructure) which is a part of .NET framework.

Exception Handling: CLR will handle any “Run-Time” exception that occurs during the execution of the application. CLR stores the current state of the execution in predefined place and switch the execution to a specific place which is known as exception handler. Handler is responsible to resume the execution at the original location if any exception occurs and jump to appropriate subroutine.


Microsoft .NET Framework Details
Features
.NET Framework Versions

1.0/1.1
2.0
3.0
3.5
3.5 SP 1
4.0
4.5
Released Year
2002-03
2005
2006
2008
2009
2010
2011
C#.Net
Yes
Yes
Yes
Yes
Yes
Yes
Yes
VB.Net
Yes
Yes
Yes
Yes
Yes
Yes
Yes
ASP.NET
Yes
Yes
Yes
Yes
Yes
Yes
Yes
ADO.NET
1.0
2.0
2.0
3.5
3.5
4.0
4.0
Web Service
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Visual Studio
2002/2003
2005
2005
2008
2008
2010
2012
CLR
1.0/1.1
2.0
2.0
2.0
2.0
4.0
4.0
ASP.NET Ajax
No
Yes
Yes
Yes
Yes
Yes
Yes
WPF / WCF /WF
No
No
Yes
Yes
Yes
Yes
Yes
LINQ
No
No
No
Yes
Yes
Yes
Yes
Silverlight
No
No
No
No
Yes
Yes
3.0
ASP.NET MVC
No
No
No
No
Yes
Yes
4.0
F# .NET
No
No
No
No
No
Yes
Yes
Entity Framework
No
No
No
No
Yes
Yes
Yes

Acronyms

WPF - Windows Communication Foundation
WCF - Windows Presentation Foundation
WF - Windows Workflow Foundation
CLR - Common Language Runtime
BCL - Base Class Library
CLI - Common Language Infrastructure
LINQ - Language-Integrated Query
XML - Extensible Markup Language
MVC - Model View Controller
GC - Garbage Collector