简体   繁体   English

使用.NET Core的Azure存储CRUD操作

[英]Azure Storage CRUD operations using .NET Core

I've been working with Azure Storage resources recently, particularly I was just playing around with CRUD operations in Table. 我最近一直在使用Azure存储资源,尤其是我只是在玩Table中的CRUD操作。 I was able to accomplish a task using .NET Framework, however, recently my requirements have changed and I had to migrate to .NET Core. 我可以使用.NET Framework完成任务,但是最近,我的要求发生了变化,不得不迁移到.NET Core。 I wrote the same type of code by it reaches try-catch statement in CreateTable() and stop for no reason, I am not getting any exceptions or messages, program just exits from there. 我编写了相同类型的代码,因为它到达CreateTable() try-catch语句并无故停止,我没有收到任何异常或消息,程序只是从那里退出。 I have been trying to figure out this issue, so far I came to the following solution that seems to be missing a tiny piece to make it work. 我一直在尝试找出这个问题,到目前为止,我来到了以下解决方案,该解决方案似乎缺少使它起作用的微小部件。

Moreover, previously I was using CosmosDB API to perform this operations. 而且,以前我使用CosmosDB API来执行此操作。 Unfortunately, it is not available in .NET Core, therefore I came up with this solution. 不幸的是,它在.NET Core中不可用,因此我想出了这个解决方案。

My thoughts: I am concerned about authentication part as well, since I can't determine if it was successful or not. 我的想法:我也担心身份验证部分,因为我无法确定它是否成功。 It goes through Auth() well without problems. 它顺利通过Auth()没问题。 I would appreciate if anyone could give me the right direction to the solution of this problem. 如果有人能给我正确的方向解决这个问题,我将不胜感激。 Thanks! 谢谢!

    static CloudStorageAccount _storageAccount;
    static void Main(string[] args)
    {
        Auth();
        CreateTable();
    }
    static void Auth()
    {
         _storageAccount = new CloudStorageAccount(
             new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
                 "MyResource", "MyKey"),true);
    }

    async static void CreateTable()
    {
        CloudTableClient tableClient = _storageAccount.CreateCloudTableClient();
        CloudTable peopleTable = tableClient.GetTableReference("XYZ");
        try
        {
            await peopleTable.CreateIfNotExistsAsync();
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        People customer = new People("Garry", "Johnson");
        customer.Email = "xxx@yyy.zzz";
        customer.PhoneNumber = "123456789";
        TableOperation insertOperation = TableOperation.Insert(customer);
        var result = await peopleTable.ExecuteAsync(insertOperation);
    }

}

I wrote the same type of code by it reaches try-catch statement in CreateTable() and stop for no reason. 我编写了相同类型的代码,因为它到达了CreateTable()中的try-catch语句,并且无故停止。

As the CreateTable() is an async method, the program will exit with no wait to get exectution result. 由于CreateTable()是异步方法,因此程序将立即退出,而无需等待获取执行结果。

I would appreciate if anyone could give me the right direction to the solution of this problem 如果有人能给我正确的方向解决这个问题,我将不胜感激

Please have a try to use the following demo code. 请尝试使用以下演示代码。 If get the 204 in the console meanings that create table and insert record to the table successfully, or you will get the exception information in the console. 如果在控制台中获得204的含义即成功创建表并将记录插入到表中,否则您将在控制台中获得异常信息。

static void Main(string[] args)
        {
            Auth();
            var result = CreateTable().Result;
            Console.WriteLine(result);
            Console.ReadKey();
        }

async static Task<string> CreateTable()
{
      CloudTableClient tableClient = _storageAccount.CreateCloudTableClient();
      CloudTable peopleTable = tableClient.GetTableReference("XYZ");
      try
         {
                await peopleTable.CreateIfNotExistsAsync();
                People customer = new People("Garry", "Johnson");
                customer.Email = "xxx@yyy.zzz";
                customer.PhoneNumber = "123456789";
                TableOperation insertOperation = TableOperation.Insert(customer);
                var result = await peopleTable.ExecuteAsync(insertOperation);
                return result.HttpStatusCode.ToString();
        }
        catch (Exception ex)
        {
             Console.WriteLine(ex.Message);
        }
        return null;

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM