C# 在Azure表中创建表失败,无异常

C# 在Azure表中创建表失败,无异常,c#,azure-storage,azure-table-storage,C#,Azure Storage,Azure Table Storage,我正尝试使用以下示例在WindowsAzure表服务中创建一个表 在运行这个示例时,我没有任何问题:表会自行创建,插入和删除工作正常 但我在项目中复制的代码似乎无法正常工作。表创建显然失败了,但没有返回任何异常。就像我的代码不等待表创建完成一样 有人能理解为什么吗 谢谢你的帮助 以下是我如何调用代码: public async void saveInAzureTable() { AzureStorage azure = new AzureStorage();

我正尝试使用以下示例在WindowsAzure表服务中创建一个表

在运行这个示例时,我没有任何问题:表会自行创建,插入和删除工作正常

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

有人能理解为什么吗

谢谢你的帮助

以下是我如何调用代码:

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

    }
}
使用系统;
使用System.Threading.Tasks;
使用Microsoft.Azure;
使用Microsoft.WindowsAzure.Storage;
使用Microsoft.Wi`在此处输入代码`ndowsAzure.Storage.Table;
使用刮刀。模型;
名称空间刮刀
{
公共级AzureStorage
{
公共异步任务storeAdvertisementInAzure(CloudTable表,AdvertisementEntity)
{
TableOperation insertOrMergeOperation=TableOperation.InsertOrMerge(实体);
TableResult结果=等待table.ExecuteAsync(insertOrMergeOperation);
返回结果;
}
/// 
///验证app.config中的连接字符串信息,并在如下情况下引发异常
///用户尚未将其更新为有效值。
/// 
///存储服务或模拟器的连接字符串
///CloudStorageAccount对象
公共静态CloudStorageAccount CreateStorageAccountFromConnectionString(string-storageConnectionString)
{
CloudStorageAccount-storageAccount;
尝试
{
storageAccount=CloudStorageAccount.Parse(storageConnectionString);
}
捕获(格式化异常)
{
Console.WriteLine(“提供的存储帐户信息无效。请确认AccountName和AccountKey在app.config文件中有效-然后重新启动应用程序。”);
投掷;
}
捕获(异常)
{
Console.WriteLine(“提供的存储帐户信息无效。请确认AccountName和AccountKey在app.config文件中有效-然后重新启动示例”);
Console.ReadLine();
投掷;
}
返回存储帐户;
}
/// 
///为示例应用程序创建一个表以处理其中的消息。
/// 
///云状物体
公共静态异步任务CreateTableAsync(字符串tableName)
{
//从连接字符串中检索存储帐户信息。
CloudStorageAccount storageAccount=CreateStorageAccountFromConnectionString(CloudConfigurationManager.GetSetting(“StorageConnectionString”);
//创建用于与表服务交互的表客户端
CloudTableClient tableClient=storageAccount.CreateCloudTableClient();
WriteLine(“创建一个表来存储来自scraper的数据”);
//创建用于与表服务交互的表客户端
CloudTable=tableClient.GetTableReference(tableName);
尝试
{
if(wait table.CreateIfNotExistsAsync())
{
WriteLine(“创建的表名为:{0}”,tableName);
}
其他的
{
WriteLine(“表{0}已经存在”,tableName);
}
}
捕获(存储异常)
{
Console.WriteLine(“如果使用默认配置运行,请确保已启动存储模拟器。按Windows键并键入Azure storage以从应用程序列表中选择并运行它,然后重新启动示例。”);
Console.ReadLine();
投掷;
}
Console.WriteLine();
返回表;
}
}
}

我真希望我能发表评论,但我还没有足够的观点

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


将其更改为同步调用,然后重试

根据您的代码和描述,我猜代码无法创建表的原因是关于您的异步savenazuretable

我猜您可以在控制台中调用这个方法main方法或其他方法

如果主方法已完全执行,则异步方法savenazuretable不会执行创建表代码。它将结束所有异步方法。因此,它可能无法创建表,毫无例外

我建议您可以使用Task关键字而不是void。然后,您可以使用wait关键字来等待方法很好地执行

 public static async Task saveInAzureTable()
        {
            AzureStorage azure = new AzureStorage();
            CloudTable table = await AzureStorage.CreateTableAsync("aaaaaa");
        }
调用此方法:

   saveInAzureTable().Wait();

async void
通常应该避免:事实上,显然async void应该被async任务所取代。这本身就是一个空白,因为您通常会使用异步任务