Microsoft Student Partner (MSP) Capacity Build up Training (Session 01)

Microsoft Student Partner (MSP) Capacity Build up Training (Session 01)
Course Title: Object Oriented Programming with C#
Venue: East West University, Dhaka, Bangladesh
Date: 28 May, 2016
Speaker / Trainer: Md. Mahedee Hasan,
Microsoft MVP, Visual Studio and Development Technologies
Software Architect, Leadsoft Bangladesh Limited
Trainer, Leads Technology Limited

ASP.NET Application Performance Tuning and Scalability Improvement (Part – 1)

Every developers and customer wants his/her application performed well. But it is very tricky to improve performance of a web application. It’s actually depends on different parameter like HTML client, HTTP network, Web server, middle-tier components, database components, resource-management components, TCP/IP networks, and database servers. Sometimes performance of the application increases drastically, if you change a single parameter. Sometimes you have to change multiple parameters. Here are the some performance optimization tips for ASP.NET web applications which may help you to increase performance of ASP.NET Application.

Prepare release with release mode
When prepare release for deployment, select release mode instead of debug mode.

How affect in performance?

  • If you choose debug mode instead of release mode, you are creating pdb (program database – uses for debugging) files which creates extra overhead. For this reason you may face timeout problem.

Best Practice

  • Always prepare release in release mode.

Release mode

In Web.Config, Set debug=”false”
Set debug=”false” in web.config as follows after deploying your application. By default debug=”true” when you create a web application. It is necessary to set debug=”true” in development environment.


How affect in performance?

  • If you set debug = “true”, application requires the pdb information to be inserted into the file and that results in a comparatively larger file and hence processing will be slow.

Best Practice

  • In deployment server, set debug = “false” in web.config

Turn off Tracing unless until required
Sometimes developers need to trace the application to monitor the executions of application or a pages. It requires for application diagnostic purposes.

How affect in performance?

  • When trace is enabled it loaded extra information to the pages which degrades performances.

Best Practice

  • Always set trace enabled = “false” unless or until you required to monitor a page’s executions. Set trace enable = “false” as follows in web.config.


Carefully manage session state
Session state is a very useful feature of Though, ASP.NET manages session state by default, we must pay attention of session memory management.

  • How affect in performance?
  • When you store your data in in-process or on a state server or in a SQL Database, session state, it requires memory.
  • It is also time consuming when you store or retrieve data in-process or state server or SQL server.

Best Practice

  • If your page is static, it is recommended not to use session state. In such cases where you don’t need to use session state, disable it on your web form using the following directive:
    <@%Page EnableSessionState="false"%>
  • In case you use the session state only to retrieve data and not to update, make the session state read-only using the following directive.
    <@%Page EnableSessionState ="ReadOnly"%>
  • If your application session state is out of process then consider carefully whether there is a need of the state server or SQL Server mode.
  • SQL Server session mode provides lower performance than state server mode.
  • Try to avoid keeping object in session. Since it requires serializing then de-serializing which affected in performance.
  • Use client-side state management than server side.

Disable View State of a page if not required

  • It stores data in the generated HTML using hidden field not on the server.
  • View State provides page level state management
  • As long as the user is on the current page, state is available and the user redirects to the next page and the current page state is lost
  • View State can store any type of data because it is object type but it is preferable not to store a complex type of data due to the need for serialization and deserialization on each post back

How affect in performance?

  • It increases the total payload of a page when submitted and when serving request.
  • Serialization and deserialization of the data is required when submitting data and gets requested data.
  • View state increases the memory allocations on the server.

Best Practice

  • Pages that do not have any server postback events can have the view state turned off.
  • The default behaviour of the View State property is enabled, but if you don’t need it, you can turn it off at the control or page level.
  • Within a control, simply set the EnableViewState property to false, or set it globally within the page using this setting.

    <%@ Page EnableViewState="false" %>

Use finally block to release resources
We always uses try, catch and finally block for exception handling. Finally block executes whether any exception occurs or not.
How affect in performance?

  • Sometimes application occupy resources where as it doesn’t need it. It is occur due to bad programming.

Best Practice

  • Always use a finally block to release resources like closing database connections, closing files, disposing objects and other resources.

Avoid unnecessary round trips to the server

How affect in performance?

  • Unnecessary round trips significantly effect on web application performance.
  • It increases network latency and downstream server latency.
  • Many data-driven Web sites heavily access the database for every user request. While connection pooling helps, the increased network traffic and processing load on the database server can adversely affect performance.

Best Practice

  • Keep round trips as minimum as possible
  • Use Ajax or partial page load instead of full page reload or refresh.

Choose low cost authentication
Authentication is a main factor for a secured applications. You must take decision which authentication will you use? Keep in mind, passport authentication is slower than form-base authentication which is slower than Windows authentication.

Use paging in grid view
In web application to show data in tabular format, generally we use grid view. Besides this we also uses DataGrid, JQgrid, Telerik grid, Kendo Grid etc. For huge number of data we cannot think general way because it takes huge time to load.

Best Practice

  • To load grid view faster take advantages of paging, it shows small subsets of data at a time. JQGrid is faster than grid view because it does everything in client side.

Minimizes number of web server control

How affect in performance?

  • The uses of web server controls increase the response time.
  • Web server controls go to the server executes all of its life cycle and then rendered on the client side.

Best Practice

  • Don’t use server control unless until required. Use HTML elements where suited.

