Static variables issue in shared hosting or multiple web servers

I just want to share with everyone a lesson learned recently.

Issue: All the static variables which are initialized as class variables are reset to empty, null or 0 depending on the data type of the variable. This happens randomly and cannot be reproduced in the development environment. This issue is specific to web applications and not stand alone/windows apps.

Explanation: Firstly to clarify what I meant by “static variables which are initialized as class variables“, below is an example code snippet.

public class MyUtility
{
static string myURL = “http://somesite.com”;
static int count = 10;


}

In the above code snippet, the variables ‘myURL’ and ‘count’ will be set to empty string and ‘0’ respectively at any time randomly on your production servers if its a load balanced servers and or shared hosting environment. The static variables are initialized properly the first time when the application starts. But whenever the application domain which is the isolated memory in which the web application process runs is reset or recycled all the memory allocated is lost. The application domain reset/recycling is possible in multiple cases. For example:

  • If the web application is not requested for an extended period of time.
  • In shared environment multiple web applications share the limited RAM. Thus, if one web app is idle and doing nothing that will be unloaded and reloaded on demand.
  • The IIS server settings can be set to recycle/reload the application pool periodically or set to recycle whenever the memory allocation exceeds a threshold limit.

Whenever this recycling or reload occurs all the static variables are reset. Not only do these not get reset to their original state but they are also not re-initialised with their design time default values. They will always be given values like null, empty string and zero.  The worst part is that we cannot reproduce this in our development environment as we don’t mimic the production environment.

So the solution is simple, do not use static variables in web applications.

Below is a very good post which mentions more technical details related to this topic.

http://www.navwin.com/Topics/AppDomainRecycling/AppDomainRecycling.aspx

Advertisements

How To: Choose between InfoPath Browser Forms and Custom ASPX Forms

Choosing between InfoPath forms and ASPX pages, depends totally on the nature of the problems we are solving on one side and level of skill set we are investing in solving that problem on the other side. Usually, in most cases, any manager or developer would want to use InfoPath Forms part of Office 2007 because it’s quick and easy. But, as the requirements get more complex and requires more and more customization, one might feel its better to go with ASPX pages and Visual Studio 2005 and SharePoint designer. Technically, InfoPath Forms by itself is incomplete; It needs a container which is again an ASPX web form using the XmlViewer web part. If we go with custom ASPX form, then it has to be designed either using SharePoint designer or Visual Studio leveraging web parts to show the required forms. So for this, developing web parts and its complexity needs to be taken into consideration.

So, to decide on one, we may have to take the decision step by step.

  1. If the requirement is for a single form and showing data from an external data store or from within SharePoint, then going with InfoPath Forms is recommended.
  2. If it’s a single form and needs complex actions, to and fro between the form and the data store, then choosing custom aspx pages is recommended.
  3. If the requirement needs wizard kind of feature i.e. which requires user to go through multiple screens, then custom aspx is the solution.
  4. If the data submitted through the form has to be saved to multiple locations (database, SharePoint, file storage) then InfoPath Forms is a good solution.
  5. If the requirement is for designing numerous forms which are simple enough and well documented, then InfoPath Forms should be considered. InfoPath can help deliver the solution in a short time period.
  6. InfoPath browser based forms has few limitations on the repository of controls one can use. For example: combo box, master/detail view control and many advanced controls are not supported in browser based InfoPath Forms. Depending on these limitations we have to decide on InfoPath or ASPX. ASPX has no such limitations.
  7. InfoPath forms is based on XML. The form is rendered using XSLT stylesheets and the data submitted through the form is available in XML format. The data is well structured to retrieve later on and dump the data/forms in some external system. This in combination with BizTalk server is a perfect architecture for document transition through multiple levels.
  8. InfoPath forms certainly have an edge over ASPX forms as a quick and neat solution. It has some complex out of the box controls, like date picker, file attachment control, repeating sections. InfoPath forms can also have code-behind if needed to manipulate the data on the form or behind the form.
  9. ASPX can have and do everything InfoPath Forms does, with an exception of additional development effort. Apart from it, the deployment process of aspx forms is much better and easier than InfoPath Forms.

Below are some of the excerpts from few blogs I follow which provides some valuable input comparing InfoPath and ASPX forms. Below are the Pros and Cons in using InfoPath Forms:

Pros:

  • Ideal for simple forms
  • Easy to build a form – no coding involved
  • InfoPath form itself is an xml document.
  • Support versioning
  • Works very nicely with SharePoint custom workflows as a workflow association tool.
  • Fully integrated in Microsoft Office 2007 suites like Outlook e-mail messages that can be deployed as Outlook e-mail messages, so colleagues can complete forms without leaving the familiar Outlook environment.
  • Firewall friendly. InfoPath Forms Services make it easy to extend forms solutions beyond firewall because of using many different Web browsers and mobile devices.
  • InfoPath can easily convert Word documents and Excel spreadsheets to forms and build templates to work with.
  • InfoPath provides data integrity and version control for document management purposes, and add structure to information gathering by converting legacy documents to rich InfoPath form templates.
  • Design of a form is much easier in InfoPath forms with a simple drag-and-drop interface and provides support for prebuilt template parts and shared data connections.
  • Creates PDF or XPS and other important document formats and is extensible by addition of a free plug-in to install third party tools and components for document, archival and records management
  • Fully integrated with MOSS 2007 and capable of using integrated workflow management tools in Office 2007
  • Fully Web browsed technology, includes a design checker to help ensure consistency for forms.
  • Support for information rights management to help manage permission control and building a powerful document management team site.
  • Fully centralized for entire organization and enables organizations to centrally

