本文整理汇总了C#中Microsoft.WindowsAzure.Storage.Table.CloudTable.ExecuteAsync方法的典型用法代码示例。如果您正苦于以下问题:C# CloudTable.ExecuteAsync方法的具体用法?C# CloudTable.ExecuteAsync怎么用?C# CloudTable.ExecuteAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.WindowsAzure.Storage.Table.CloudTable
的用法示例。
在下文中一共展示了CloudTable.ExecuteAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RetrieveEntityUsingPointQueryAsync
/// <summary>
/// Demonstrate the most efficient storage query - the point query - where both partition key and row key are specified.
/// </summary>
/// <param name="table">Sample table name</param>
/// <param name="partitionKey">Partition key - ie - last name</param>
/// <param name="rowKey">Row key - ie - first name</param>
private static async Task<AuditModel> RetrieveEntityUsingPointQueryAsync(CloudTable table, string partitionKey, string rowKey)
{
TableOperation retrieveOperation = TableOperation.Retrieve<AuditModel>(partitionKey, rowKey);
TableResult result = await table.ExecuteAsync(retrieveOperation);
AuditModel audit = result.Result as AuditModel;
return audit;
}
示例2: DoInsert
async Task<CommandResult> DoInsert(CloudTable table, long n, Func<long, EntityNk> entityFactory)
{
var e = entityFactory(n);
#if INSERT_OR_REPLACE
var tableOperation = TableOperation.InsertOrReplace(e);
#else
var tableOperation = TableOperation.Insert(e);
#endif
var cresult = new CommandResult { Start = DateTime.UtcNow.Ticks };
var cbt = 0L;
var context = GetOperationContext((t) => cbt = t);
try
{
var result = await table.ExecuteAsync(tableOperation, operationContext: context);
cresult.Elapsed = cbt;
}
catch (Exception ex)
{
cresult.Elapsed = -1;
Console.Error.WriteLine("Error DoInsert {0} {1}", n, ex.ToString());
}
return cresult;
}
示例3: UpdateEntity
private async Task UpdateEntity(CloudTable table, DynamicTableEntity entity)
{
try
{
await table.ExecuteAsync(TableOperation.Merge(entity)).ConfigureAwait(false);
return;
}
catch(StorageException ex)
{
if(!Helper.IsError(ex, "EntityTooLarge"))
throw;
}
var serialized = entity.Serialize();
Configuration
.GetBlocksContainer()
.GetBlockBlobReference(entity.GetFatBlobName())
.UploadFromByteArray(serialized, 0, serialized.Length);
entity.MakeFat(serialized.Length);
await table.ExecuteAsync(TableOperation.InsertOrReplace(entity)).ConfigureAwait(false);
}
示例4: TableSASNullAccessPolicyAsync
public async Task TableSASNullAccessPolicyAsync()
{
CloudTableClient tableClient = GenerateCloudTableClient();
CloudTable table = tableClient.GetTableReference("T" + Guid.NewGuid().ToString("N"));
try
{
await table.CreateAsync();
await table.ExecuteAsync(TableOperation.Insert(new BaseEntity("PK", "RK")));
TablePermissions expectedPermissions = new TablePermissions();
// Add a policy
expectedPermissions.SharedAccessPolicies.Add(Guid.NewGuid().ToString(), new SharedAccessTablePolicy
{
Permissions = SharedAccessTablePermissions.Query | SharedAccessTablePermissions.Add,
SharedAccessStartTime = DateTimeOffset.Now - TimeSpan.FromHours(1),
SharedAccessExpiryTime = DateTimeOffset.Now + TimeSpan.FromHours(1)
});
await table.SetPermissionsAsync(expectedPermissions);
await Task.Delay(30 * 1000);
// Generate the sasToken the user should use
string sasToken = table.GetSharedAccessSignature(null, expectedPermissions.SharedAccessPolicies.First().Key, "AAAA", null, "AAAA", null);
CloudTable sasTable = new CloudTable(table.Uri, new StorageCredentials(sasToken));
await sasTable.ExecuteAsync(TableOperation.Insert(new DynamicTableEntity("AAAA", "foo")));
TableResult result = await sasTable.ExecuteAsync(TableOperation.Retrieve("AAAA", "foo"));
Assert.IsNotNull(result.Result);
// revoke table permissions
await table.SetPermissionsAsync(new TablePermissions());
await Task.Delay(30 * 1000);
OperationContext opContext = new OperationContext();
try
{
await sasTable.ExecuteAsync(TableOperation.Insert(new DynamicTableEntity("AAAA", "foo2")), null, opContext);
Assert.Fail();
}
catch (Exception)
{
Assert.AreEqual(opContext.LastResult.HttpStatusCode, (int)HttpStatusCode.Forbidden);
}
opContext = new OperationContext();
try
{
result = await sasTable.ExecuteAsync(TableOperation.Retrieve("AAAA", "foo"), null, opContext);
Assert.Fail();
}
catch (Exception)
{
Assert.AreEqual(opContext.LastResult.HttpStatusCode, (int)HttpStatusCode.Forbidden);
}
}
finally
{
table.DeleteIfExistsAsync().AsTask().Wait();
}
}
示例5: TableSasUriTestAsync
public async Task TableSasUriTestAsync()
{
CloudTableClient tableClient = GenerateCloudTableClient();
CloudTable table = tableClient.GetTableReference("T" + Guid.NewGuid().ToString("N"));
try
{
await table.CreateAsync();
BaseEntity entity = new BaseEntity("PK", "RK");
BaseEntity entity1 = new BaseEntity("PK", "RK1");
await table.ExecuteAsync(TableOperation.Insert(entity));
await table.ExecuteAsync(TableOperation.Insert(entity1));
SharedAccessTablePolicy policy = new SharedAccessTablePolicy()
{
SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5),
SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
Permissions = SharedAccessTablePermissions.Delete,
};
string sasToken = table.GetSharedAccessSignature(policy, null, null, null, null, null);
StorageCredentials creds = new StorageCredentials(sasToken);
CloudStorageAccount sasAcc = new CloudStorageAccount(creds, null /* blobEndpoint */, null /* queueEndpoint */, new Uri(TestBase.TargetTenantConfig.TableServiceEndpoint), null /* fileEndpoint */);
CloudTableClient client = sasAcc.CreateCloudTableClient();
CloudTable sasTable = new CloudTable(client.Credentials.TransformUri(table.Uri));
await sasTable.ExecuteAsync(TableOperation.Delete(entity));
CloudTable sasTable2 = new CloudTable(new Uri(table.Uri.ToString() + sasToken));
await sasTable2.ExecuteAsync(TableOperation.Delete(entity1));
}
finally
{
table.DeleteIfExistsAsync().AsTask().Wait();
}
}
示例6: TableUpdateSasTestAsync
public async Task TableUpdateSasTestAsync()
{
CloudTableClient tableClient = GenerateCloudTableClient();
CloudTable table = tableClient.GetTableReference("T" + Guid.NewGuid().ToString("N"));
try
{
await table.CreateAsync();
BaseEntity entity = new BaseEntity("PK", "RK");
await table.ExecuteAsync(TableOperation.Insert(entity));
SharedAccessTablePolicy policy = new SharedAccessTablePolicy()
{
SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5),
SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
Permissions = SharedAccessTablePermissions.Delete,
};
string sasToken = table.GetSharedAccessSignature(policy, null, null, null, null, null);
StorageCredentials creds = new StorageCredentials(sasToken);
CloudTable sasTable = new CloudTable(table.Uri, creds);
OperationContext context = new OperationContext();
await TestHelper.ExpectedExceptionAsync(
async () => await sasTable.ExecuteAsync(TableOperation.Insert(new BaseEntity("PK", "RK2")), null, context),
context,
"Try to insert an entity when SAS doesn't allow inserts",
HttpStatusCode.Forbidden);
await sasTable.ExecuteAsync(TableOperation.Delete(entity));
SharedAccessTablePolicy policy2 = new SharedAccessTablePolicy()
{
SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5),
SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
Permissions = SharedAccessTablePermissions.Delete | SharedAccessTablePermissions.Add,
};
string sasToken2 = table.GetSharedAccessSignature(policy2, null, null, null, null, null);
creds.UpdateSASToken(sasToken2);
sasTable = new CloudTable(table.Uri, creds);
await sasTable.ExecuteAsync(TableOperation.Insert(new BaseEntity("PK", "RK2")));
}
finally
{
table.DeleteIfExistsAsync().AsTask().Wait();
}
}
示例7: ExecuteAsync
/// <inheritdoc />
public async Task<TableResult> ExecuteAsync(CloudTable table, TableOperation operation)
{
if (table == null)
{
throw new ArgumentNullException(nameof(table));
}
if (operation == null)
{
throw new ArgumentNullException(nameof(operation));
}
try
{
TableResult result = await table.ExecuteAsync(operation);
return result;
}
catch (Exception ex)
{
string errorMessage = GetStorageErrorMessage(ex);
int statusCode = GetStorageStatusCode(ex);
string msg = string.Format(CultureInfo.CurrentCulture, AzureStorageResources.StorageManager_OperationFailed, statusCode, errorMessage);
_logger.Error(msg, ex);
return new TableResult { HttpStatusCode = statusCode };
}
}
示例8: RetrieveEntityUsingPointQueryAsync
/// <summary>
/// Demonstrate the most efficient storage query - the point query - where both partition key and row key are specified.
/// </summary>
/// <param name="table">Sample table name</param>
/// <param name="partitionKey">Partition key - ie - last name</param>
/// <param name="rowKey">Row key - ie - first name</param>
private static async Task<CustomerEntity> RetrieveEntityUsingPointQueryAsync(CloudTable table, string partitionKey, string rowKey)
{
TableOperation retrieveOperation = TableOperation.Retrieve<CustomerEntity>(partitionKey, rowKey);
TableResult result = await table.ExecuteAsync(retrieveOperation);
CustomerEntity customer = result.Result as CustomerEntity;
if (customer != null)
{
Console.WriteLine("\t{0}\t{1}\t{2}\t{3}", customer.PartitionKey, customer.RowKey, customer.Email, customer.PhoneNumber);
}
return customer;
}
示例9: GetEntity
private static async Task<UrlEntityInternal> GetEntity(string id, string AccessToken, CloudTable tableRef)
{
TableResult retrievedResult = null;
TableOperation retrieveOperation = TableOperation.Retrieve<UrlEntityInternal>( id.Substring(3, 3), id.Substring(6));
retrievedResult = await tableRef.ExecuteAsync(retrieveOperation);
var entity = retrievedResult.Result as UrlEntityInternal;
if (entity == null)
throw new NotFoundException(id);
if(AccessToken!=null)
if (!entity.AccessToken.Equals(AccessToken))
throw new UnAuthorizedException(id);
return entity;
}
示例10: TestTableSAS
/// <summary>
/// Tests a table SAS to determine which operations it allows.
/// </summary>
/// <param name="sasUri">A string containing a URI with a SAS appended.</param>
/// <param name="customer">The customer entity.</param>
/// <returns>A Task object</returns>
private static async Task TestTableSAS(string sasUri, CustomerEntity customer)
{
// Try performing table operations with the SAS provided.
// Note that the storage account credentials are not required here; the SAS provides the necessary
// authentication information on the URI.
// Return a reference to the table using the SAS URI.
CloudTable table = new CloudTable(new Uri(sasUri));
// Upsert (add/update) operations: insert an entity.
// This operation requires both add and update permissions on the SAS.
try
{
// Insert the new entity.
customer = await InsertOrMergeEntityAsync(table, customer);
Console.WriteLine("Add operation succeeded for SAS {0}", sasUri);
Console.WriteLine();
}
catch (StorageException e)
{
if (e.RequestInformation.HttpStatusCode == 403)
{
Console.WriteLine("Add operation failed for SAS {0}", sasUri);
Console.WriteLine("Additional error information: " + e.Message);
Console.WriteLine();
}
else
{
Console.WriteLine(e.Message);
Console.ReadLine();
throw;
}
}
// Read operation: query an entity.
// This operation requires read permissions on the SAS.
try
{
TableOperation retrieveOperation = TableOperation.Retrieve<CustomerEntity>(customer.PartitionKey, customer.RowKey);
TableResult result = await table.ExecuteAsync(retrieveOperation);
CustomerEntity customerRead = result.Result as CustomerEntity;
if (customerRead != null)
{
Console.WriteLine("\t{0}\t{1}\t{2}\t{3}", customerRead.PartitionKey, customerRead.RowKey, customerRead.Email, customerRead.PhoneNumber);
}
Console.WriteLine("Read operation succeeded for SAS {0}", sasUri);
Console.WriteLine();
}
catch (StorageException e)
{
if (e.RequestInformation.HttpStatusCode == 403)
{
Console.WriteLine("Read operation failed for SAS {0}", sasUri);
Console.WriteLine("Additional error information: " + e.Message);
Console.WriteLine();
}
else
{
Console.WriteLine(e.Message);
Console.ReadLine();
throw;
}
}
// Delete operation: delete an entity.
try
{
TableOperation retrieveOperation = TableOperation.Retrieve<CustomerEntity>(customer.PartitionKey, customer.RowKey);
TableResult result = await table.ExecuteAsync(retrieveOperation);
CustomerEntity customerDelete = result.Result as CustomerEntity;
if (customerDelete != null)
{
await DeleteEntityAsync(table, customerDelete);
}
Console.WriteLine("Delete operation succeeded for SAS {0}", sasUri);
Console.WriteLine();
}
catch (StorageException e)
{
if (e.RequestInformation.HttpStatusCode == 403)
{
Console.WriteLine("Delete operation failed for SAS {0}", sasUri);
Console.WriteLine("Additional error information: " + e.Message);
Console.WriteLine();
}
else
{
Console.WriteLine(e.Message);
Console.ReadLine();
throw;
}
//.........这里部分代码省略.........
示例11: DeleteEntityAsync
/// <summary>
/// Delete an entity
/// </summary>
/// <param name="table">Sample table name</param>
/// <param name="deleteEntity">Entity to delete</param>
/// <returns>A Task object</returns>
private static async Task DeleteEntityAsync(CloudTable table, CustomerEntity deleteEntity)
{
try
{
if (deleteEntity == null)
{
throw new ArgumentNullException("deleteEntity");
}
TableOperation deleteOperation = TableOperation.Delete(deleteEntity);
await table.ExecuteAsync(deleteOperation);
}
catch (StorageException e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
throw;
}
}
示例12: InsertOrMergeEntityAsync
/// <summary>
/// The Table Service supports two main types of insert operations.
/// 1. Insert - insert a new entity. If an entity already exists with the same PK + RK an exception will be thrown.
/// 2. Replace - replace an existing entity. Replace an existing entity with a new entity.
/// 3. Insert or Replace - insert the entity if the entity does not exist, or if the entity exists, replace the existing one.
/// 4. Insert or Merge - insert the entity if the entity does not exist or, if the entity exists, merges the provided entity properties with the already existing ones.
/// </summary>
/// <param name="table">The sample table name</param>
/// <param name="entity">The entity to insert or merge</param>
/// <returns>A Task object</returns>
private static async Task<CustomerEntity> InsertOrMergeEntityAsync(CloudTable table, CustomerEntity entity)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
try
{
// Create the InsertOrReplace table operation
TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(entity);
// Execute the operation.
TableResult result = await table.ExecuteAsync(insertOrMergeOperation);
CustomerEntity insertedCustomer = result.Result as CustomerEntity;
return insertedCustomer;
}
catch (StorageException e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
throw;
}
}
示例13: ClearSagaIndex
static async Task ClearSagaIndex(ISagaData sagaData, CloudTable table)
{
var partitionKey = sagaData.GetType().Name;
var op = TableOperation.Retrieve<DynamicTableEntity>(partitionKey, $"{sagaData.Id:N}_{sagaData.Revision:0000000000}");
var operationContext = new OperationContext();
var tableRequestOptions = new TableRequestOptions { RetryPolicy = new ExponentialRetry() };
var res = await table.ExecuteAsync(op, tableRequestOptions, operationContext);
if (res != null && res.Result != null)
{
var index = (DynamicTableEntity)res.Result;
var entries = GetIndicies(index, partitionKey);
foreach (var e in entries)
{
await table.ExecuteAsync(TableOperation.Delete(e), tableRequestOptions, operationContext);
}
await table.ExecuteAsync(TableOperation.Delete(index), tableRequestOptions, operationContext);
}
}
示例14: InsertOrMergeEntityAsync
private async Task<PersonEntity> InsertOrMergeEntityAsync(CloudTable table, PersonEntity entity)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(entity);
TableResult result = await table.ExecuteAsync(insertOrMergeOperation);
PersonEntity insertedCustomer = result.Result as PersonEntity;
return insertedCustomer;
}
示例15: Backup
private static async Task Backup(dynamic u, CloudTable table)
{
try
{
if (u.Value.messages.Value != "")
{
var origin = new List<dynamic>();
foreach (var s in u.Value.messages)
{
origin.Add(s);
}
var sorted = origin.OrderBy(o => o.Value.timestamp).ToList();
if (sorted.Count <= RecordRemained) return;
for (int i = 0; i < sorted.Count - RecordRemained; i++)
{
var s = sorted[i];
TableChat c = new TableChat(u.Value.room.ID.ToString(), s.Name.ToString())
{
roomName = u.Value.room.Name,
timestamp = s.Value.timestamp,
user = s.Value.user,
uid = s.Value.uid,
message = s.Value.message
};
TableOperation insertOperation = TableOperation.Insert(c);
// Execute the insert operation.
var res = await table.ExecuteAsync(insertOperation);
if (res.HttpStatusCode == 204)
{
string url = "ChatRoom/" + u.Name + "/messages/" + s.Name;
await _firebaseClient.DeleteAsync(url);
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}