Partager via


How to increase the size of the Windows Azure Web Role ASP.NET Temporary Folder

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.’. 

In order to change the size of the temp folder you need to create a new folder by defining a LocalStorage resource in your service definition file, then on role startup modify the website configuration to point the system.web/Compilation tempDirectory property to point to this folder.

  1. Create a new cloud service project and add a web role.

  2. In the ServiceDefinition.csdef create 2 LocalStorage resources 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. One LocalStorage resource will be for the AspNetTemp folder and one will be used to store the file uploaded by the user.

     <WebRole name="WebRole1" >
       <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" />
         <LocalStorage name="FileUploadFolder" sizeInMB="1000" />
       </LocalResources>
     </WebRole>
    
  3. Add a FileUpload control and an Upload button to Default.aspx.

     <asp:FileUpload ID="FileUpload1" runat="server" />
     <asp:Button ID="Button1" Text="Upload" runat="server" OnClick="Button1_OnClick" />
    
  4. In Default.aspx.cs add the code for the Upload button’s OnClick event handler. This code will simply store the file uploaded into the FileUploadFolder LocalStorage resource.

     protected void Button1_OnClick(object sender, EventArgs e)
     {
         Microsoft.WindowsAzure.ServiceRuntime.LocalResource FileUploadFolder = Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetLocalResource("FileUploadFolder");
         FileUpload1.SaveAs(System.IO.Path.Combine(FileUploadFolder.RootPath, FileUpload1.FileName));
     }
    
  5. Add a reference to %System32%\inetsrv\Microsoft.Web.Administration.dll and set CopyLocal=True.

  6. 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()
     {
         // 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();
     }
    
  7. Modify the web.config to increase the size of the allowed requests so that you can upload large files. Edit the configuration/system.webServer/security/requestFiltering/requestLimites to increase the maxAllowedContentLength, and the configuration/system.web/httpRuntime to increase the maxRequestLength.

     <system.webServer>
       <security>
         <requestFiltering>
           <!-- maxAllowedContentLength = 1 GB -->
           <requestLimits maxAllowedContentLength="1073741824" />
         </requestFiltering>
       </security>
     </system.webServer>
    
     <!-- maxRequestLength = 1 GB -->
     <httpRuntime maxRequestLength="1048576" />
    
  8. Deploy your service and you are now able to upload a file larger than 100 MB.

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

Comments

  • Anonymous
    December 09, 2012
    Is it necessary to TEMP folder after creating small tmp files (Path.GetTempFileName()) for a while?

  • Anonymous
    December 09, 2012
    Joey, can you clarify your question?  If you create lots of small temp files in the TEMP folder, then yes, eventually you will run out of space.  But for normal temp files that you are creating in code, you should really be using a LocalResource storage location rather than the TEMP folder.

  • Anonymous
    June 20, 2013
    thanks for your great article about the 100mb limitation! it really helped me with my data intensive azure worker role problem :)

  • Anonymous
    July 14, 2014
    Am using windows azure custom mobile service, how can i increase the size

  • Anonymous
    July 18, 2015
    I am running a php application azure,currently i cannot upload files above 25 MB Size its getting timedout,i have changed all required settings to higher  using .user.ini file,the changes are showing in phpinfo page as well still no sucess upload_max_filesize = 950M post_max_size = 900M max_execution_time = 1200 max_input_time = 1200 memory_limit = 512M max_file_uploads = 100 these are my values i am using,is the issue is related to any with the temporary folder,i even tried to change upload_tmp_dir value to another path but  it didn't worked out,please help me

  • Anonymous
    September 02, 2015
    anoop try reading this article.  it worked for me in azure with php file uploads. robertgreiner.com/.../404-error-when-uploading-large-files-in-windows-azure