Cons:

  • A web browser-enabled InfoPath form does not support all features of InfoPath client.
  • Inflexible — Difficult or impossible to customize (well, you can argue that you can hack the xsl file of InfoPath)
  • No support for image buttons
  • No support for html
  • No support for tree control
  • Difficult to support Forms Based Authentication
  • By default, InfoPath cannot support SharePoint web services data connections in FBA.
  • Forms Services does not support username() function in FBA. This means that InfoPath form does not recognize the current user.
  • Difficult to perform automated web test against Forms Services.
  • Difficult to support automated deployment. Basically, you have to unzip the xsn file and programmatically modify the manifest.xsf file, and zip back to the xsn file for automated publishing.
  • The way that Forms Services supports deployment of InfoPath forms is quirky – It creates SharePoint solution packages with GUID and creates features with meaningless names on the SharePoint Features folder.

Below are the blog links which talk more about this topic:

http://www.jyhuh.com/blog/archive/2008/03/02/AspNet_vs_InfoPath_Forms_Services.aspx

http://office12.blogspot.com/2007/06/infopath-web-forms-vs-aspx.html

ASP .Net tab missing or SharePoint site leading to Page not found and IIS 32/64-bit modes

Problems:

  • ASP .Net Tab Missing in the IIS Managers Website properties
  • SharePoint site shows “Page cannot be found error”.
  • SharePoint sites shows “You are not authorized to view this page”, “HTTP Error 403 – Forbidden: Access is denied.”
  • Either of the above problems in a server farm.

Possible Solutions:

There could be couple of reasons for the ASP .Net tab missing in the IIS manager. And this issue in turn could be the reason for the SharePoint site throwing the Page cannot be found or You are not authorized to view this page errors. Reasons that could be are:

  1. Improper installation of IIS and ASP .Net framework.
  2. IIS running in 32-bit mode on a 64-bit windows 2003 server.
  3. Having multiple versions of ASP .Net framework lying around on the machine which needs registry cleanup.

If your server is a 64 bit machine, then make sure your IIS is not running in 32-bit mode. For this run,

cscript %SYSTEMDRIVE%\inetpub\adminscripts\adsutil.vbs GET W3SVC/AppPools/Enable32bitAppOnWin64

The output tells if enable32bitapponwin64 is true or false. If its true then, go ahead and run the following to disable 32 bit mode and enable 64 bit mode:

cscript %SYSTEMDRIVE%\inetpub\adminscripts\adsutil.vbs SET W3SVC/AppPools/Enable32bitAppOnWin64 1

If your server is a 32 – bit machine, and your ASP .Net tab is missing then, you might follow the below steps I found in this msdn blog post and KB Article from MicroSoft:

  • Manually run the install for ASP.NET: ASPNET_regiis -i
  • Register the dll for the MMC: regsvr32.exe “C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mmcaspext.dll” “rundll32.exe” “C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\webengine.dll”,RegisterAspNetMMC
  • From a command prompt run: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\regasm.exe C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\AspNetMmcExt.dll /tlb:AspNetMMCExt.tlb
  • Change the About value in “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MMC\SnapIns\{fedb2179-2335-48f1-aa28-5cda35a2 b36d}” from {7D23CCC6-A390-406F-AB67-2F8B7558F6F7} to {7D23CCC6-A390-406F-AB67-2F8B7558F6F6}

If you want to have just the .net framework 2.0 on the machine go cleanup the earlier versions of it in the registry. Run regedit.exe and remove all the versions other than 2.0 below these registry entries:

  • HKEY_CLASSES_ROOT\CLSID\{7D23CCC6-A390-406E-AB67-2F8B7558F6F6}\InprocServer­32
  • HKEY_CLASSES_ROOT\CLSID\{FD5CD8B1-6FE0-44F3-BBFB-65E3655B096E}\InprocServer32
  • HKEY_CLASSES_ROOT\CLSID\{FEDB2179-2335-48F0-AA28-5CDA35A2B36D}\InprocServer­32

Try to unregister the .net framework and re-register it. Depending on your machine if 32/64-bit and the asp .net version (1.1 or 2.0?):

%SYSTEMROOT%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -u //for uninstallation

%SYSTEMROOT%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -i //for installation

%SYSTEMROOT%\Microsoft.NET\Framework64\v2.0.50727\aspnet_regiis.exe -u //for uninstallation

%SYSTEMROOT%\Microsoft.NET\Framework64\v2.0.50727\aspnet_regiis.exe -i //for installation

If doing all the above doesn’t bring back the ASP .Net tab then reboot the machine.

Doing all this should at least get rid of the ‘Page cannot be found‘ error but may lead  to the authorization error.

For the 403 or the authorization error, if you get this error when you access the site using the url like http://portalsite:<port&gt; then try access the site as http://portalsite:<port>/default.aspx. If the later is successful then try to create a new application pool and map it to the corresponding website. Otherwise delete the web application and recreate it from scratch and it should work. This blog post something similar.

To learn more about how to maintain multiple versions of .net framework on the same machine and serve various web applications , read the article from MSDN which talks specifics about the same.