Saturday, March 20, 2010

ASP.NET and Load balancing.

In one of our project the site usage of site was very heavy and we need to migrate it to load balancing server. I have never configured the sites in the load balancing server but it was quite interspersing experience Here are the some points which we need to take care while we move asp.net sites into the load balancing environments. So first we will see what is load balancing.

Following is a load balancing definition from the Google.

In computer networking, load balancing is a technique to distribute workload evenly across two or more computers, network links, CPUs, hard drives, or other resources, in order to get optimal resource utilization, maximize throughput, minimize response time, and avoid overload.

Following are the points which you need to take care when you are deploying your asp.net sites into load balancing server environments.

Machine Key Should be same for both servers: View state and session both are depends on the machine key. If you machine key is not same then you will have problems related to session and view state you may loose your session and view state in between request during post backs. If machine key will not be same then its possible that you can get strange result in Ajax requests. There is a machine key section in web.config where you can specify machine key.

<machineKey validationKey='C44B8B7C521CB5BC7E602BAE6118AA44CD690C7304817129DA27C17E800132A1BD946C6D9AD12F0A5B342840C7D130564195428160B7466146938CA9E3A62686'   decryptionKey='0E9DF2DA7F210B84087690FF0BF25C905182AD81E16A5FA9'   validation='SHA1'/>
Session Configuration: Any web application is not possible without having session as web pages are stateless so you can configure session in load balancing also. There are two configuration which can be used in load balancing environments. One is state service and another is Storing session in SQL Server.

Following is a good link to learn how you can configure sessions state in asp.net application.

http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/0d9dc063-dc1a-46be-8e84-f05dbb402221.mspx?mfr=true

And here is a good link to configure session on SQL Server.

http://support.microsoft.com/kb/317604

You can configure session state mode in your web.config like following.

<sessionState mode="SQLServer" StateConnectionString="tcpip=127.0.0.1:42424"

SqlConnectionString = "data source=SERVERNAME; user id=sa; password=sa"

cookieless="false" timeout="20" />

enableViewStateMac="false": This is a alternative approach to machine key. This will tell asp.net engine that whether it should check Machine authentication check or not and if you made it false then it will not check for machine authentication. You can define that in your web.config pages section like following.
<system.web>
<pages enableViewStateMac="false" />
</system.web>
Caspol Utility: You can use caspol utility to share some resources on between load balancing servers. Like we have file base cache dependency in our application so we have created a central location(A shared folder for cache files) for the both the server and used Caspol utility to give full trust share on that folder for load balancing servers. Following are some of the good links to which will teach how you can use CasPol utility to for asp.net application

http://blogs.msdn.com/shawnfa/archive/2004/12/30/344554.aspx

http://forums.asp.net/p/1119925/1881331.aspx#1881331

http://www.eggheadcafe.com/software/aspnet/30227544/caspol-addfulltrust.aspx

File Replication:File Replication is also an important features of load balancing you should have replication enabled on the folders of web application so if you upload anything on one server it should replicated to other sites. Following is good link to understand file replication.

http://www.tgrmn.com/web/kb/item28.htm

Sticky Sessions:
In some scenario Sticky session is very useful. In one page our application we have used extensive Ajax and we need that for each request and partial post back it should stay on one server till request completes.To achieve that we can used sticky session. Following are some good links to know about sticky sessions.

http://dev.fyicenter.com/Interview-Questions/JavaScript/What_does_the_term_sticky_session_mean_in_a_web_.html

http://blogs.msdn.com/drnick/archive/2007/07/13/sticky-sessions.aspx

Hope this will help you on deploying your asp.net on load balancing sites. Following are some good links for understanding load balancing in more details

http://technet.microsoft.com/en-us/library/bb742455.aspx
http://support.microsoft.com/kb/323437
http://technet.microsoft.com/en-us/library/cc754833%28WS.10%29.aspx.
http://edge.technet.com/Media/Network-Load-Balancing-NLB-in-Windows-Server-2008/

Shout it

