Table检索现有 Azure Table 项时结果挂起
TableResult hangs when retrieving an existing Azure Table item
我正在使用 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 表中,但是一旦我在 table 中有了项目,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);
永远不会返回。但是如果 table 是空的(所以插入工作正常,但不能从现有的 rowKey 中检索)。
知道为什么它没有回来吗?
我能够重现这个问题。
要解决此问题,请为您的实体添加无参数构造函数。
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;
}
}
我正在使用 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 表中,但是一旦我在 table 中有了项目,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);
永远不会返回。但是如果 table 是空的(所以插入工作正常,但不能从现有的 rowKey 中检索)。
知道为什么它没有回来吗?
我能够重现这个问题。
要解决此问题,请为您的实体添加无参数构造函数。
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;
}
}