微软.Azure.EventGrid.Models.EventGridEvent无法通过控制器动作反序列化。

我正在编写一个Controller Action来接受来自Azure EventGrid中webhook的Post请求。文档中的示例将HTTP请求中的json手动解析为Microsoft.Azure.EventGrid.Models.EventGridEvent对象。https:/docs.microsoft.comen-usazureevent-gridreceive-events

这对我来说似乎有点复杂,我想我应该能够让.Net为我反序列化Json。我的理解是,.Net应该将JSON对象反序列化为action的预期对象。

我的Action如下。

    [HttpPost]
    public async Task<OkObjectResult> Post(EventGridEvent gridEvent)
    {
        var response = string.Empty;
        if (gridEvent.EventType == EventTypes.EventGridSubscriptionValidationEvent)
        {
            // Validate
            var eventData = (SubscriptionValidationEventData) gridEvent.Data;

            var responseData = new SubscriptionValidationResponse()
            {
                ValidationResponse = eventData.ValidationCode
            };

            return new OkObjectResult(responseData);
        }

        if (gridEvent.EventType == EventTypes.StorageBlobCreatedEvent)
        {
            var eventData = (StorageBlobCreatedEventData) gridEvent.Data;
            // Get File

            // Validate File

            // Dispatch Events
        }

        return new OkObjectResult(response);
    }

我通过postman向Action发送了一个Post请求,并使用了文档中的一个示例体。

[{
  "topic": "/subscriptions/{subscription-id}/resourceGroups/Storage/providers/Microsoft.Storage/storageAccounts/xstoretestaccount",
  "subject": "/blobServices/default/containers/testcontainer/blobs/testfile.txt",
  "eventType": "Microsoft.Storage.BlobCreated",
  "eventTime": "2017-06-26T18:41:00.9584103Z",
  "id": "831e1650-001e-001b-66ab-eeb76e069631",
  "data": {
    "api": "PutBlockList",
    "clientRequestId": "6d79dbfb-0e37-4fc4-981f-442c9ca65760",
    "requestId": "831e1650-001e-001b-66ab-eeb76e000000",
    "eTag": "0x8D4BCC2E4835CD0",
    "contentType": "text/plain",
    "contentLength": 524288,
    "blobType": "BlockBlob",
    "url": "https://example.blob.core.windows.net/testcontainer/testfile.txt",
    "sequencer": "00000000000004420000000000028963",
    "storageDiagnostics": {
      "batchId": "b68529f3-68cd-4744-baa4-3c0498ec19f0"
    }
  },
  "dataVersion": "",
  "metadataVersion": "1"
}]

我检查了EventGridEvent对象的样子,一切看起来都是正确的,而且json映射到了预期的对象。

public class EventGridEvent
{
    public EventGridEvent()
    {
    }

    public EventGridEvent(
      string id,
      string subject,
      object data,
      string eventType,
      DateTime eventTime,
      string dataVersion,
      string topic = null,
      string metadataVersion = null)
    {
      ...
    }

    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "topic")]
    public string Topic { get; set; }

    [JsonProperty(PropertyName = "subject")]
    public string Subject { get; set; }

    [JsonProperty(PropertyName = "data")]
    public object Data { get; set; }

    [JsonProperty(PropertyName = "eventType")]
    public string EventType { get; set; }

    [JsonProperty(PropertyName = "eventTime")]
    public DateTime EventTime { get; set; }

    [JsonProperty(PropertyName = "metadataVersion")]
    public string MetadataVersion { get; private set; }

    [JsonProperty(PropertyName = "dataVersion")]
    public string DataVersion { get; set; }

    public virtual void Validate()
    {
       ...
    }
  }

对我来说,唯一反序列化的属性是eventTime,其他的都是空的。

解决方案:

这最终是一个简单的问题。我忘了在预期类型之前添加[FromBody]。

所以我的控制器动作变成了

[HttpPost]
public async Task<OkObjectResult> Post([FromBody]List<EventGridEvent> request)
{
  ...
}

给TA打赏
共{{data.count}}人
人已打赏
未分类

Django应用部署到多个服务器时,如何管理Django应用的collectstatic?

2022-11-1 4:02:25

未分类

哨兵在Flutter内没有报告错误。

2022-11-1 4:02:27

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索