同时写入两个azure实例的角色环境

时间:2014-07-23 09:05:46

标签: azure io high-availability azure-cloud-services azure-role-environment

我在Azure上托管了一个MVC Web应用程序作为云服务,该文件在角色环境中本地存储文件。这是一个可行的解决方案,直到将服务移动到具有两个实例的高可用性,但现在文件仅写入上载它们的环境,因此如果用户由于负载平衡器而跳转到另一个实例,则这些文件将变得不可用。

如果我登录到实例[0],是否有任何写入实例[1]的其他角色环境的方法。会话使用Redis缓存跨两个实例存储。目前我写入角色环境的代码如下:

public static string ImportContentPackage(string iEnVar, string fileName, HttpPostedFileBase zipFile, string companyName)
{
  //Get the Local Storage place.
  LocalResource tempDirectory = RoleEnvironment.GetLocalResource("TempZipDirectory");

  //Now System.IO to store the Zip file in there. Get the file name without the extension.
  string actualFileName = fileName.Split('.')[0];

  System.IO.Directory.CreateDirectory(tempDirectory.RootPath + "\\" + companyName + "\\" + actualFileName); 

  string holderDir = tempDirectory.RootPath + @"\" + companyName + @"\" + actualFileName;

  try
  {
    //Clear out any directories that might be there.
    CleanImportDirectory(holderDir);

    //Save the zip into the package to enable unzipping.
    BinaryReader br = new BinaryReader(zipFile.InputStream);
    File.WriteAllBytes(tempDirectory.RootPath + "\\" + fileName, br.ReadBytes(zipFile.ContentLength));


    // Unzip the content package into a local directory for processing
    SCORM.Validation.Handlers.UnZipFile uzh = new SCORM.Validation.Handlers.UnZipFile(tempDirectory.RootPath + "\\" + fileName, holderDir);

    SCORM.Validation.Helpers.Result result = uzh.Extract();

    if(result.PackageCheckerMessages.Count > 0)
    {
      holderDir = "";
    }
  }
  catch (System.NullReferenceException npe)
  {
  }
  return holderDir;
}

LocalResource tempDirecory变量在两个实例上都应该相同,因为它们都被命名为TempZipDirectory。