New-AzEventGridSubscription "The attempt to validate the provided endpoint" 失败

New-AzEventGridSubscription "The attempt to validate the provided endpoint" failed

我在 /events 路径的 C# 代码中创建了一个端点。如果事件的类型为 "Microsoft.EventGrid.SubscriptionValidationEvent",我将使用带有 validationResponse 的 json 对象提取 ValidationCode 和 return 200。我使用 Postman 对此进行了测试,似乎一切正常,但是当使用 "New-AzEventGridSubscription" 时,它失败了。有什么想法吗?

这是我使用的命令,当然是擦洗了。

New-AzEventGridSubscription -ResourceGroup MyResourceGroupName -Endpoint https://requestb.in/19qlscd1/events -EventSubscriptionName EventSubscription1 -TopicName Topic1

样本Post:

[
{
"id": "531d4a96-d4c7-43d6-8b4c-e1ff9351b869",
"topic": "scrubbed",
"subject": "",
"data": {
"validationCode": "7062AAC0-656D-4C4C-BDD6-0FA673676D95",
"validationUrl": "scrubbed"
},
"eventType": "Microsoft.EventGrid.SubscriptionValidationEvent",
"eventTime": "2020-03-30T17:46:15.1827678Z",
"metadataVersion": "1",
"dataVersion": "2"
}
]

响应示例:

{
"validationResponse": "7062AAC0-656D-4C4C-BDD6-0FA673676D95"
}

这是我处理事件的代码。我已经使用 Ngrok 对此进行了测试,它似乎有效。

private void HandleEvent(IApplicationBuilder app) 
        { 
            app.Run(async context => 
            { 
                string content; 
                using (var reader = new StreamReader(context.Request.Body)) 
                { 
                    content = reader.ReadToEnd(); 
                } 

                var eventGridEvents = JsonConvert.DeserializeObject<EventGridEvent[]>(content); 

                foreach (var eventGridEvent in eventGridEvents) 
                { 
                    if (eventGridEvent.EventType == "Microsoft.EventGrid.SubscriptionValidationEvent") 
                    { 
                        var eventDataJson = JsonConvert.SerializeObject(eventGridEvent.Data); 
                        var eventData = JsonConvert.DeserializeObject<SubscriptionValidationEventData>(eventDataJson); 

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

                        context.Response.StatusCode = 200; 
                        await context.Response.WriteAsync(JsonConvert.SerializeObject(responseData)); 
                    } 
                    else 
                    { 
                        var deserialized = JsonConvert.DeserializeObject((string)eventGridEvent.Data); 
                        if (typeof(IEnumerable).IsAssignableFrom(deserialized.GetType())) 
                        { 
                            eventGridEvent.Data = (IEnumerable<object>)deserialized; 
                        } 
                        else 
                        { 
                            eventGridEvent.Data = new List<object>() { deserialized }; 
                        } 

                        await HandleMessageListAsync(eventGridEvents.ToList()); 
                    } 
                } 

                context.Response.StatusCode = 200; 
                await context.Response.WriteAsync(string.Empty); 
            }); 
        } 

谢谢!

原来问题是我的 API 被返回为 "Chunked"。 Microsoft 应用了一个补丁来支持 "Chunked" 响应,现在它可以工作了。 :)