且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Azure函数删除CosmosDB条目

更新时间:2023-02-14 08:31:05

您需要在Http触发器中创建名为"function.proj"的文件。

内容:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netcoreapp3.0</TargetFramework>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.12.0" />
    </ItemGroup>
</Project>

创建步骤:

1.切换到经典体验 2.创建文件

下面是我的测试代码,运行正常:

#r "Newtonsoft.Json"
#r "Microsoft.Azure.DocumentDB.Core"

using System.Net;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using System;
using System.Threading.Tasks;

private static DocumentClient client;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    client = new DocumentClient(new Uri("endpointUrl"), "authorizationKey");
    ResourceResponse<Document> response = await client.DeleteDocumentAsync(
        UriFactory.CreateDocumentUri("databaseName", "collectionName", "id"),
        new RequestOptions { PartitionKey = new PartitionKey("partition-key-value") });

    log.LogInformation("Request charge of delete operation: {0}", response.RequestCharge);
    log.LogInformation("StatusCode of operation: {0}", response.StatusCode);


    return new OkResult();    
}

顺便说一下,您可以找到文档here