C# AzureFunction从表中读取

C# AzureFunction从表中读取,c#,azure,azure-functions,azure-storage,azure-table-storage,C#,Azure,Azure Functions,Azure Storage,Azure Table Storage,正在尝试编写一个函数,该函数将项目从函数插入Azure表(工作)。 我想捕获项目已经存在的场景-因此我想查询/读取/执行表,如果找不到项目,则插入: public static async Task<IActionResult> Run(HttpRequest req, ILogger log, IAsyncCollector<StripeHookResponse> outputTable, IAsyncCollector<string> outputQueu

正在尝试编写一个函数,该函数将项目从函数插入Azure表(工作)。 我想捕获项目已经存在的场景-因此我想查询/读取/执行表,如果找不到项目,则插入:

public static async Task<IActionResult> Run(HttpRequest req, ILogger log, IAsyncCollector<StripeHookResponse> outputTable, IAsyncCollector<string> outputQueueItem)
{

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

    try{

        if(WHAT_GOES_HERE)
        {
            await outputTable.AddAsync(MyItem); 
        }
    }
}
公共静态异步任务运行(HttpRequest请求、ILogger日志、IAsyncCollector输出、IAsyncCollector输出队列项)
{
string requestBody=等待新的StreamReader(req.Body).ReadToEndAsync();
试一试{
如果(这里是什么)
{
wait outputTable.AddAsync(MyItem);
}
}
}
我试过了 可输出。执行 TableOperation.Retrieve 但是什么都不起作用,门户工具中的intellisense是垃圾


由于插入是异步的,因此无法在Exception()块中捕获该插入。有什么想法吗?

使用
CloudTable
方法参数获取对表的引用,然后可以对该表执行查询和操作:

public class MyItem : TableEntity
{
}

public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req,
    [Table("AzureWebJobsHostLogsCommon")] CloudTable cloudTable,
    ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    MyItem myItem = JsonConvert.DeserializeObject<MyItem>(requestBody);

    // query the table - here I have used the partition key but you could replace "PartitionKey" with any column in your table
    TableQuery<MyItem> query = new TableQuery<MyItem>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, myItem.PartitionKey));
    IEnumerable<MyItem> entities = await cloudTable.ExecuteQuerySegmentedAsync(query, null);

    // if all items have a unique partition key and the item exists 
    // you should only get one item returned, if not it will be null (default)
    MyItem existingMyItem = entities.FirstOrDefault();

    // if the item is null, you want to insert it into the table
    if (existingMyItem == null)
    {
        // create an InsertOperation for your new item
        TableOperation insertOperation = TableOperation.Insert(myItem);
        await cloudTable.ExecuteAsync(insertOperation);
    }

    return new OkObjectResult("");
}
公共类MyItem:TableEntity
{
}
公共静态异步任务运行(
[HttpTrigger(AuthorizationLevel.Function,“get”,“post”,Route=null)]HttpRequestMessage请求,
[表(“AzureWebJobsHostLogsCommon”)]CloudTable,
ILogger日志)
{
LogInformation(“C#HTTP触发器函数处理了一个请求。”);
string requestBody=等待新的StreamReader(req.Body).ReadToEndAsync();
MyItem MyItem=JsonConvert.DeserializeObject(requestBody);
//查询表-这里我使用了分区键,但是您可以用表中的任何列替换“PartitionKey”
TableQuery query=new TableQuery().Where(TableQuery.GenerateFilterCondition(“PartitionKey”,QueryComparisons.Equal,myItem.PartitionKey));
IEnumerable entities=await cloudTable.ExecuteQuerySegmentedAsync(查询,null);
//如果所有项都具有唯一的分区键且该项存在
//您应该只返回一个项目,否则将为null(默认值)
MyItem existingMyItem=entities.FirstOrDefault();
//如果该项为空,则希望将其插入表中
如果(existingMyItem==null)
{
//为新项目创建插入操作
TableOperation insertOperation=TableOperation.Insert(myItem);
等待cloudTable.ExecuteAsync(插入操作);
}
返回新的OkObjectResult(“”);
}

编辑:我刚刚重读了这个问题,看到您正在使用门户,所以我假设是C#脚本。请参见此处的示例-逻辑将是相同的。

因为它是异步的,所以无法在Exception()块中捕获插入。嗯,你这是什么意思?因为如果我捕获(异常ex{}它永远不会捕获,因为它不是顺序操作。它应该。Try/catch around完全支持异步调用。您可以编辑您的问题并描述您所说的
是什么意思吗?我已经尝试了可输出。执行TableOperation。检索,但没有任何效果
?您好,您正在尝试从azure表存储读取数据吗?