public byte[] GetStream(string filename)
        {
            RbAzureStorage rbAzureStorage = new RbAzureStorage(_accountName, _accountKey);
            Stream         stream         = rbAzureStorage.BlockBlobOpenRead(_container, filename);
            long           fileSize       = rbAzureStorage.BlockBlobFileSize();

            byte[] buffer = new byte[fileSize];
            int    readSize;
            int    remain = (int)fileSize;
            int    bufPos = 0;

            while (remain > 0)
            {
                // Read each 1024 bytes
                readSize = stream.Read(buffer, bufPos, Math.Min(1024, remain));
                bufPos  += readSize;
                remain  -= readSize;
            }
            stream.Close();

            return(buffer);
        }
        public void Delete(string filename)
        {
            RbAzureStorage rbAzureStorage = new RbAzureStorage(_accountName, _accountKey);

            rbAzureStorage.BlockBlobDelete(_container, filename);
        }
        CachedDllFileInfo CopyBlobToLocalDir(RbAppMasterCache rbappmc, RbAppRouterCache rbapprc, string partitionId)
        {
            //string curdir = Environment.CurrentDirectory;
            CachedDllFileInfo cachedDllFileInfo = new CachedDllFileInfo();
            string            curdir            = AppDomain.CurrentDomain.BaseDirectory;

            cachedDllFileInfo.BaseDirectory       = curdir;
            cachedDllFileInfo.PrivateDllDirectory = Path.Combine(curdir, "P" + partitionId);

            string            blobTargetFilePath     = string.Empty;
            RbAppDllCacheInfo rbAppDllInfo           = null;
            RbAppDllCacheInfo rbAppDllInfo_partition = null;
            bool   loadAction             = true;
            bool   blobCopyAction         = true;
            string partitionedFileNameKey = "P" + partitionId + "_" + rbapprc.FileName;

            // Check original DLL info
            if (rbAppDllCacheInfoDic.ContainsKey(rbapprc.FileName))
            {
                // Original DLL
                rbAppDllInfo       = (RbAppDllCacheInfo)rbAppDllCacheInfoDic[rbapprc.FileName];
                blobTargetFilePath = Path.Combine(rbAppDllInfo.CacheDir, rbAppDllInfo.CachedFileName);

                // Use cached original DLL if Registered_Datetime not changed.
                if (rbAppDllInfo.AppId == rbapprc.AppId &&
                    rbAppDllInfo.AppProcessingId == rbapprc.AppProcessingId &&
                    rbAppDllInfo.Registered_DateTime == rbapprc.Registered_DateTime)
                {
                    blobCopyAction = false;
                }
            }

            // Check partitioned DLL info
            if (rbAppDllCacheInfoDic.ContainsKey(partitionedFileNameKey))
            {
                // DLL copied into each partition directory
                rbAppDllInfo_partition = (RbAppDllCacheInfo)rbAppDllCacheInfoDic[partitionedFileNameKey];
                cachedDllFileInfo.PrivateDllFilePath = Path.Combine(rbAppDllInfo_partition.CacheDir, rbAppDllInfo_partition.CachedFileName);

                // Use cached DLL copied into each partition directory if Registered_Datetime not changed.
                if (rbAppDllInfo_partition.AppId == rbapprc.AppId &&
                    rbAppDllInfo_partition.AppProcessingId == rbapprc.AppProcessingId &&
                    rbAppDllInfo_partition.Registered_DateTime == rbapprc.Registered_DateTime)
                {
                    loadAction = false;
                }
            }

            if (loadAction)
            {
                if (blobTargetFilePath != string.Empty)
                {
                    AppDomain appDomain = null;
                    if (appDomainList.ContainsKey(partitionId))
                    {
                        appDomain = appDomainList[partitionId];
                        AppDomain.Unload(appDomain);
                        appDomainList[partitionId] = null;
                    }

                    if (blobCopyAction)
                    {
                        // Move current DLL to archive directory
                        if (File.Exists(blobTargetFilePath))
                        {
                            string archivedDirectory   = Path.Combine(curdir, archivedDirectoryName);
                            string archivedDllFilePath = archivedDirectory + @"\" + rbapprc.FileName
                                                         + ".bk" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                            if (!Directory.Exists(archivedDirectory))
                            {
                                Directory.CreateDirectory(archivedDirectory);
                            }
                            File.Move(blobTargetFilePath, archivedDllFilePath);
                        }
                    }
                }

                if (blobCopyAction)
                {
                    // Download DLL from BLOB
                    RbAzureStorage rbAzureStorage = new RbAzureStorage(rbappmc.StorageAccount, rbappmc.StorageKey);
                    rbAppDllInfo          = new RbAppDllCacheInfo();
                    rbAppDllInfo.FileName = rbapprc.FileName;
                    rbAppDllInfo.CacheDir = Path.Combine(curdir, "cache");
                    if (!Directory.Exists(rbAppDllInfo.CacheDir))
                    {
                        Directory.CreateDirectory(rbAppDllInfo.CacheDir);
                    }
                    rbAppDllInfo.AppId               = rbapprc.AppId;
                    rbAppDllInfo.AppProcessingId     = rbapprc.AppProcessingId;
                    rbAppDllInfo.Registered_DateTime = rbapprc.Registered_DateTime;
                    //rbAppDllInfo.GenerateCachedFileName();
                    rbAppDllInfo.CachedFileName = rbAppDllInfo.FileName;
                    blobTargetFilePath          = Path.Combine(rbAppDllInfo.CacheDir, rbAppDllInfo.CachedFileName);

                    using (var fileStream = File.OpenWrite(blobTargetFilePath))
                    {
                        rbAzureStorage.BlockBlobDownload(fileStream, rbapprc.BlobContainer, rbapprc.FileName);
                    }
                    // Update cache info if DLL download from BLOB is successful.
                    rbAppDllCacheInfoDic[rbapprc.FileName] = rbAppDllInfo;

                    // Logging
                    if (rbTraceLevel == RbTraceType.Detail)
                    {
                        RbTraceLog.WriteLog(string.Format("App DLL is copied from BLOB strage.  Dir:{0}, FileName:{1}",
                                                          curdir, rbAppDllInfo.CachedFileName));
                    }
                }

                // Copy original DLL into partition directory
                rbAppDllInfo_partition                     = new RbAppDllCacheInfo();
                rbAppDllInfo_partition.FileName            = rbapprc.FileName;
                rbAppDllInfo_partition.CacheDir            = cachedDllFileInfo.PrivateDllDirectory;
                rbAppDllInfo_partition.AppId               = rbapprc.AppId;
                rbAppDllInfo_partition.AppProcessingId     = rbapprc.AppProcessingId;
                rbAppDllInfo_partition.Registered_DateTime = rbapprc.Registered_DateTime;
                rbAppDllInfo_partition.CachedFileName      = rbAppDllInfo_partition.FileName;

                string sourceFilePath = Path.Combine(rbAppDllInfo.CacheDir, rbAppDllInfo.CachedFileName);
                string targetFilePath = Path.Combine(rbAppDllInfo_partition.CacheDir, rbAppDllInfo_partition.CachedFileName);
                cachedDllFileInfo.PrivateDllFilePath = targetFilePath;
                if (!Directory.Exists(rbAppDllInfo_partition.CacheDir))
                {
                    Directory.CreateDirectory(rbAppDllInfo_partition.CacheDir);
                }
                File.Copy(sourceFilePath, targetFilePath, true);

                // Update cache info if DLL copied successfully.
                rbAppDllCacheInfoDic[partitionedFileNameKey] = rbAppDllInfo_partition;

                // Logging
                if (rbTraceLevel == RbTraceType.Detail)
                {
                    RbTraceLog.WriteLog(string.Format("Original App DLL is copied into partition directory.  Dir:{0}, FileName:{1}, PartitionId:{2}",
                                                      curdir, rbAppDllInfo.CachedFileName, partitionId));
                }
            }

            return(cachedDllFileInfo);
        }