Thursday 8 June 2023

Publish events to Azure Event Grid Topic using c# console app

In this aticle I explained how to publish events to Azure Event Grid Topic using C# console app.

 Steps to publish Events to Event Grid Topic

· Create a azure function using Azure Event Grid Trigger template.

· If you haven't previously used Event Grid in your Azure subscription, you may need to register the Event Grid resource provider.

· Create an event grid topic provides a user-defined endpoint that you post your events to.

· You subscribe to an event grid topic to tell Event Grid which events you want to track, and where (azure function) to send the events.

· Publish an event to Event Grid Topic using URL and KEY of the custom topic created (using postman or event publisher client console app).

 

Publish Events to EventGrid Topic

  • First if all install Azure and EventGrid packages and include the below ref.

        using Azure;

        using Azure.Messaging.EventGrid;

 

  • Declare and Event Grid Topic’s end point and public key:

        private const string topicEndpoint = "copy and paste here Topic end points";

        private const string topicKey = "Copay and paste here public key

 

  • Create Event Grid Publisher Client using credentials

        static Uri endpoint = new Uri(topicEndpoint);

        static AzureKeyCredential credential = new AzureKeyCredential(topicKey);

        static EventGridPublisherClient client = new EventGridPublisherClient(endpoint, credential);

 

  • Now create EventPublisher method to post events to Event Grid Topic

        private static async Task<string> EventPublisher()

        {

            List<EventGridEvent> eventsList = new List<EventGridEvent>

            {

                new EventGridEvent(

                subject: $"New Employee: Alba Sutton",

                eventType: "Employees.Registration.New",

                dataVersion: "1.0",

                 data: new{

                  Id = "108",

                   Description = "456 College Street, Bow, WA 98107",

                   isDone="true"}),

                new EventGridEvent(

                subject: $"New Employee: Alexandre Doyon",

                eventType: "Employees.Registration.New",

                dataVersion: "1.0",

                data: new{

                    Id = "109",

                   Description = "456 College Street, Bow, WA 98107",

                   isDone="true"})

            };

              await client.SendEventsAsync(eventsList);

              Console.WriteLine("All events have been published to the EventGrid Topic");

              return "All events have been published to the EventGrid Topic";

        }

 

  • Finally, call EventPublisher method from Manin method:

 

        static async Task Main()

            {

                try

                {

                await EventPublisher();

                Console.WriteLine("Press any key to exit");

                Console.ReadLine();

                }

               catch (Exception ex)

                {

                Console.WriteLine($"Exception: {ex.Message}");

                }

           }

 

  • The complete code example is here:

    using System;

    using Azure;

    using System.Threading.Tasks;

    using Azure.Messaging.EventGrid;

    namespace AzEventbasedSolution

    {

    internal class Program

    {

        private const string topicEndpoint = "copy and paste here Topic end points";

         private const string topicKey = "Copay and paste here public key

        static Uri endpoint = new Uri(topicEndpoint);

        static AzureKeyCredential credential = new AzureKeyCredential(topicKey);

        static EventGridPublisherClient client = new EventGridPublisherClient(endpoint, credential);

        static async Task Main()

        {

            try

            {

                await EventPublisher();

                Console.WriteLine("Press any key to exit");

                Console.ReadLine();

            }

            catch (Exception ex)

            {

                Console.WriteLine($"Exception: {ex.Message}");

            }

        }


        private static async Task<string> EventPublisher()

         {

            List<EventGridEvent> eventsList = new List<EventGridEvent>

            {

                new EventGridEvent(

                subject: $"New Employee: Alba Sutton",

                eventType: "Employees.Registration.New",

                dataVersion: "1.0",

                 data: new{

                  Id = "108",

                   Description = "456 College Street, Bow, WA 98107",

                   isDone="true"}),

                new EventGridEvent(

                subject: $"New Employee: Alexandre Doyon",

                eventType: "Employees.Registration.New",

                dataVersion: "1.0",

                data: new{

                    Id = "109",

                   Description = "456 College Street, Bow, WA 98107",

                   isDone="true"} )

            };

              await client.SendEventsAsync(eventsList);

              Console.WriteLine("All events have been published to the EventGrid Topic");

              return "All events have been published to the EventGrid Topic";

        }

      }

    }




No comments:

Post a Comment