kick it on DotNetKicks.com
Share:
Monday, March 15, 2010

Rename feature in Visual Web Developer 2010/Visual Studio 2010

Visual web developer is great tool and I am playing more and more with it and every time I am discovering some new features of it. Recently I have discovered a very cool feature of it. I want to rename a variable in visual studio 2010 Web Developer express edition and I found a great refractor tool for that which will rename that variable in all the instance and all the methods. First you need to select variable which you want to rename and then you need to Right Click ->Refractor->Rename. You can also invoke that via its shortcut Ctrl + R,Ctrl +R and it will be available For reference see the below screenshot.

Rename variable featuers in visual studio 2010

Once you click the rename which will have a dialog box open which will ask for new name or variable like following.

RenameDilaog option in visual web developer 2010

It is also having options for search in comments and search in string. Search in comment will search and replace in comment and search in string will replace word for any string which contains that variable name. Once you put new name in dialog box it will display a preview of replaced strings like following. Here also you can select where you need to rename and where you don’t want to rename and this is really cool. :)

Preview dialog for visual web developer 2010

Shout it
Share:
Friday, February 26, 2010

How to send mail asynchronously in asp.net

With Microsoft.NET Framework 2.0 everything is asynchronous and we can send mail also asynchronously. This features is very useful when you send lots of bulk mails like news letter. You don’t have to wait for response from mail server and you can do other task .

Let's create a simple example to send mail. For sending mail asynchronously you need to create a event handler that will notify that mail successfully sent or some errors occurred during sending mail. Let’s create a mail object and then we will send it asynchronously. You will require System.Net.Mail space to import in your page to send mail.

//you require following name space
using System.Net.Mail;

//creating mail message object
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("Put From mail address here");
mailMessage.To.Add(new MailAddress("Put To Email Address here"));
mailMessage.CC.Add(new MailAddress("Put CC Email Address here"));
mailMessage.Bcc.Add(new MailAddress("Put BCC Email Address here"));
mailMessage.Subject = "Put Email Subject here";
mailMessage.Body = "Put Email Body here ";
mailMessage.IsBodyHtml = true;//to send mail in html or not


SmtpClient smtpClient = new SmtpClient("Put smtp server host here", 25);//portno here
smtpClient.EnableSsl = false; //True or False depends on SSL Require or not
smtpClient.UseDefaultCredentials = true ; //true or false depends on you want to default credentials or not
Object mailState = mailMessage;

//this code adds event handler to notify that mail is sent or not
smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
try
{
smtpClient.SendAsync(mailMessage, mailState);
}
catch (Exception ex)
{
Response.Write(ex.Message);
Response.Write(ex.StackTrace);
}
Following is a code for event handler

void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
MailMessage mailMessage = e.UserState as MailMessage;
if (!e.Cancelled && e.Error != null)
{
Response.Write("Email sent successfully");
}
else
{
Response.Write(e.Error.Message);
Response.Write(e.Error.StackTrace);
}
}
So that’s it you can send mail asynchronously this 30-40 lines of code.

Technorati Tags: ,

Shout it

kick it on DotNetKicks.com
Share:
Sunday, February 21, 2010

How to import your tweets into Google Buzz.

Social networking is a important feature for any person for professionally ass well as personally. You can stay in touch with your friends,colleague and other peoples thanks to social networking sites. Google Buzz is a counter part of twitter from Google. you can post anything on that and you don’t have limit of 140 characters just like. But now there are lots of social networking site available  like Face book,Twitter,Orkut,LinkedIn and now Google Buzz and updating your profile and other things on every social networking site is a tedious job.So, I decided to synchronize my twitter account into Google Buzz. Adding your twitter account in Google Buzz is very easy. First open your Gmail Account and then click on Buzz it will redirect you to your Google Buzz screen. Then click on connected sites and following skin will appear.

Twitter

Click on add it will ask your twitter name just put your twitter name and that’s it you have done with it. Your tweet will appear in Google Buzz depends on traffic from 1 min to 1 hour.

kick it on DotNetKicks.com
Shout it
Share:

