【问题标题】:TableResult hangs when retrieving an existing Azure Table item检索现有 Azure 表项时 TableResult 挂起
【发布时间】:2020-08-04 08:37:02
【问题描述】:

我正在使用 Azure Tables 来对特定对象进行 CRUD 操作。我可以使用此代码正确插入:

public async Task CreateOrUpdate(UserCredentials data)
    {
        if (data == null)
        {
            throw new ArgumentNullException("data");
        }

        await _cloudTable.CreateIfNotExistsAsync();

        var result = Get(data.RowKey).Result;

        if (result != null)
        {
            result.Username = data.Username;
            result.FailedAttempts += 1;

            var retrieveOperation = TableOperation.Replace(result);
            await _cloudTable.ExecuteAsync(retrieveOperation);
        }
        else
        {
            data.CreatedOn = DateTime.Now;
            data.FailedAttempts = data.LoginFailed ? 1 : 0;
            TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(data);
            await _cloudTable.ExecuteAsync(insertOrMergeOperation);
        }
    }

第一次项目在 Azure 表中不存在,但一旦我在表中有项目,Get 功能将无法正常工作。它在执行操作时挂起:

public async Task<UserCredentials> Get(string key)
    {
        await _cloudTable.CreateIfNotExistsAsync();

        TableOperation retrieve = TableOperation.Retrieve<UserCredentials>(nameof(UserCredentials), key);

        TableResult result = await _cloudTable.ExecuteAsync(retrieve);  <-- It never comes back from here

        return result.Result as UserCredentials;
    }

这是我的实体对象:

public class UserCredentials : TableEntity
{
    public string Username { get; set; }
    public string PasswordHash { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime ExpiresOn { get; set; }
    public int FailedAttempts { get; set; }
    public bool LoginFailed { get; set; }


    public UserCredentials(string username)
    {
        PartitionKey = nameof(UserCredentials);
        RowKey = username;
    }
}

因此,当传递现有的 rowKey 时,我的 await _cloudTable.ExecuteAsync(retrieve); 永远不会回来。但是如果表是空的,它就可以完美地工作(因此插入工作正常,但不能从现有的 rowKey 中检索)。

知道为什么它不回来了吗?

【问题讨论】:

    标签: .net azure azure-table-storage


    【解决方案1】:

    我能够重现此问题。

    要解决此问题,请为您的实体添加一个无参数构造函数。

    public class UserCredentials : TableEntity
    {
        public string Username { get; set; }
        public string PasswordHash { get; set; }
        public DateTime CreatedOn { get; set; }
        public DateTime ExpiresOn { get; set; }
        public int FailedAttempts { get; set; }
        public bool LoginFailed { get; set; }
    
        public UserCredentials()
        {
    
        }
    
        public UserCredentials(string username)
        {
            PartitionKey = nameof(UserCredentials);
            RowKey = username;
        }
    }
    

    【讨论】:

    • 我在 30 秒前才发现...是的!而已。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 2013-03-25
    • 2011-06-08
    相关资源
    最近更新 更多