[英]Azure Function returns 500 internal server error
我正在嘗試從“掌握 Xamarin.Forms”第三版一書中做一個練習。 我已按照書中的說明通過門戶添加功能應用程序。
run.csx 文件如下所示:
#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using Newtonsoft.Json;
public static async Task<IActionResult> Run(HttpRequest req, Newtonsoft.Json.Linq.JArray entryTableInput, IAsyncCollector<Entry> entryTableOutput, ILogger log)
{
log.LogInformation(req.Method);
if (req.Method == "GET")
{
return (ActionResult) new OkObjectResult(entryTableInput);
}
var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var entry = JsonConvert.DeserializeObject<Entry>(requestBody);
if (entry != null)
{
await entryTableOutput.AddAsync(entry);
return (ActionResult) new OkObjectResult(entry);
}
return new BadRequestObjectResult("Invalid entry request.");
}
public class Entry
{
public string Id => Guid.NewGuid().ToString("n");
public string Title { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public DateTime Date { get; set; }
public int Rating { get; set; }
public string Notes { get; set; }
// Required for Table Storage entities
public string PartitionKey => "ENTRY";
public string RowKey => Id;
}
函數.json:
{
"bindings": [
{
"authLevel": "anonymous",
"name": "req",
"type": "httpTrigger",
"direction": "in"
},
{
"name": "$return",
"type": "http",
"direction": "out"
},
{
"type": "table",
"name": "entryTableOutput",
"tableName": "entry",
"connection": "AzureWebJobStorage",
"direction": "out"
},
{
"type": "table",
"name": "entryTableInput",
"tableName": "entry",
"take": 50,
"connection": "AzureWebJobsStorage",
"direction": "in"
}
],
"disabled": false
}
我正在使用 Postman 來測試請求。 GET 和 POST 都返回 500 內部服務器錯誤。 我不知道接下來要做什么。
我不知道您是否找到了您正在尋找的答案,但是,以防其他人將來看到這篇文章(就像我遇到相同錯誤時所做的那樣,並且之前沒有使用過 Azure)...
創建函數(根據上面列出的代碼輸入)后,您需要選擇集成選項並指定以下內容:
輸入
綁定類型: Azure 表存儲
表參數名稱: entryTableInput
表名:條目
存儲帳戶連接: AzureWebJobsStorage
輸出
綁定類型: Azure 表存儲
表參數名稱: entryTableOutput
表名:條目
存儲帳戶連接: AzureWebJobsStorage
此外,轉到作為設置 Azure 功能的一部分而創建的存儲帳戶,然后在Tables下創建一個名為entry的空白表。
然后你應該會發現你可以按照書中的說明使用 Postman 來測試 API。
我首先嘗試在本地運行該函數並在該函數內設置斷點。 然后,您可以使用 Postman 來觸發該功能,通過調試器逐步執行,並查看問題所在。 在將 Azure 函數部署到 Azure 時,我還建議啟用 Application Insights,您可以使用它來查看拋出的異常的詳細信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.