繁体   English   中英

Azure 函数返回 500 内部服务器错误

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM