2

I am running an isolated process function that is failing to bind the input model from the JSON payload.

This is my function:

[Function("AddChip")]
public async Task<HttpResponseData> AddChip([HttpTrigger(AuthorizationLevel.Anonymous,
 "post", Route = "chip")] Chip chip, HttpRequestData req, FunctionContext executionContext)
{
            
    var logger = executionContext.GetLogger("Function1");
    logger.LogInformation("C# HTTP trigger function processed a request.");

    var result = await _chipService.UpsertChipAsync(chip);

    var response = req.CreateResponse(HttpStatusCode.OK);
    await response.WriteAsJsonAsync(result);
    return response;
}

And this is my Chip class:

public class Chip
{
    public string Id {  get; set; }
    public double Balance { get; set; }
    public DateTime Created { get; set; }
    public DateTime Updated { get; set; }
}

But when I call it from Insomnia making sure the Content-Type application/json header is set with this body:

{
    "Id": "1",
    "Balance": 0,
    "Created": "2001-01-01T00:00:00",
    "Updated": "2001-01-01T00:00:00"
}

I get an error message saying that the chip parameter could not be converted:

Microsoft.Azure.Functions.Worker.Diagnostics.Exceptions.FunctionInputConverterException: Error converting 1 input parameters for Function 'AddChip': Cannot convert input parameter 'chip' to type 'Models.Chip' from type 'Microsoft.Azure.Functions.Worker.GrpcHttpRequestData'.

I have tried unsuccessfully:

  • Removing the HttpRequestData parameter,
  • Different classes and payloads,
  • Different AuthorizationLevel

It seems related to gRPC somehow as the error references GrpcHttpRequestData.

Different sources like Microsoft documentation or this Rick van den Bosch post or even other Stack Overflow answers like Azure Functions model binding imply this should work, but it does not.

4
  • This seems annoying ++, what happens if you remove Chip chip, altogether, i am just curious ? Commented Aug 23, 2021 at 5:18
  • 1
    I assume this is Azure Functions on .NET 5? Commented Aug 23, 2021 at 6:32
  • @TheGeneral It works without Chip chip. The RequestData comes through and req.Body contains the JSON which can be deserialized correctly. Commented Aug 23, 2021 at 15:34
  • @IanKemp, Correct .NET 5 Commented Aug 23, 2021 at 15:35

2 Answers 2

2

As Stephen mentioned in the other answer, there is no fully fledged model binding capability today in the .net 5 azure function(out of process model). Your request payload is available in the Body property of the HttpRequestData instance and you may de-serialize it inside your function.

The below example uses JsonSerializer from System.Text.Json namespace.

static JsonSerializerOptions SerializerOptions = new JsonSerializerOptions
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
    PropertyNameCaseInsensitive = true
};

[Function("AddChip")]
public async Task<HttpResponseData> AddChip(
    [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "chip")] HttpRequestData req,
    FunctionContext executionContext)
{
    Chip chip = null;

    if (req.Body is not null)
    {
        chip = await JsonSerializer.DeserializeAsync<Chip>(req.Body, SerializerOptions);
    }

    var logger = executionContext.GetLogger("Function1");
    logger.LogInformation("C# HTTP trigger function processed a request for." + chip?.Id);

    var result = await _chipService.UpsertChipAsync(chip);

    var response = req.CreateResponse(HttpStatusCode.OK);
    await response.WriteAsJsonAsync(result);
    return response;
}
Sign up to request clarification or add additional context in comments.

Comments

2

Azure Functions .NET 5 uses the new out-of-proc model, which is currently very limited in terms of model binding.

In the case of HTTP requests, your method gets a HttpRequestData. Any mapping of request body or URI parameters must be done by your code.

3 Comments

The documentation of out-of-proc definitely suggests bindings should work learn.microsoft.com/en-us/azure/azure-functions/… Am I missing something or do you have any links supporting the contrary?
@AntonioDlp: That section says "For HTTP triggers, you must use HttpRequestData and HttpResponseData to access the request and response data." I expect they're planning to add support for it, but I believe it is not there yet.
Thank you, I guess I was reading it incorrectly. It's not very clear as the previous paragraph references POCOs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.