AlexandreG
AlexandreG

Reputation: 1683

Creating a table in Azure Table fails without exception

I'm trying to create a table in Windows Azure Table service, using the example https://github.com/Azure-Samples/storage-table-dotnet-getting-started

While running this example, I have no problem : the table creates itself, insert and delete work fine.

But what I copied as code into my project can't seem to work properly. Table creation apparently fails but returns no exception whatsoever. It's like my code does not wait for the table creation to finish.

Can any understand why ?

Thanks for your help !

Here is how I call the code :

public async void saveInAzureTable()
{
            AzureStorage azure = new AzureStorage();
            CloudTable table = await     AzureStorage.CreateTableAsync("randomtable123");
}

And here's the AzureStorage class :

    using System;
    using System.Threading.Tasks;
    using Microsoft.Azure;
    using Microsoft.WindowsAzure.Storage;
    using Microsoft.Wi`enter code here`ndowsAzure.Storage.Table;
    using Scraper.Models;

    namespace Scraper
    {
        public class AzureStorage
        {
            public async Task<TableResult> storeAdvertisementInAzure(CloudTable table, AdvertisementEntity entity)
        {
            TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(entity);
            TableResult result = await table.ExecuteAsync(insertOrMergeOperation);
            return result;
        }

        /// <summary>
        /// Validate the connection string information in app.config and throws an exception if it looks like 
        /// the user hasn't updated this to valid values. 
        /// </summary>
        /// <param name="storageConnectionString">Connection string for the storage service or the emulator</param>
        /// <returns>CloudStorageAccount object</returns>
        public static CloudStorageAccount CreateStorageAccountFromConnectionString(string storageConnectionString)
        {
            CloudStorageAccount storageAccount;
            try
            {
                storageAccount = CloudStorageAccount.Parse(storageConnectionString);
            }
            catch (FormatException)
            {
                Console.WriteLine("Invalid storage account information provided. Please confirm the AccountName and AccountKey are valid in the app.config file - then restart the application.");
                throw;
            }
            catch (ArgumentException)
            {
                Console.WriteLine("Invalid storage account information provided. Please confirm the AccountName and AccountKey are valid in the app.config file - then restart the sample.");
                Console.ReadLine();
                throw;
            }

            return storageAccount;
        }


        /// <summary>
        /// Create a table for the sample application to process messages in. 
        /// </summary>
        /// <returns>A CloudTable object</returns>
        public static async Task<CloudTable> CreateTableAsync(string tableName)
        {
            // Retrieve storage account information from connection string.
            CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(CloudConfigurationManager.GetSetting("StorageConnectionString"));

            // Create a table client for interacting with the table service
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            Console.WriteLine("Create a Table to store data from the scraper");

            // Create a table client for interacting with the table service 
            CloudTable table = tableClient.GetTableReference(tableName);
            try
            {
                if (await table.CreateIfNotExistsAsync())
                {
                    Console.WriteLine("Created Table named: {0}", tableName);
                }
                else
                {
                    Console.WriteLine("Table {0} already exists", tableName);
                }
            }
            catch (StorageException)
            {
                Console.WriteLine("If you are running with the default configuration please make sure you have started the storage emulator. Press the Windows key and type Azure Storage to select and run it from the list of applications - then restart the sample.");
                Console.ReadLine();
                throw;
            }

            Console.WriteLine();
            return table;
        }

    }
}

Upvotes: 2

Views: 1635

Answers (2)

Brando Zhang
Brando Zhang

Reputation: 28387

According to your code and description, I guess the reason why your code couldn't create the table is about your async saveInAzureTable.

I guess you call this method in console main method or something else.

If your main method is executed completely, your async method saveInAzureTable isn't execute the create the table codes. It will end all async method. So it may not create table without any exception.

I suggest you could use Task keyword instead of the void. Then you could use wait key word to wait the method execute well.

 public static async Task saveInAzureTable()
        {
            AzureStorage azure = new AzureStorage();
            CloudTable table = await AzureStorage.CreateTableAsync("aaaaaa");
        }

Call this method:

   saveInAzureTable().Wait();

Upvotes: 3

Andrew
Andrew

Reputation: 770

I really wish I could comment but I don't have enough points yet.

I believe that the async call could be swallowing your error message.

Change it to a synchronous call and try then

Upvotes: 0

Related Questions