Migrating 32Bit ASP.NET application to Windows Azure Web Role

If you have a 32bit ASP.NET application and decided to port to Windows Azure application you will do the following:

 

  • 1. Create a Web Role using Windows Azure SDK 1.3
  • 2. Add existing Website (hosted on a 32-bits server) converted to Web Application.
  • 3. Build
  • 4. Test locally in Compute Emulator
  • 5. Deploy it

 

Now it is possible that when your website runs in cloud you might get the following error:

 

Server Error in '/' Application.


is not a valid Win32 application. (Exception from HRESULT: 0x800700C1)

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.BadImageFormatException: is not a valid Win32 application. (Exception from HRESULT: 0x800700C1)Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

 

[BadImageFormatException:  is not a valid Win32 application. (Exception from HRESULT: 0x800700C1)]

   System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection) +0

   System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +416

   System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +166

   System.Reflection.Assembly.Load(String assemblyString) +35

   System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +190

 

[ConfigurationErrorsException:  is not a valid Win32 application. (Exception from HRESULT: 0x800700C1)]

   System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +1149

   System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +332

   System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +119

   System.Web.Configuration.AssemblyInfo.get_AssemblyInternal() +36

   System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +219

   System.Web.Compilation.WebDirectoryBatchCompiler..ctor(VirtualDirectory vdir) +192

   System.Web.Compilation.BuildManager.BatchCompileWebDirectoryInternal(VirtualDirectory vdir, Boolean ignoreErrors) +57

   System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath) +295

   System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile) +482

   System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile) +117

   System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean noAssert) +203

   System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp, Boolean noAssert) +52

   System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) +53

   System.Web.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +428

   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +274

 


Version Information:  Microsoft .NET Framework Version:2.0.50727.4206; ASP.NET Version:2.0.50727.4209

 

 

 

 

The reason for this problem is that the IIS server doesn't have 32bit application support enabled in Application Pool so to fix this error you will need to enable 32 bit applications in the Application pool. And to make it happen, you will need to create a "Startup Task" to enabled 32-Bit application support in IIS and this has to be done in your Windows Azure Application. Here are the detailed steps:

 

1. Create a BATCH file with proper command lines which can run the script to enable 32Bit application support in IIS as below:

File: Enable32BitAppPool.cmd

 

%windir%\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.enable32BitAppOnWin64:true

 

 

 

2. Add this BATCH file into Windows Azure Application Startup Task by adding the following lines in ServiceDefinition.csdef to enable 32-bit application in IIS advance setting

File: ServiceDefinition.csdef

<Startup>

      <Task commandLine="Enable32BitAppPool.cmd" executionContext="elevated" taskType="simple">

      </Task>

</Startup>

 

 

3. Please set the file Enable32BitAppPool.cmd property to "Copy Local" as true so this batch file will be deployed to Azure Portal with your package.