Multiple DbContext in a single mvc applications with EF code first

Is it possible to create multiple DbContext in a single application with entity framework code first? Yes! Then, How!! This was the questions to me for a couple of weeks. Here is the simple article where you will get all answer. First, I want to say, yes it is possible to create multiple DbContext in a single application with entity framework code first.

Let’s see how to create multiple DbContext

Tools and Technology used
I used following tools and technology for my project –

  1. Visual Studio 2013
  2. Visual C#
  3. ASP.NET MVC 5
  4. Entity Framework 6

Here, I have an application name PMTool with two DbContext name PMToolContext and SecurityContext.

Step 1: Enable Migration
If you run only Enable-Migrations in package manager console, you will get following error.

PM> Enable-Migrations
More than one context type was found in the assembly 'PMTool'.
To enable migrations for 'PMTool.Repository.SecurityContext', use Enable-Migrations -ContextTypeName PMTool.Repository.SecurityContext.
To enable migrations for 'PMTool.Repository.PMToolContext', use Enable-Migrations -ContextTypeName PMTool.Repository.PMToolContext.

So you have to run enable-migration for each context. You have to specify the directory for each Configuration.cs also. Now run the command in package manager console in the following way.

PM> Enable-Migrations -ContextTypeName PMToolContext -MigrationsDirectory Migrations\PMToolContext
Checking if the context targets an existing database...
Code First Migrations enabled for project PMTool.
PM> Enable-Migrations -ContextTypeName SecurityContext -MigrationsDirectory Migrations\SecurityContext
Checking if the context targets an existing database...
Code First Migrations enabled for project PMTool.

Step 2: Add Migration
Use fully qualified name of the configuration class as follows.

PM> Add-Migration -ConfigurationTypeName PMTool.Migrations.PMToolContext.Configuration FirstMigForPMTool
Scaffolding migration 'FirstMigForPMTool'.

PM> Add-Migration -ConfigurationTypeName PMTool.Migrations.SecurityContext.Configuration FirstMigForSecurity
Scaffolding migration 'FirstMigForSecurity'.

Step 3: Update database

Now update the database. Use fully qualified name of configuration class here also.

PM> Update-Database -ConfigurationTypeName PMTool.Migrations.PMToolContext.Configuration
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201512071619030_FirstMigForPMTool].
Applying explicit migration: 201512071619030_FirstMigForPMTool.
Running Seed method.
PM> Update-Database -ConfigurationTypeName PMTool.Migrations.SecurityContext.Configuration
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201512071627058_FirstMigForSecurity].
Applying explicit migration: 201512071627058_FirstMigForSecurity.
Running Seed method.

If each commands run individually, means you could use multiple DbContext in an web application with Entity framework. Let’s cheer!!

bobj is undefined

Problem Statement
I have an application developed with the following tools and technologies.

  • MS .NET Framework 4.5
  • SAP Crystal Report 13.5
  • ASP.NET Web form
  • SQL Server 2008 R2

In deployment server the report is not displayed. Instead I found a javascript error “bobj is undefined”. But it works fine in development server.


  • Copy aspnet_client folder (From a running project) from c:\inetpub\wwwroot folder to the new website root folder. For example: c:\inetpub\wwwroot
  • Create a virtual directory called aspnet_client that points to c:\inetpub\wwwroot\aspnet_client inside the new website
  • If this is a 32 bit application running on a 64 bit OS, make sure the application pool is set to 32 bit
  • Set application pool to run under Local System Account
  • Change application pool to Classic mode.

How to register .NET framework in IIS

If you install IIS after installing framework, you have to register framework in IIS. Otherwise you will get following message after hosting application in IIS.

ASP.NET 4.5 has not been registered on the Web server. You need to manually configure your Web server for ASP.NET 4.5 in order for your site to run correctly.

You may get error of different version instead of 4.5 according to your configuration. So to register framework in IIS, run the following command in command window.

  • Register framework to IIS without changing existing web applications to use this version of

C:\windows\Microsoft.NET\Framework\v4.0.30319->aspnet_regiis –ir

  • Or Installing framework to IIS

C:\windows\Microsoft.NET\Framework\v4.0.30319->aspnet_regiis –i

This installed ASP.NET version v4.0.30319 without updating all script maps. After installing Rational

System Architect XT, the following command was run:

C:\windows\Microsoft.NET\Framework\v4.0.30319->aspnet_regiis -s W3SVC/1/ROOT/SAXT

This installed ASP.NET version v4.0.30319 at the specified application root and its subfolders. All existing script maps in the specified path and below were updated.

For the Rational System Architect XT Web Service add-on product, the Registration tool was run with the following command:

C:\windows\Microsoft.NET\Framework\v4.0.30319->aspnet_regiis -sW3SVC/1/ROOT/SAXTWebService

How to configure IIS for different port

1.      Open IIS Manager (run -> inetmgr)
2.      In the Connections pane, expand the Sites node in the tree, and then click to select the site for which you want to add/edit binding.
3.      Right click on the site and click Edit bindings
4.      Select Port on the site binding pane
5.      Click Edit button
6.      In Edit Site Binding pane type port (ex. 1080) and click Ok
IIS Port change

Call JavaScript from code behind

Suppose you want to display confirmation message by javascript from code behind after saving data. Write the following code in your button event.

protected void btnSaved_Click(object sender, EventArgs e)
         string msg = "Data saved sucessfully"; 
         Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "