[英]Azure Function - Cosmos DB Delete Document Issue
我是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.Client.GatewayServiceConfigurationReader.d__0.MoveNext()-從堆棧結束跟蹤引發異常的先前位置---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)在Microsoft.Azure.Documents.Routing.GlobalEndpointManager.d__0。 MoveNext()---從上一個引發異常的位置開始的堆棧結束跟蹤--- Microsoft.System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)處System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務) .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(Task任務)處的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)。 Documents.Client.DocumentClient.d__29.MoveNext()---從上一個引發異常的位置開始的堆棧結束跟蹤---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)在System.Runtime.CompilerServices.TaskAwaiter Microsoft.Azure.Documents.Client.DocumentClient.d__44.MoveNext()上的.HandleNonSuccessAndDebuggerNotification(任務任務)-從上一個引發異常的位置開始的堆棧結束跟蹤-在System.Runtime.Compil System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)的erServices.TaskAwaiter.ThrowForNonSuccess(任務任務),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()---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
處從引發異常的先前位置開始的堆棧結束跟蹤。 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.BackoffRetryUtility
1.d__a.MoveNext()上的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
-從上一個引發異常的位置開始的堆棧結束跟蹤-在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task任務)在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)在Submission#0.d__3.MoveNext()在D:\\ home \\ blah \\ run.csx:line 34
首先,使用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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.