我有一个天蓝色的功能,看起来像:
[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" });
}
此函数使用自定义对象AddMaterialCommand作为每个the docs的请求。
自定义对象类看起来像这样:
{
[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;
}
}
发布到该函数时,它会抛出此错误:
执行'AddMaterial'(失败,Id = d7322061-c972-4e93-83cd-4d0313d26e86)[9/12/2018 8:59:46 PM] System.Private.CoreLib:执行函数时出现异常:AddMaterial。 Microsoft.Azure.WebJobs.Host:异常绑定参数'command'。 System.Private.CoreLib:没有为此对象定义的无参数构造函数。
当我添加一个无参数构造函数(为什么我需要这样做?)时,它会因此错误而失败:
执行'AddMaterial'(失败,Id = 973cd363-19d6-49a3-a2eb-759f30c284bb)[9/12/2018 9:01:27 PM] System.Private.CoreLib:执行函数时出现异常:AddMaterial。 Microsoft.Azure.WebJobs.Host:异常绑定参数'command'。 System.Private.CoreLib:从'System.String'到'System.Guid'的无效转换。
这里发生了什么?
我最好的猜测是请求的主体没有被读取,并且空值正在抛出无效的强制转换异常。我仍然不知道为什么我需要一个无表情的构造函数。我在使用[FromBody]
绑定时转移到azure函数之前没有遇到此问题,但我认为我不能将该绑定与azure函数结合使用。
我最终只是用httpRequestMessage替换自定义类,并在函数中创建命令,就像这样
dynamic command = await req.Content.ReadAsAsync<AddMaterialCommand>();
仍然希望只使用自定义类作为函数的参数但是哦。