云服务 Web 角色/辅助角色的默认 TEMP 文件夹大小太小Default TEMP folder size is too small on a cloud service web/worker role

云服务辅助角色或 Web 角色的默认临时目录的最大大小为 100 MB,该目录可能会在某个时候被填满。The default temporary directory of a cloud service worker or web role has a maximum size of 100 MB, which may become full at some point. 本文介绍如何避免临时目录空间不足的问题。This article describes how to avoid running out of space for the temporary directory.

如果本文未解决你的 Azure 问题,请访问 MSDN 和 CSDN 上的 Azure 论坛。If your Azure issue is not addressed in this article, visit the Azure forums on MSDN and CSDN. 可以在这些论坛上发布问题。You can post your issue in these forums. 还可提交 Azure 支持请求。You also can submit an Azure support request. 若要提交支持请求,请在 Azure 支持页上提交。To submit a support request, on the Azure support page.

为什么空间会不足?Why do I run out of space?

标准 Windows 环境变量 TEMP 和 TMP 可供应用程序中运行的代码使用。The standard Windows environment variables TEMP and TMP are available to code that is running in your application. TEMP 和 TMP 都指向一个最大大小为 100 MB 的目录。Both TEMP and TMP point to a single directory that has a maximum size of 100 MB. 此目录中存储的任何数据都不会在云服务的整个生命周期中持久保存;如果云服务中的角色实例被回收,则会清除此目录。Any data that is stored in this directory is not persisted across the lifecycle of the cloud service; if the role instances in a cloud service are recycled, the directory is cleaned.

解决此问题的建议Suggestion to fix the problem

实现以下备选方案之一:Implement one of the following alternatives:

  • 配置本地存储资源,直接对其进行访问而不使用 TEMP 或 TMP。Configure a local storage resource, and access it directly instead of using TEMP or TMP. 若要通过应用程序内运行的代码访问本地存储资源,请调用 RoleEnvironment.GetLocalResource 方法。To access a local storage resource from code that is running within your application, call the RoleEnvironment.GetLocalResource method.
  • 配置本地存储资源,将 TEMP 和 TMP 目录指向本地存储资源的路径。Configure a local storage resource, and point the TEMP and TMP directories to point to the path of the local storage resource. 应在 RoleEntryPoint.OnStart 方法中进行这种修改。This modification should be performed within the RoleEntryPoint.OnStart method.

下面的代码示例演示了如何在 OnStart 方法中修改 TEMP 和 TMP 的目标目录:The following code example shows how to modify the target directories for TEMP and TMP from within the OnStart method:

using System;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace WorkerRole1
{
    public class WorkerRole : RoleEntryPoint
    {
        public override bool OnStart()
        {
            // The local resource declaration must have been added to the
            // service definition file for the role named WorkerRole1:
            //
            // <LocalResources>
            //    <LocalStorage name="CustomTempLocalStore"
            //                  cleanOnRoleRecycle="false"
            //                  sizeInMB="1024" />
            // </LocalResources>

            string customTempLocalResourcePath =
            RoleEnvironment.GetLocalResource("CustomTempLocalStore").RootPath;
            Environment.SetEnvironmentVariable("TMP", customTempLocalResourcePath);
            Environment.SetEnvironmentVariable("TEMP", customTempLocalResourcePath);

            // The rest of your startup code goes here…

            return base.OnStart();
        }
    }
}

后续步骤Next steps

查看更多针对云服务的 故障排除文章View more troubleshooting articles for cloud services.