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.
<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>
- 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" />
- 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));
}
- 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.
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();
}
- 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" />
- 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.