【问题标题】:Creating a table in Azure Table fails without exception在 Azure 表中创建表失败,无一例外
【发布时间】:2017-10-16 06:06:37
【问题描述】:

我正在尝试在 Windows Azure 表服务中创建一个表,使用示例 https://github.com/Azure-Samples/storage-table-dotnet-getting-started

在运行这个例子时,我没有问题:表自己创建,插入和删除工作正常。

但是我作为代码复制到我的项目中的内容似乎无法正常工作。表创建显然失败,但不返回任何异常。就像我的代码没有等待表创建完成。

谁能明白为什么?

感谢您的帮助!

我是这样称呼代码的:

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

这是 AzureStorage 类:

    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;
        }

    }
}

【问题讨论】:

  • async void 通常应该避免:stackoverflow.com/q/12144077/453277
  • 确实,显然 async void 应该替换为 async Task。这本身就是一个空白,因为您通常会使用 async Task

标签: c# azure-storage azure-table-storage


【解决方案1】:

根据您的代码和描述,我猜您的代码无法创建表的原因是您的异步​​ saveInAzureTable。

我猜你在控制台主方法或其他方法中调用了这个方法。

如果您的 main 方法已完全执行,则您的异步方法 saveInAzureTable 不会执行创建表代码。它将结束所有异步方法。所以它可能不会毫无例外地创建表。

我建议您可以使用 Task 关键字代替 void。然后你可以使用wait关键字等待方法执行好。

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

调用这个方法:

   saveInAzureTable().Wait();

【讨论】:

    【解决方案2】:

    我真的希望我能发表评论,但我还没有足够的积分。

    我相信异步调用可能会吞噬您的错误消息。

    改成同步调用然后试试

    【讨论】:

      猜你喜欢
      • 2019-12-27
      • 2012-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-29
      • 2019-06-07
      相关资源
      最近更新 更多