尝试创建 WebHook 订阅时,部署无法在 Azure Functions 中为 Cloud Events v1.0 架构验证 HTTP 触发器。我已确认我的端点处理了具有适当响应的验证握手。我的事件网格域正在使用 Cloud Events v1.0 架构。看来我的 Azure 函数在验证期间甚至没有被调用,因为该函数的控制台中没有输出。
这是否适用于 Cloud Events v1.0 和 WebHooks?
部署失败并出现以下错误:{"code":"Url validation","message":"验证提供的端点 xxxxx 的尝试失败。"}
这是我的代码:
[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);
}