An Example of Azure function having HTTP trigger and the Event Grid that define the received parameters.

 

using Azure.Messaging.EventGrid;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.EventGrid;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;

 

namespace AzureFunctions
{
   public static class EventGirdFunction
   {
       [FunctionName("EventGirdFunction")]
       public static async Task<IActionResult> Run(

           // An HttpTrigger can either bith get- triggered as a result of an HTTP call.
           // it does not require any authentication.
           [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,


           [EventGrid(TopicEndpointUri = "MyEventGridPointTopicUriString", TopicKeySetting = "MyGridTopicSettings")]
           IAsyncCollector<EventGridEvent> outputEvents,
           ILogger log)
       {
           log.LogInformation("C# HTTP trigger function processed a request.");

           string name = req.Query["name"];

           string eventId = "message-id-3";
           string eventType = "user-add";
           string data = $"{{\"name\": \"{name}\" }}"; // Assuming 'name' is a variable in your scope
           string subject = "evet-type"; // This typically describes the source of the event
           DateTimeOffset eventTime = DateTime.UtcNow;
           string dataVersion = "1.0";
           var myEvent = new EventGridEvent(
               subject: subject,
               eventType: eventType,
               data: BinaryData.FromString(data), // Data should be a BinaryData object                
               dataVersion: dataVersion
           )
           {
               Id = eventId // Set the Id property separately
           };

           await outputEvents.AddAsync(myEvent);

           string responseMessage = $"Event with ID '{eventId}' of type '{eventType}' for user '{name}' has been published to Event Grid.";
           return new OkObjectResult(responseMessage);
       }
   }
}
 


Related Question