} // End of the UploadImage method /// <summary> /// Download an image from the blob /// </summary> private async Task DownloadImage(Int32 threadId) { // Add options BlobLockOptions options = new BlobLockOptions() { connection_string = this.configuration.GetSection("AppSettings")["AzureStorageAccount"], container_name = "test-locks", blob_name = "image.jpg" }; // Use a blob lock, the lock is disposed by the using block using (BlobLock blobLock = new BlobLock(options)) { // Do work inside a blob lock if (await blobLock.CreateOrSkip() == true) { Logger.LogMessage("Thread " + threadId.ToString() + ": Has lock during download."); // Download an image to a file using (FileStream fileStream = File.OpenWrite("D:\\Bilder\\Azure-blob-image.jpg")) { await blobLock.ReadFrom(fileStream); } Logger.LogMessage("Thread " + threadId.ToString() + ": Image has been downloaded."); } else { Logger.LogMessage("Thread " + threadId.ToString() + ": Does not wait for the lock to be released."); } } } // End of the DownloadImage method
} // End of the CreateOrWaitForLock method /// <summary> /// Create a lock or skip if the lock is taken /// </summary> private async Task CreateOrSkipLock(Int32 threadId) { // Add options BlobLockOptions options = new BlobLockOptions(); options.connection_string = this.configuration.GetSection("AppSettings")["AzureStorageAccount"]; options.container_name = "test-locks"; options.blob_name = "test.lck"; // Use a blob lock, the lock is disposed by the using block using (BlobLock blobLock = new BlobLock(options)) { // Do work inside a blob lock if (await blobLock.CreateOrSkip() == true) { Logger.LogMessage("Thread " + threadId.ToString() + ": Has lock for 30 seconds."); // Sleep for 30 seconds await Task.Delay(TimeSpan.FromSeconds(30)); // Write to the blob await blobLock.WriteTo(threadId.ToString()); } else { Logger.LogMessage("Thread " + threadId.ToString() + ": Does not wait for the lock to be released."); } } } // End of the CreateOrSkipLock method
public static Int32 DATABASE_VERSION = 13; // The version number is +1 compared to the file version number #endregion #region Upgrade methods /// <summary> /// Upgrade the database /// </summary> public static void UpgradeDatabase() { // Variables string azureStorageAccount = Tools.GetAzureStorageAccount(); Int32 currentDatabaseVersion = 0; // Check if a storage account exists if (azureStorageAccount != "") { // Variables BlobLock blobLock = null; try { // Create a blob lock blobLock = new BlobLock(azureStorageAccount, "locks", GetLockFilename()); // Do work inside a blob lock if (blobLock.CreateOrSkip() == true) { // Get the database version if(Int32.TryParse(blobLock.ReadFrom(), out currentDatabaseVersion) == false) { currentDatabaseVersion = GetDatabaseVersion(); } // Upgrade the database UpgradeDatabaseFromFiles(currentDatabaseVersion); // Set the new database version blobLock.WriteTo(DATABASE_VERSION.ToString()); } } catch (Exception ex) { throw ex; } finally { // Dispose of the blob lock if (blobLock != null) { blobLock.Dispose(); } } } else { // Get the current database version currentDatabaseVersion = GetDatabaseVersion(); // Upgrade the database UpgradeDatabaseFromFiles(currentDatabaseVersion); // Set the new database version SetDatabaseVersion(DATABASE_VERSION); } } // End of the UpgradeDatabase method