Azure EventGrid验证使用OkObjectResult失败

brc7rcf0  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(135)

我正在尝试使用Microsoft found here中的示例对EventGrid进行同步握手验证

public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestData OriginalRequest)
{
    Log.LogInformation("HTTP Request Received");
    BinaryData RawEvents = BinaryData.FromStream(OriginalRequest.Body);
    EventGridEvent[] EventGridEvents = EventGridEvent.ParseMany(RawEvents);

    foreach (EventGridEvent Event in EventGridEvents) {
        if (Event.TryGetSystemEventData(out object EventData)) {
            if (EventData is SubscriptionValidationEventData ValidationEventData) {

                var ResponseData = new {
                    ValidationResponse = ValidationEventData.ValidationCode
                };
                Log.LogInformation("Validation Complete");
                return new OkObjectResult(ResponseData);
            }
            else {
                RelayEvent(OriginalRequest);
            }
        }
    }
    return new OkObjectResult(string.Empty);

这是我应该得到的结果:

{
  "ValidationResponse": "512d38b6-c7b8-40c8-89fe-f46f9e9622b6"
}

这就是我得到的结果:

{
  "Value": {
    "ValidationResponse": "512d38b6-c7b8-40c8-89fe-f46f9e9622b6"
  },
  "Formatters": [],
  "ContentTypes": [],
  "DeclaredType": null,
  "StatusCode": 200
}

我认识到这只是序列化的OkObjectResult,但这似乎与Microsoft示例声称我应该得到的内容不符。我不认为我错过了什么,我甚至尝试让我的代码异步匹配的例子,但这并没有改变什么,当然。
我如何得到微软得到的结果,为什么会有差异?

hjzp0vay

hjzp0vay1#

原来这是我的误会。Microsoft文档中的示例是一个完整的MVC应用程序,OkObjectResult是一个MVC类。Azure Functions不是MVC应用程序,因此正确的设计是返回HttpResponseData

相关问题