Soru

Zorluk: OrtaImplement Azure Event Grid Solutions

You are developing an ASP.NET Core Web API that will serve as a webhook subscriber for an Azure Event Grid custom topic. To start receiving events, you must implement the synchronous validation handshake. You have created the following controller action method to handle incoming POST requests:

csharp
[HttpPost]
public async Task<IActionResult> HandleEvent()
{
using var reader = new StreamReader(Request.Body);
string requestBody = await reader.ReadToEndAsync();

EventGridEvent[] events = EventGridEvent.ParseMany(BinaryData.FromString(requestBody));

foreach (EventGridEvent ev in events)
{
if (ev.EventType == "Microsoft.EventGrid.SubscriptionValidationEvent")
{
var data = ev.Data.ToObjectFromJson<SubscriptionValidationEventData>();

// [MISSING CODE]
}
}
return new OkResult();
}

Which of the following code segments should you use to replace `// [MISSING CODE]` to successfully complete the synchronous subscription validation handshake?

  1. var responseData = new { validationResponse = data.ValidationCode };
    return new OkObjectResult(responseData);
    Cevap
  2. B
    var responseData = new { validationCode = data.ValidationCode };
    return new OkObjectResult(responseData);
  3. C
    return new OkResult();
  4. D
    Response.Headers.Add("Webhook-Allowed-Origin", "eventgrid.azure.net");
    return new OkResult();

Cevap

The code segment that returns an anonymous object with the validationResponse property set to the validation code from the request.
To complete the synchronous validation handshake, the endpoint must return an HTTP 200 OK response containing a JSON payload with a single property named 'validationResponse'. The value of this property must match the 'validationCode' sent in the request data. Constructing an anonymous object in ASP.NET Core with the property name 'validationResponse' and passing it to OkObjectResult satisfies this requirement.

Adım Adım Çözüm

1
Detect the validation event type in the incoming request payload.
Confirm the request represents a SubscriptionValidationEvent.
Event Grid sends a validation event to verify that the endpoint is owned by the developer and is ready to accept events.
2
Extract the ValidationCode from the SubscriptionValidationEventData object.
Access the system-generated code sent by Event Grid.
The validation response must echo back this specific code to complete the verification.
3
Format the response JSON object with the property name 'validationResponse' and return it with an HTTP 200 status code.
Complete the validation handshake synchronously.
Event Grid expects the validation code to be returned in the response body inside a JSON object with the exact property key 'validationResponse'.

Anahtar Kavram

Azure Event Grid Webhook Endpoint Validation Handshake
Bu soruyu puanla