Azure功能 - Cosmos DB删除文档问题

时间:2018-01-11 11:37:18

标签: c# azure azure-cosmosdb

我是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
}

exeption的堆栈跟踪如下所示。堆栈跟踪的顶部是指' GenerateKeyAuthorizationSignature'。根本原因是权限问题吗?我很感激任何帮助解决这个问题。

  

在   Microsoft.Azure.Documents.AuthorizationHelper.GenerateKeyAuthorizationSignature(字符串   动词,Uri uri,NameValueCollection标题,IComputeHash   stringHMACSHA256Helper,String clientVersion)at   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.BackoffRetryUtility 1.<>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.BackoffRetryUtility 1.d__1b.MoveNext()---   从抛出异常的先前位置开始的堆栈跟踪结束   ---在Microsoft.Azure.Documents.BackoffRetryUtility 1.<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.BackoffRetryUtility 1.d__a.MoveNext()---   从抛出异常的先前位置开始的堆栈跟踪结束   ---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)在提交#0.d__3.MoveNext()中   D:\ home \ blah \ run.csx:第34行

1 个答案:

答案 0 :(得分:2)

首先,在使用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
}