简体   繁体   中英

Azure Function returns 500 internal server error

I am trying to work an exercise from the book "Mastering Xamarin.Forms" 3rd edition. I have followed the instructions in the book to add a function app through the portal.

The run.csx file looks like this:

#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:

{
  "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. Both GET and POST return 500 internal server error. 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)...

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
Table parameter name: entryTableInput
Table name: entry
Storage account connection: AzureWebJobsStorage

Outputs
Binding Type: Azure Table Storage
Table parameter name: entryTableOutput
Table name: entry
Storage account connection: 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 .

You should then find that you can use Postman to test the API as per the instructions in the book.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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