Delete all cookies and session in asp.net

Cookies are one of most important features of web application. From where we store little information on client side in one of my project we need to delete all cookies and as you can not delete cookies from server side so we have to expire them manually. Here is the code for that.

 string[] myCookies = Request.Cookies.AllKeys;
foreach (string cookie in myCookies)
{
Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
}
Same way we can delete all session for following code.
Session.Abandon();
Technorati Tags: ,,,

Shout it
kick it on DotNetKicks.com
Share:
Saturday, February 20, 2010

How to remove all the cache object from asp.net application

Cache object in .NET technologies very power full feature and we all use it for increasing out application performance. In one of my project i need to remove all of the my cache once some task was completed. So i have done some digging and i have found some beautiful way of removing all the cache object. As you know cache it self is dictionary entry object so we can always remove them via loop here are some of code examples from which we can remove the all the cache object.

//Following namespace you need.
using System.Collections;
using System;
using System.Collections.Generic;
using System.Web;

IDictionaryEnumerator cacheEnumerator = HttpContext.Current.Cache.GetEnumerator();

while (cacheEnumerator.MoveNext())
{
HttpContext.Current.Cache.Remove(cacheEnumerator.Key.ToString());
}
Here is the another example of removing cache.
foreach (DictionaryEntry dEntry in HttpContext.Current.Cache)
{
HttpContext.Current.Cache.Remove(dEntry.Key.ToString());
}
Please make sure this is way only for the cache object it will not going to remove other cache like output cache etc.



kick it on DotNetKicks.com
Shout it
Share:
Thursday, February 4, 2010

Microsoft Community Techdays at Ahmedabad- A great Event

On 30th January Microsoft,Pass Organization and Ahemdabad .NET User Group organized Microsoft Community Techdays. It was a great event and full of information. There were five session with the full of content. First session was given by Mahesh Dhola on the Windows Azure Platform. In his session he explained all the thing related with windows azure platform from basics like what is windows azure why it is required and what is use of windows azure platform and how its works.

Windows Azure presentation by Mahesh Dhola

Insight of Windows Azure Platform by Mahesh Dhola

The next session was presented by Pinal Dave- A Microsoft Most Valuable Professional and Well know Database Architect for SQL Server. It was about SQL Azure. He has explained what is SQL Azure and all the tools that are needed to connect and maintain database on SQL Azure Server.

PICT0015

SQL Azure: Extending SQL Data Platform to Cloud By Pinal Dave

Then after lunch there was session given by Jadeja Dushyantsinh on Silvelight 4.0. In this session he has explained what is Silvelight, History of Silvelight and what’s new in Silvelight 4.0.The session also include some interesting demo of Silvelight 4.0 Features.

PICT0029

Microsoft Silvelight 4.0- An Overview By Dushyantsinh Jadeja

After this session there was an interesting session by Jacob Sebastian A well know SQL Book writer and Microsoft Most Valuable Professional . He explained SQL Server 2008 R2 data migration features and integration with Visual Studio 2010 by taking an example of love story of Summit- A DBA and Leena-A Developer. It was a great session We have learnt lots of refracting features available in SQL Server 2008 R2 and Visual Studio 2010 Ultimate.

PICT0038

Fall in love with SQL Server 2008 R2 By Jacob Sebastian

Last but not least session given by Prabhjot Bakshi – A well known Microsoft Certified Trainer about Visual Studio 2010 enhancement for Developers. In this session he has explained UML Designer features and lots of new things about Microsoft.NET 4.0 and Microsoft Visual Studio 2010. He has also explained why we need functional programming and some features of F#- A new functional programming language by Microsoft. Indeed it was great i have learnt lot about that in this session.

PICT0049

Visual Studio 2010 enhancement for Developers By Prabhjot Bakshi

PICT0054

Me(First from Left) with Mahesh Dhola,Prabhjot Bakshi and other Ahmedabad Group Members

Overall it was a great event and thank you once again Microsoft for organizing such events.

Shout it
Share:

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews