Azure表存储未保存所有对象属性

时间:2019-02-13 10:44:22

标签: azure azure-storage

我对Azure表存储有问题。我要实现的目标是保存SharePoint列表的ChangeToken,以便正确使用Webhook。

代码如下:

public class TablesHelper
{
    private static readonly string TokenTableName = "TokenTable";

    public static async Task<ListChangeToken> GetChangeTokenForListAsync(string listId)
    {
        var retrieveOperation = TableOperation.Retrieve<ListChangeToken>("Lists", listId, new List<string>() { "ChangeToken" });
        var tableReference = await GetTableReferenceAsync(TokenTableName);
        var tableResult = await tableReference.ExecuteAsync(retrieveOperation);
        if(tableResult.Result != null)
        {
            return tableResult.Result as ListChangeToken;
        }
        return null;
    }

    public static async Task SaveChangeTokenForListAsync(ListChangeToken changeToken)
    {
        var insertOperation = TableOperation.Insert(changeToken);
        var tableReference = await GetTableReferenceAsync(TokenTableName);
        var result = await tableReference.ExecuteAsync(insertOperation);
    }

    private static async Task<CloudTable> GetTableReferenceAsync(string tableName)
    {
        var storageAccount = CloudStorageAccount.Parse(ConfigurationHelper.CloudStorage);
        var tableClient = storageAccount.CreateCloudTableClient();
        var reference = tableClient.GetTableReference(tableName);
        await reference.CreateIfNotExistsAsync();
        return reference;
    }
}

ListChangeToken类:

public class ListChangeToken : TableEntity
{
    public ListChangeToken(string listId, string changeToken)
    {
        this.PartitionKey = "Lists";
        this.RowKey = listId;
        this.ChangeToken = changeToken;
    }

    public ListChangeToken() { }

    public string ChangeToken { get; set;}
}

根据请求,调用TablesHelper的函数:

 [FunctionName("EventHandler")]
    public static async Task Run([QueueTrigger("events", Connection = "CloudStorage")]string myQueueItem, TraceWriter log)
    {
        var notificationGroup = Newtonsoft.Json.JsonConvert.DeserializeObject<NotificationGroup>(myQueueItem);
        var contextHelper = new ContextHelper();
        foreach (var notification in notificationGroup.Value)
        {
            UriBuilder uriBuilder = new UriBuilder();
            uriBuilder.Scheme = "https";
            uriBuilder.Host = ConfigurationHelper.TenantDomain;
            uriBuilder.Path = notification.SiteUrl;
            using (var ctx = contextHelper.GetAppOnlyContext(uriBuilder.ToString()))
            {
                //Read change token
                var currentChangeToken = await TablesHelper.GetChangeTokenForListAsync(notification.Resource);
                if(currentChangeToken == null)
                {
                    log.Error($"No change token found for list {notification.Resource}. This is a NO GO. Please use the '/api/Setup' function.");
                }
                var listId = Guid.Parse(notification.Resource);
                var changes = await CSOMHelper.GetListItemChangesAsync(ctx, listId, currentChangeToken.ChangeToken);
                if(changes.Count > 0)
                {
                    var lastChange = changes[changes.Count - 1];
                    //Save the last change token
                    var changeTokenValue = lastChange.ChangeToken.StringValue;
                    await TablesHelper.SaveChangeTokenForListAsync(new ListChangeToken(
                            notification.Resource,
                            changeTokenValue
                        ));
                    await HandleChanges(ctx, changes);
                }
            }
        }
        log.Info($"C# Queue trigger function processed: {myQueueItem}");
    }

问题是,总是在使用“ GetChangeTokenForListAsync”时正确接收到实体,但是.ChangeToken属性始终为null。当使用Azure Storage Explorer浏览时,它也不可见。我在这里做什么错了?

1 个答案:

答案 0 :(得分:0)

该问题与Azure存储模拟器(V. 5.7.0.0)有关。使用“实时” Azure时,相同的代码可以完美地工作。