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.
- Create a new cloud service project and add a web role.
- 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.
- Add a FileUpload control and an Upload button to Default.aspx.
- 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.
- Add a reference to %System32%\inetsrv\Microsoft.Web.Administration.dll and set CopyLocal=True.
- Add the following code to the OnStart routine in WebRole.cs. This code configures the Website to point to the AspNetTemp1GB LocalStorage resource.
- 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.
- 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 http://msdn.microsoft.com/en-us/magazine/cc163496.aspx.