【问题标题】:Azure Function - Cosmos DB Delete Document IssueAzure 函数 - Cosmos DB 删除文档问题
【发布时间】:2018-06-20 17:29:33
【问题描述】:

我是 Azure 新手,我正在尝试使用 C# 创建一个带有 Azure 函数和 Cosmos DB 的简单 CRUD Web 应用程序。到目前为止,我已经成功实现了两个功能,一个是将文档写入数据库,另一个是从数据库中读取所有文档。我还实现了一个删除文档的功能,但我无法让它工作,即使使用 Azure 门户的功能测试实用程序也是如此。调用 DeleteDocumentAsync 会出现问题,这会导致引发异常。

完整的 run.csx 代码(使用硬编码文档 SelfLink 进行了简化)如下所示:

#r "Microsoft.Azure.Documents.Client"
using System.Net;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;

private static bool success;

public static HttpResponseMessage Run(HttpRequestMessage req, out object deletionDocument, TraceWriter log)
{
    string endpointUrl = "https://blahblah.documents.azure.com:443/"; // ** Copied from 'URI' in Read-Write Keys screen.
    string authorizationKey = "blahblahblah"; // ** Copied from 'PRIMARY KEY' in Read-Write Keys screen.
    ConnectionPolicy connectionPolicy = new ConnectionPolicy();
    connectionPolicy.RetryOptions.MaxRetryAttemptsOnThrottledRequests = 3;
    connectionPolicy.RetryOptions.MaxRetryWaitTimeInSeconds = 60;
    connectionPolicy.RequestTimeout = new TimeSpan(0, 0, 30);
    deletionDocument = null;

    using (DocumentClient client = new DocumentClient(new Uri(endpointUrl), authorizationKey, connectionPolicy))
    {
        success = true;
        Task t = DeleteDocument(client, log);
    }

    return success
        ? req.CreateResponse(HttpStatusCode.OK, "Deletion succeeded")
        : req.CreateResponse(HttpStatusCode.BadRequest, "Deletion failed");
}

private static async Task DeleteDocument(DocumentClient client, TraceWriter log)
{
    try
    {
        await client.DeleteDocumentAsync("dbs/p3wOAA==/colls/p3wOAPsBIwA=/docs/p3wOAPsBIwAEAAAAAAAAAA==/");
    }
    catch (Exception ex)
    {
        success = false;
        log.Info("ex: " + ex.StackTrace);
    }
}

function.json文件如下图:

{
  "bindings": [
    {
      "authLevel": "anonymous",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "delete"
      ]
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "documentDB",
      "name": "deletionDocument",
      "databaseName": "taskDatabase",
      "collectionName": "MsgCollection",
      "createIfNotExists": false,
      "connection": "apw-messages-id_DOCUMENTDB",
      "direction": "out"
    }
  ],
  "disabled": false
}

异常的堆栈跟踪如下所示。堆栈跟踪的顶部是指“GenerateKeyAuthorizationSignature”。根本原因是权限问题吗?对于解决此问题,我将不胜感激。

在 Microsoft.Azure.Documents.AuthorizationHelper.GenerateKeyAuthorizationSignature(字符串 动词、Uri uri、NameValueCollection 标头、IComputeHash stringHMACSHA256Helper,字符串 clientVersion)在 Microsoft.Azure.Documents.Client.GatewayServiceConfigurationReader.d__0.MoveNext()--- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.Azure.Documents.Routing.GlobalEndpointManager.d__0.MoveNext()--- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.Azure.Documents.Client.GatewayServiceConfigurationReader.d__b.MoveNext()--- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.Azure.Documents.Client.DocumentClient.d__35d.MoveNext()--- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.Azure.Documents.Client.DocumentClient.d__29.MoveNext()--- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.Azure.Documents.Client.DocumentClient.d__44.MoveNext()--- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.Azure.Documents.Client.DocumentClient.d__cf.MoveNext()--- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.Azure.Documents.BackoffRetryUtility1.<>c__DisplayClass2.<<ExecuteAsync>b__0>d__4.MoveNext()--- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.Documents.BackoffRetryUtility1.d__1b.MoveNext()--- 从先前抛出异常的位置结束堆栈跟踪 --- 在 Microsoft.Azure.Documents.BackoffRetryUtility1.<ExecuteRetry>d__1b.MoveNext()--- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.Documents.BackoffRetryUtility1.d__a.MoveNext()--- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Submission#0.d__3.MoveNext() 中 D:\home\blah\run.csx:第 34 行

【问题讨论】:

    标签: c# azure azure-cosmosdb


    【解决方案1】:

    首先,在使用 Azure Functions 时,保持 DocumentClient 静态,以便在执行之间共享实例,这是一种性能改进。

    考虑到这一点,您可以创建这个函数:

    #r "Microsoft.Azure.Documents.Client"
    using Microsoft.Azure.Documents;
    using Microsoft.Azure.Documents.Client;
    using System.Net;
    
    private static string endpointUrl = "https://blahblah.documents.azure.com:443/"; // ** Copied from 'URI' in Read-Write Keys screen.
    private static string authorizationKey = "blahblahblah"; // ** Copied from 'PRIMARY KEY' in Read-Write Keys screen.
    private static DocumentClient client = new DocumentClient(new Uri(endpointUrl), authorizationKey, new ConnectionPolicy() {
        RequestTimeout = new TimeSpan(0, 0, 30),
        RetryOptions = new RetryOptions() {
            MaxRetryAttemptsOnThrottledRequests = 3,
            MaxRetryWaitTimeInSeconds = 60
        }
    });
    
    public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
    {
        bool success = true;
    
        try {
            await client.DeleteDocumentAsync("dbs/p3wOAA==/colls/p3wOAPsBIwA=/docs/p3wOAPsBIwAEAAAAAAAAAA==/");
            // or you could use the UriFactory if you have the document id
            //Uri documentUri = UriFactory.CreateDocumentUri("name of database","name of collection","document id");
            //await client.DeleteDocumentAsync(documentUri);
        }
        catch(Exception ex){
            success = false;
            log.Info("ex: " + ex.StackTrace);
        }
    
        return success
            ? req.CreateResponse(HttpStatusCode.OK, "Deletion succeeded")
            : req.CreateResponse(HttpStatusCode.BadRequest, "Deletion failed");
    }
    

    functions.json:

    {
      "bindings": [
        {
          "authLevel": "anonymous",
          "name": "req",
          "type": "httpTrigger",
          "direction": "in",
          "methods": [
            "delete"
          ]
        },
        {
          "name": "$return",
          "type": "http",
          "direction": "out"
        }
      ],
      "disabled": false
    }
    

    【讨论】:

    • 谢谢,Matias,您的建议奏效了。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多