首页 > 解决方案 > Azure C# v2 函数:数据未插入 Azure 表存储

问题描述

我正在编写一个 C# Azure v2 计时器函数,它从一个表 ( OneAuthZRoleAssignments) 复制十行并将它们插入到另一个表 ( OneAuthZPreviousRoleAssignments) 中。我的函数用于从OneAuthZRoleAssignments表中读取(打印语句显示正确的值)......但是,它无法将读取的行插入OneAuthZPreviousRoleAssignments(注意:accountNameaccountKey变量已定义,出于安全目的,我只是省略了它们的实际值当然)。下面是我的代码:

using System;
using System.Buffers.Text;
using System.Collections.Generic;
using System.Data;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Table;

namespace AccessChangeMonitoring
{
    public static class Function1
    {
        // Authenticate access into the database's Azure Table Storage
        static StorageCredentials creds = new StorageCredentials(accountName, accountKey);
        static CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);

        [FunctionName("Function1")]
        // Function that reads a small portion of the role assignments table (OneAuthZRoleAssignments) every 
        // configurable number of times
        public static async System.Threading.Tasks.Task RunAsync([TimerTrigger("%TimerTriggerPeriod%")]TimerInfo myTimer, ILogger log)
        {
            // Retrieve the role assignments table
            CloudTableClient client = account.CreateCloudTableClient();
            // Current role assignments
            CloudTable roleAssignmentsTable = client.GetTableReference("OneAuthZRoleAssignments"); 
            // LKG (Last Known Good) role assignments
            CloudTable previousRoleAssignmentsTable = client.GetTableReference("OneAuthZPreviousRoleAssignments");

            // Test out retrieving a small portion from the role assignments table (10 rows)
            var tablePortion = new List<RoleAssignment>(); // Stores the query of role assignment
            TableContinuationToken token = null; // Allows our query to iterate to the next role row

            // Define our query (in this case, set the number of rows we want to retrieve at 10)
            TableQuery<RoleAssignment> tableQuery = new TableQuery<RoleAssignment>();
            tableQuery.Take(10);

            // Retrieve the rows from Azure Table Storage
            var queryResult = await roleAssignmentsTable.ExecuteQuerySegmentedAsync(tableQuery, token);
            tablePortion.AddRange(queryResult.Results);

            // Copy the rows to the LKG (Last Known Good) table
            CopyRows(tablePortion, previousRoleAssignmentsTable);
        }

        [FunctionName("CopyRows")]
        // Copies a list of rows to another table 
        public static void CopyRows(List<RoleAssignment> queriedRows, CloudTable destinationTable)
        {
            // Iterate through all of the rows
            foreach (RoleAssignment row in queriedRows)
            {
                // Define the insertion operation
                TableOperation insert = TableOperation.Insert(row);
                // Execute the insertion operation
                destinationTable.ExecuteAsync(insert);

                // OUTPUTS THE CORRECT VALUES
                Console.WriteLine("------------------------------");
                Console.WriteLine("Row: {0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n{7}\n{8}\n{9}\n{10}\n{11}\n{12}" +
                    "\n{13}\n{14}\n{15}\n{16}\n{17}", row.PartitionKey, row.RowKey, row.Timestamp,
                    row.AppId, row.ApplicationName, row.AssignedAlias, row.AssignedName, row.AssignedUPN, row.Condition,
                    row.Id, row.IsBuiltIn, row.PrincipalId, row.RoleDefinitionId, row.RoleDefintionName, row.Scope, row.UpdatedBy,
                    row.UpdatedByAlias, row.UpdatedByName
                    );
                Console.WriteLine("------------------------------");
            }
        }
    }
}

作为参考,这是代表表中一行的实体类RoleAssignment

using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.Collections.Generic;
using System.Text;

namespace AccessChangeMonitoring
{
    // RoleAssignment represents a row in the Azure Table Storage OneAuthZRoleAssignments
    public class RoleAssignment:TableEntity
    {
        // Constructors
        public RoleAssignment()
        {
        }

        public RoleAssignment(string PartitionKey, string RowKey, DateTime Timestamp, Guid AppId, string ApplicationName,
            string AssignedAlias, string AssignedName, string AssignedUPN, string Condition,
            string Id, bool IsBuiltIn, string PrincipalId, string RoleDefinitionId, string RoleDefintionName,
            string Scope, string UpdatedBy, string UpdatedByAlias, string UpdatedByName)
        {
            this.PartitionKey = PartitionKey;
            this.RowKey = RowKey;
            this.TimeStamp = Timestamp;
            this.AppId = AppId;
            this.ApplicationName = ApplicationName;
            this.AssignedAlias = AssignedAlias;
            this.AssignedName = AssignedName;
            this.AssignedUPN = AssignedUPN;
            this.Condition = Condition;
            this.Id = Id;
            this.IsBuiltIn = IsBuiltIn;
            this.PrincipalId = PrincipalId;
            this.RoleDefinitionId = RoleDefinitionId;
            this.RoleDefintionName = RoleDefintionName;
            this.Scope = Scope;
            this.UpdatedBy = UpdatedBy;
            this.UpdatedByAlias = UpdatedByAlias;
            this.UpdatedByName = UpdatedByName;
        }

        // The row properties
        public DateTime TimeStamp { get; set; }
        public Guid AppId { get; set; }
        public string ApplicationName { get; set; }
        public string AssignedAlias { get; set; }
        public string AssignedName { get; set; }
        public string AssignedUPN { get; set; }
        public string Condition { get; set; }
        public string Id { get; set; }
        public Boolean IsBuiltIn { get; set; }
        public string PrincipalId { get; set; }
        public string RoleDefinitionId { get; set; }
        public string RoleDefintionName { get; set; }
        public string Scope { get; set; }
        public string UpdatedBy { get; set; }
        public string UpdatedByAlias { get; set; }
        public string UpdatedByName { get; set; }
    }
}

下面是OneAuthZRoleAssignments表格: 在此处输入图像描述

这看起来很奇怪,考虑到我可以从表中读取数据,OneAuthZRoleAssignments并且我从 ( OneAuthZRoleAssignments) 读取的表和我从 ( ) 写入的表OneAuthZPreviousRoleAssignments都在同一个存储帐户中:

在此处输入图像描述

标签: c#azure

解决方案


可能发生了一个异常,destinationTable.ExecuteAsync(insert);因为您丢失了,所以您没有看到await。查看对代码的这些更改如何更改结果:

            // Copy the rows to the LKG (Last Known Good) table
            CopyRows(tablePortion, previousRoleAssignmentsTable);
        }

        public static void CopyRows(List<RoleAssignment> queriedRows, CloudTable destinationTable)
        {
//...
                destinationTable.ExecuteAsync(insert);

将其更改为:

            // Copy the rows to the LKG (Last Known Good) table
            await CopyRows(tablePortion, previousRoleAssignmentsTable);
        }

        public static async Task CopyRows(List<RoleAssignment> queriedRows, CloudTable destinationTable)
        {
//...
                await destinationTable.ExecuteAsync(insert);


推荐阅读