gpt4 book ai didi

.net - Azure函数自定义类请求正文 - 无无参数构造函数/无效的转换字符串 -> guid

转载 作者:行者123 更新时间:2023-12-02 02:50:49 24 4
gpt4 key购买 nike

我有一个 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;
}
}

当发布到函数时,它会抛出此错误:

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.

当我添加无参数构造函数时(为什么需要这样做?),它会失败并出现以下错误:

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

这是怎么回事?

我最好的猜测是请求的正文没有被读取,并且空值引发了无效的强制转换异常。我仍然不明白为什么我需要一个无参数的构造函数。在使用 [FromBody] 绑定(bind)时,在迁移到 azure 函数之前,我没有遇到此问题,但我认为我无法将该绑定(bind)与 azure 函数一起使用。

最佳答案

我最终只是用 httpRequestMessage 替换了自定义类,并在函数中创建了命令,如下所示

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

仍然希望仅使用自定义类作为函数的参数,但是哦,好吧。

关于.net - Azure函数自定义类请求正文 - 无无参数构造函数/无效的转换字符串 -> guid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52319281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com