Example #1
0
        } // 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
Example #2
0
        } // End of the TestToDownloadImage method

        /// <summary>
        /// Create or wait for a lock
        /// </summary>
        private async Task CreateOrWaitForLock(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.CreateOrWait() == true)
                {
                    Logger.LogMessage("Thread " + threadId.ToString() + ": Has lock for 2 seconds. Date: " + DateTime.UtcNow.ToString("s"));

                    // Read from the blob
                    Logger.LogMessage("Text: " + await blobLock.ReadFrom());

                    // Sleep for 3 seconds
                    await Task.Delay(TimeSpan.FromSeconds(2));
                }
            }

        } // End of the CreateOrWaitForLock method
Example #3
0
        } // 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
Example #4
0
    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