TableResult在检索现有Azure表项目时挂起

问题描述 投票:0回答:1

我正在使用Azure表来对特定对象进行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个回答
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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.