简体   繁体   English

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

[英]Azure Function returns 500 internal server error

I am trying to work an exercise from the book "Mastering Xamarin.Forms" 3rd edition.我正在尝试从“掌握 Xamarin.Forms”第三版一书中做一个练习。 I have followed the instructions in the book to add a function app through the portal.我已按照书中的说明通过门户添加功能应用程序。

The run.csx file looks like this: 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;
}

The function.json:函数.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
}

I'm using Postman to test the request.我正在使用 Postman 来测试请求。 Both GET and POST return 500 internal server error. GET 和 POST 都返回 500 内部服务器错误。 I don't know what to do next.我不知道接下来要做什么。

I don't know if you found the answer you were looking for, but, in case anyone else comes across this post in future (like I did when I had the same error, and not having used Azure before)...我不知道您是否找到了您正在寻找的答案,但是,以防其他人将来看到这篇文章(就像我遇到相同错误时所做的那样,并且之前没有使用过 Azure)...

After you have created the Function ( entry as per the code listed above) you need to select the Integration option and specify the following:创建函数(根据上面列出的代码输入)后,您需要选择集成选项并指定以下内容:

Inputs输入
Binding Type: Azure Table Storage绑定类型: Azure 表存储
Table parameter name: entryTableInput表参数名称: entryTableInput
Table name: entry表名:条目
Storage account connection: AzureWebJobsStorage存储帐户连接: AzureWebJobsStorage

Outputs输出
Binding Type: Azure Table Storage绑定类型: Azure 表存储
Table parameter name: entryTableOutput表参数名称: entryTableOutput
Table name: entry表名:条目
Storage account connection: AzureWebJobsStorage存储帐户连接: AzureWebJobsStorage

Also, go to the Storage Account that is created as part of setting the Azure function up, and under Tables , create a blank table called entry .此外,转到作为设置 Azure 功能的一部分而创建的存储帐户,然后在Tables下创建一个名为entry的空白表。

You should then find that you can use Postman to test the API as per the instructions in the book.然后你应该会发现你可以按照书中的说明使用 Postman 来测试 API。

I'd first try running the function locally and setting breakpoints within the function.我首先尝试在本地运行该函数并在该函数内设置断点。 You can then use Postman to trigger the function, step through with the debugger, and see what the problem is.然后,您可以使用 Postman 来触发该功能,通过调试器逐步执行,并查看问题所在。 When deploying an Azure Function to Azure, I'd also suggest enabling Application Insights, which you can use to see the details of exceptions that are thrown.在将 Azure 函数部署到 Azure 时,我还建议启用 Application Insights,您可以使用它来查看抛出的异常的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure 存储模拟器返回 500 内部服务器错误 - Azure Storage Emulator returns 500 internal server error 500-天蓝色的内部服务器错误 - 500 - Internal server error in azure Azure 函数的 http 触发器给出 500 内部服务器错误 - Azure function's http trigger is giving 500 internal server error Azure Function 状态代码 500 内部服务器错误 - Azure Function Status Code 500 internal server error 500(内部服务器错误):Forge 部署在 Azure 上 - 500(Internal Server Error): Forge deployed on Azure Azure 500内部服务器中的httphandlers错误 - httphandlers in azure 500 internal server error Azure Rest Api建议ElasticPools始终返回内部服务器错误500,是否有人面临此问题? - Azure Rest Api recommendedElasticPools always returns Internal Server Error 500, Is there anyone facing this issue? 尝试调用包装在 Docker 容器中的 Azure Function 时出现内部服务器错误 (500) - Internal Server Error (500) when trying to invoke a Azure Function wrapped in a Docker container 预编译的Azure函数输出到Blob存储,收到500个内部服务器错误 - Precompiled Azure Function Output to Blob Storage receiving 500 Internal Server Error 当“代码”是查询字符串中的参数时,为什么匿名 httptrigger azure 函数会抛出 500 内部服务器错误? - Why does an anonymous httptrigger azure function throw a 500 internal server error when 'code' is a param in query string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM