Azure 事件网格订阅 WebHook 验证因 Cloud Events Schema v1.0 失败
Azure Event Grid Subscription WebHook Validation Fails with Cloud Events Schema v1.0
尝试创建 WebHook 订阅时,部署无法验证 Azure Functions 中的 HTTP 触发器以用于 Cloud Events v1.0 Schema.I 已确认我的端点使用适当的响应处理验证握手。我的事件网格域正在使用 Cloud Events v1.0 架构。看来我的 Azure 函数在验证期间甚至没有被调用,因为函数的控制台中没有输出。
这适用于 Cloud Events v1.0 和 WebHooks 吗?
部署失败,出现以下错误:{"code":"Url validation","message":"The attempt to validate the provided endpoint xxxxx failed."}
这是我的代码:
[FunctionName("ProcessEvent")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
var requestmessage = await req.Content.ReadAsStringAsync();
var message = JToken.Parse(requestmessage);
if (message.Type == JTokenType.Array)
{
// If the request is for subscription validation, send back the validation code.
if (string.Equals((string)message[0]["eventType"],
"Microsoft.EventGrid.SubscriptionValidationEvent",
System.StringComparison.OrdinalIgnoreCase))
{
log.LogInformation("Validate request received");
var myObj = new { validationResponse = message[0]["data"]["validationCode"].ToString() };
var jsonToReturn = JsonConvert.SerializeObject(myObj);
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(jsonToReturn, Encoding.UTF8, "application/json")
};
}
}
else
{
// The request is not for subscription validation, so it's for an event.
// CloudEvents schema delivers one event at a time.
log.LogInformation($"Source: {message["source"]}");
log.LogInformation($"Time: {message["eventTime"]}");
log.LogInformation($"Event data: {message["data"].ToString()}");
}
return req.CreateResponse(HttpStatusCode.OK);
}
使用 CloudEvents v1.0 的端点验证不同于此处描述的 EventGridSchema 和 CustomInputSchema 等交付模式。
出于测试目的,您可以使用 Webhook.site 作为端点处理程序。
以下屏幕显示 EventGrid 发送 OPTIONS 调用以进行验证握手:
请注意,端点处理程序的地址必须是 https。
如上图所示,有一个webhook-request-callback header。复制它的值a,放到浏览器上发送。您应该得到以下文本:
"Webhook succesfully validated as a subscription endpoint."
从现在开始,webhook 可以接收通知消息,请参见以下屏幕:
如您所见,CloudEvent v1.0 需要为 endpoint validation 实现 HTTP OPTIONS 响应。以下是此实现的示例:
string webhookcallback = req.Headers.GetValues("WebHook-Request-Callback")?.FirstOrDefault()?.Trim();
if(string.IsNullOrEmpty(webhookcallback) == false)
{
var hrm2 = req.CreateResponse(HttpStatusCode.OK);
hrm2.Headers.Add("WebHook-Request-Origin", "eventgrid.azure.net");
hrm2.Headers.Add("WebHook-Allowed-Rate", "120");
System.Threading.ThreadPool.QueueUserWorkItem(delegate (object state)
{
Task.Delay(5000).Wait();
using(var client = new HttpClient())
{
log.Warning($"{client.GetAsync(webhookcallback).Result.Content.ReadAsStringAsync().Result}");
}
});
return hrm2;
}
将上面的代码片段放入您的函数中。注意,调用WebHook-Request-Callback地址不能在OPTIONS调用内,必须在其OK响应返回后进行EventGrid,因此我们有一些延迟和后台线程。如果 EventGrid 将在 OPTIONS 调用中接受此调用,那就太好了。
尝试创建 WebHook 订阅时,部署无法验证 Azure Functions 中的 HTTP 触发器以用于 Cloud Events v1.0 Schema.I 已确认我的端点使用适当的响应处理验证握手。我的事件网格域正在使用 Cloud Events v1.0 架构。看来我的 Azure 函数在验证期间甚至没有被调用,因为函数的控制台中没有输出。
这适用于 Cloud Events v1.0 和 WebHooks 吗?
部署失败,出现以下错误:{"code":"Url validation","message":"The attempt to validate the provided endpoint xxxxx failed."}
这是我的代码:
[FunctionName("ProcessEvent")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
var requestmessage = await req.Content.ReadAsStringAsync();
var message = JToken.Parse(requestmessage);
if (message.Type == JTokenType.Array)
{
// If the request is for subscription validation, send back the validation code.
if (string.Equals((string)message[0]["eventType"],
"Microsoft.EventGrid.SubscriptionValidationEvent",
System.StringComparison.OrdinalIgnoreCase))
{
log.LogInformation("Validate request received");
var myObj = new { validationResponse = message[0]["data"]["validationCode"].ToString() };
var jsonToReturn = JsonConvert.SerializeObject(myObj);
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(jsonToReturn, Encoding.UTF8, "application/json")
};
}
}
else
{
// The request is not for subscription validation, so it's for an event.
// CloudEvents schema delivers one event at a time.
log.LogInformation($"Source: {message["source"]}");
log.LogInformation($"Time: {message["eventTime"]}");
log.LogInformation($"Event data: {message["data"].ToString()}");
}
return req.CreateResponse(HttpStatusCode.OK);
}
使用 CloudEvents v1.0 的端点验证不同于此处描述的 EventGridSchema 和 CustomInputSchema 等交付模式。
出于测试目的,您可以使用 Webhook.site 作为端点处理程序。 以下屏幕显示 EventGrid 发送 OPTIONS 调用以进行验证握手:
请注意,端点处理程序的地址必须是 https。 如上图所示,有一个webhook-request-callback header。复制它的值a,放到浏览器上发送。您应该得到以下文本:
"Webhook succesfully validated as a subscription endpoint."
从现在开始,webhook 可以接收通知消息,请参见以下屏幕:
如您所见,CloudEvent v1.0 需要为 endpoint validation 实现 HTTP OPTIONS 响应。以下是此实现的示例:
string webhookcallback = req.Headers.GetValues("WebHook-Request-Callback")?.FirstOrDefault()?.Trim();
if(string.IsNullOrEmpty(webhookcallback) == false)
{
var hrm2 = req.CreateResponse(HttpStatusCode.OK);
hrm2.Headers.Add("WebHook-Request-Origin", "eventgrid.azure.net");
hrm2.Headers.Add("WebHook-Allowed-Rate", "120");
System.Threading.ThreadPool.QueueUserWorkItem(delegate (object state)
{
Task.Delay(5000).Wait();
using(var client = new HttpClient())
{
log.Warning($"{client.GetAsync(webhookcallback).Result.Content.ReadAsStringAsync().Result}");
}
});
return hrm2;
}
将上面的代码片段放入您的函数中。注意,调用WebHook-Request-Callback地址不能在OPTIONS调用内,必须在其OK响应返回后进行EventGrid,因此我们有一些延迟和后台线程。如果 EventGrid 将在 OPTIONS 调用中接受此调用,那就太好了。