我是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' . 根本原因是权限问题吗?我很感激任何帮助解决这个问题 .
at Microsoft.Azure.Documents.AuthorizationHelper.GenerateKeyAuthorizationSignature(String verb,Uri uri,NameValueCollection headers,IComputeHash stringHMACSHA256Helper,String clientVersion)at Microsoft.Azure.Documents.Client.GatewayServiceConfigurationReader.d__0.MoveNext()---堆栈跟踪结束从抛出异常的先前位置---在Microsoft.Azure.Documents.Routing.GlobalEndpointManager.d__0的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)中.MoveNext()---抛出异常的前一个位置的堆栈跟踪结束---在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)处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()处抛出了... ---在抛出异常的前一个位置的堆栈跟踪结束---在Microsoft.Azure的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)中 . Documents.Client.DocumentClient.d__29.MoveNext()---抛出异常的上一个位置的堆栈跟踪结束---在System.Runtime.CompilerServices.TaskAwaiter的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)处Microsoft.Azure.Documents.Client.DocumentClient.d__44.MoveNext()中的.HandleNonSuccessAndDebuggerNotification(任务任务)---从抛出异常的上一个位置开始的堆栈跟踪结束---在System.Runtime.Comp位于Microsoft.Azure.Documents.Client.DocumentClient.d__cf.MoveNext()的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中的ilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)---来自上一个位置的堆栈跟踪结束异常被抛出---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)处于System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务),位于Microsoft.Azure.Documents.BackoffRetryUtility1 . <> c__DisplayClass2 . << ExecuteAsync > b__0> d__4.MoveNext()---在抛出异常的前一个位置的堆栈跟踪结束---在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)处( Microsoft.Azure.Documents.BackoffRetryUtility1.d__1b.MoveNext()中的任务任务---从抛出异常的上一个位置的堆栈跟踪结束---在Microsoft.Azure.Documents . BackoffRetryUtility1 . <ExecuteRetry> d__1b.MoveNext()---从抛出异常的先前位置开始的堆栈跟踪结束---在System.Runtime.CompilerServices.TaskAwaiter的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)处 . Microsoft.Azure.Documents.BackoffRetryUtility1.d__a.MoveNext()中的HandleNonSuccessAndDebuggerNotification(任务任务)---抛出异常的前一个位置的堆栈跟踪结束---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)在D:\ home \ blah \ run.csx:第34行中的提交#0.d__3.MoveNext()的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)
1 回答
首先,在使用Azure功能时,请保持
DocumentClient
静态,以便在执行期间共享实例,这是性能改进 .考虑到这一点,您可以创建此功能:
functions.json
: