Azure Function Custom Class request body - No Parameter-less constructor/Invalid Cast string -> guid

Viewed 2058

I have an azure function that looks something like:

    [FunctionName("AddMaterial")]
    public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]AddMaterialCommand command, 
        ILogger log, [Inject(typeof(IMediator))]IMediator mediator)
    {
        log.LogInformation("AddMaterial Function is processing a request");

        var events = await mediator.Send(command);
        if (events != null)
        {
            await mediator.Publish(events);
            return (ActionResult)new OkObjectResult(events);
        }
        return new BadRequestObjectResult(new { message = "Please check that WarehouseId, RollPoNumber, RollNumber, Location and RollWeight are included in request" });
    }

This function uses the custom object AddMaterialCommand as the request per the docs.

The custom object class looks something like this:

{
    [DataContract]
    public class AddMaterialCommand : IRequest<EventList>
    {
        [DataMember]
        public Guid WarehouseId { get; set; } 
        [DataMember]
        public int RollPoNumber { get; set; }
        [DataMember]
        public DateTime? DateRecieved { get; set; }

        public AddMaterialCommand(Guid warehouseId, int rollPoNumber,  DateTime dateRecieved)
        {
            WarehouseId = warehouseId;
            RollPoNumber = rollPoNumber;
            Location = location;
            DateRecieved = dateRecieved;
        }
}

When posting to the function it throws this error:

Executed 'AddMaterial' (Failed, Id=d7322061-c972-4e93-83cd-4d0313d26e86) [9/12/2018 8:59:46 PM] System.Private.CoreLib: Exception while executing function: AddMaterial. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'command'. System.Private.CoreLib: No parameterless constructor defined for this object.

When I add a parameterless constructor (why do I need to do this?), it then fails with this error:

Executed 'AddMaterial' (Failed, Id=973cd363-19d6-49a3-a2eb-759f30c284bb) [9/12/2018 9:01:27 PM] System.Private.CoreLib: Exception while executing function: AddMaterial. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'command'. System.Private.CoreLib: Invalid cast from 'System.String' to 'System.Guid'.

What is going on here?

My best guess is that the body of the request is not getting read and that an empty value is throwing the invalid cast exception. I'm still clueless as to why I need a paramaterless constructor. I didn't have this issue before moving to azure functions when I was using the [FromBody] binding, but I don't think I can use that binding with azure functions.

1 Answers

I ended up just replacing the custom class with an httpRequestMessage and creating the command in the function like so

dynamic command = await req.Content.ReadAsAsync<AddMaterialCommand>();

Still would have like to just use custom class as a parameter to the function but oh well.

Related