我有一个类似于azure的函数:
[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:执行函数时发生异常: 添加材料。 Microsoft.Azure.WebJobs.Host:异常绑定参数 '命令'。 System.Private.CoreLib:没有无参数构造函数 为此对象定义。
当我添加无参数的构造函数(为什么需要这样做吗?)时,它会因以下错误而失败:
已执行“ AddMaterial”(失败, Id = 973cd363-19d6-49a3-a2eb-759f30c284bb)[9/12/2018 9:01:27 PM] System.Private.CoreLib:执行函数时发生异常: 添加材料。 Microsoft.Azure.WebJobs.Host:异常绑定参数 '命令'。 System.Private.CoreLib:从'System.String'强制转换 到“ System.Guid ”。
这是怎么回事?
我的最佳猜测是请求的主体未得到读取,并且空值引发了无效的强制转换异常。对于为什么我需要一个无参数的构造函数,我仍然一无所知。在使用[FromBody]
绑定时,在转移到Azure函数之前没有这个问题,但是我认为我无法将绑定与Azure函数一起使用。
答案 0 :(得分:1)
我最终只是用httpRequestMessage替换了自定义类,并在函数中创建了这样的命令
dynamic command = await req.Content.ReadAsAsync<AddMaterialCommand>();
还是想只使用自定义类作为函数的参数,但是哦。