Host Asp.Net Application in Web Farms
Written on 9:12 PM by Mj blog
Web Farms?
A web farm is a group or collection of servers which act as a single server and housed in single location. A Web farm can also be called as a server cluster.
Why Web Farms ?
It is used to increase the scalability, performance of the website when the load is more. The Major advantage is the availability (less downtime) of your web application. If one server in the Web farm is crashed or disabled , other servers in the Web farm take over, so users are never aware of the offline server.
ASP.net Application in Web Farms
Points to Remember
ASP.net uses viewstate which has a encryption along with machine key of the server. In web farm you don’t know which machine is going to return you the requested page (provided the Affinity is set to single no problem).
Scenario : when you request a Home page and it come to the browser with a viewstate from server A and when you select some dropdownlist and that has a autopostback the request might go to server B , the viewstate encryption does not match and you will see the “Yellow Screen of Death”
Let’s see the various approaches to host a ASP.net Applicaiton in Web Farm
Approach 1:
Disable the EnableStateMac, EnableViewStateMac indicates that ASP.NET should run a machine authentication check (MAC) on the page’s view state when the page is posted back from the client;
1: <system.web>
2: <pages enableViewStateMac=”false” />
3: </system.web>
True - if view state should be MAC checked
False - We need to ensure that it is kept to false
You can do this in web.config else in Machine.config in c:\windows\Microsoft.net\Framework\v2.*\config folder
Approach 2:
The Next option is provide the unique Machine key to all the servers in the web farms you can find the
<Machine Key> tag in the Machine.config file
1: <machineKey
2: validationKey=‘A130E240DF1C49E2764EF8A86CEDCBB11274E5298A130CA08B90EED016C0
3: 14CEAE1D86344C29E67E99DF83347E43820050A2B9C9FC89E0574BF3394B6D0401A9′
4: decryptionKey=’2CC37FFA8D14925B9CBCC0E3B1506F35066FEF33FEB4ADC8′
5: validation=‘SHA1′/>
If all the machine’s where using same machine key there is no need to disable the viewstate
Approach 3:
Maintaining session is difficult since the request can go to any machines, hence we can use the ASP.net State Server, but remember you should have the unique machinekey throughtout all the servers in the web farm
Open the web.config of the ASP.net Application and add the following syntax
1: <configuration>
2: <sessionstate
3: mode=”stateserver”
4: cookieless=”false”
5: timeout=”20”
6: sqlconnectionstring=”data source=127.0.0.1;user id=userid;password=password”
7: server=”127.0.0.1”
8: port=”42424“/>
9: </configuration>
Approach 4:
The Next Step is maintaining Session State using SQL Server.
Open the %windir%\Microsoft.NET\Framework\<versionNumber>\
You will find two SQL Scripts
InstallSqlState.sql
UninstallSqlState.sql
Run the InstallSqlState Script and you will have a new database and tables all stuff in the database and if you feel to remove it you can use the UninstallSqlState.sql scripts
Open the web.config of the ASP.net Application and add the following syntax
1: <configuration>
2: <sessionstate
3: mode=”sqlserver”
4: cookieless=”false”
5: timeout=”20”
6: sqlconnectionstring=”data source=127.0.0.1;user id=userid;password=password”
7: server=”127.0.0.1”
8: port=”42424“/>
9: </configuration>
So when every you hit a request all the sessions are maintained in SQL Server
Thus you can host a ASP.net Application in Web Farms