Homepage image
image RSS Feed

Sample Download: http://code.msdn.microsoft.com/CSAzureIncreaseTempFolderSi-d58c604d

By default the ASP.NET temp folder size in a Windows Azure web role is limited to 100 MB. This is sufficient for the majority of applications, but some applications may require more storage space for temp files. The sample demonstrates how to increase the ASP.NET temp folder size.

The sample was written by Microsoft Escalation Engineer Narahari Dogiparthi.

imageYou can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage http://1code.codeplex.com/.

 

Introduction

By default the ASP.NET temporary folder size in a Windows Azure web role is limited to 100 MB. This is sufficient for the vast majority of applications, but some applications may require more storage space for temporary files. In particular this will happen for very large applications which generate a lot of dynamically generated code, or applications which use controls that make use of the temporary folder such as the standard FileUpload control. If you are encountering the problem of running out of temporary folder space you will get error messages such as OutOfMemoryException or ‘There is not enough space on the disk.’.

 

Building the Sample

This sample can be run as-is without making any changes to it.

 

Running the Sample

  1. Open the sample on the machine where VS 2010, Windows Azure SDK 1.6 are installed.
  2. Right click on the cloud service project i.e. CSAzureIncreaseTempFolderSize and choose Publish.
  3. Follow the steps in publish Wizard and choose subscription details, deployment slots, etc. and enable remote desktop for all roles.
  4. After successful publish, login to Windows Azure VM via RDP and verify that IIS is using newly created AspNetTemp1GB for storing temporary files instead of default temporary ASP.net folder.

 

Using the Code

1) In the ServiceDefinition.csdef create one LocalStorage resource in the Web Role, and set the Runtime executionContext to elevated.  The elevated executionContext allows us to use the ServerManager class to modify the IIS configuration during role startup.  

<WebRole name="IncreaseAspnetTempFolderSize" vmsize="Small"> 
  <Runtime executionContext="elevated" /> 
  <Sites> 
    <Site name="Web"> 
      <Bindings> 
        <Binding name="Endpoint1" endpointName="Endpoint1" /> 
      </Bindings> 
    </Site> 
  </Sites> 
  <Endpoints> 
    <InputEndpoint name="Endpoint1" protocol="http" port="80" /> 
  </Endpoints> 
  <LocalResources> 
    <LocalStorage name="AspNetTemp1GB" sizeInMB="1000" /> 
  </LocalResources> 
  
  <Imports> 
    <Import moduleName="Diagnostics" /> 
  </Imports> 
</WebRole> 

2) Add reference to Microsoft.Web.Administration (location: <systemdrive>\system32\inetsrv) assembly and add below using statement to your project

using Microsoft.Web.Administration;

3) Add the following code to the OnStart routine in WebRole.cs.  This code configures the Website to point to the AspNetTemp1GB LocalStorage resource.

public override bool OnStart() 
{ 
    // For information on handling configuration changes 
    // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357. 
     
    // Get the location of the AspNetTemp1GB resource      
    Microsoft.WindowsAzure.ServiceRuntime.LocalResource aspNetTempFolder =  
        Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetLocalResource("AspNetTemp1GB");    
     
    //Instantiate the IIS ServerManager      
    ServerManager iisManager = new ServerManager();      
  
    // Get the website.  Note that "_Web" is the name of the site in the ServiceDefinition.csdef,  
    // so make sure you change this code if you change the site name in the .csdef      
    Application app = iisManager.Sites[RoleEnvironment.CurrentRoleInstance.Id + "_Web"].Applications[0];     
  
    // Get the web.config for the site      
    Configuration webHostConfig = app.GetWebConfiguration();      
  
    // Get a reference to the system.web/compilation element      
    ConfigurationSection compilationConfiguration = webHostConfig.GetSection("system.web/compilation");   
    
    // Set the tempDirectory property to the AspNetTemp1GB folder      
    compilationConfiguration.Attributes["tempDirectory"].Value = aspNetTempFolder.RootPath;    
   
    // Commit the changes      
    iisManager.CommitChanges(); 
  
    return base.OnStart(); 
} 

 

More Information

For more information about the ASP.NET Temporary Folder see http://msdn.microsoft.com/en-us/magazine/cc163496.aspx