Lab 3: Publish Event Grid events from .NET

Task 1: Create a .NET project

  1. On the Start screen, select the Visual Studio Code tile.

  2. On the File menu, select Open Folder.

  3. In the File Explorer window that opens, browse to $HOME\training-az204\Labs\09\Starter\EventPublisher, and then select Select Folder.

  4. In the Visual Studio Code window, from its top menu bar, go to Terminal menu and select New Terminal.

  5. Run the following command to create a new .NET project named EventPublisher in the current folder:

     dotnet new console --framework net6.0 --name EventPublisher --output .
    

    Note: The dotnet new command will create a new console project in a folder with the same name as the project.

  6. Run the following command to import version 4.11.0 of Azure.Messaging.EventGrid from NuGet:

     dotnet add package Azure.Messaging.EventGrid --version 4.11.0
    
> **Note**: The **dotnet add package** command will add the **Microsoft.Azure.EventGrid** package from NuGet. For more information, go to [Azure.Messaging.EventGrid](https://www.nuget.org/packages/Azure.Messaging.EventGrid/4.11.0).

> **Note**: The **dotnet add package** command will add the **Azure.Messaging.EventGrid** package from NuGet. For more information, go to [Azure.Messaging.EventGrid](https://www.nuget.org/packages/Azure.Messaging.EventGrid/4.11.0).
  1. Run the following command to build the .NET web application:

     dotnet build
    
  2. Select Kill Terminal or the Recycle Bin icon to close the currently open terminal and any associated processes.

Task 2: Modify the Program class to connect to Event Grid

  1. On the Explorer pane of the Visual Studio Code window, open the Program.cs file.

  2. On the code editor tab for the Program.cs file, delete all the code in the existing file.

  3. Add the following line of code to import the Azure, and Azure.Messaging.EventGrid namespaces from the Azure.Messaging.EventGrid package imported from NuGet:

     using Azure;
     using Azure.Messaging.EventGrid;
    
  4. Add the following lines of code to add using directives for the built-in namespaces that will be used in this file:

     using System;
     using System.Threading.Tasks;
    
  5. Enter the following code to create a new Program class:

     public class Program
     {
     }
    
  6. In the Program class, enter the following line of code to create a new string constant named topicEndpoint:

     private const string topicEndpoint = "";
    
  7. Update the topicEndpoint string constant by setting its value to the Topic Endpoint of the Event Grid topic that you recorded previously in this lab.

  8. In the Program class, enter the following line of code to create a new string constant named topicKey:

     private const string topicKey = "";
    
  9. Update the topicKey string constant by setting its value to the Key of the Event Grid topic that you recorded previously in this lab.

  10. In the Program class, enter the following code to create a new asynchronous Main method:

     public static async Task Main(string[] args)
     {
     }
    
  11. Observe the Program.cs file, which should now include the following lines of code:

     using Azure;
     using Azure.Messaging.EventGrid;
     using System;
     using System.Threading.Tasks;    
     public class Program
     {
         private const string topicEndpoint = "<topic-endpoint>";
         private const string topicKey = "<topic-key>";        
         public static async Task Main(string[] args)
         {
         }
     }
    

Task 3: Publish new events

  1. In the Main method, perform the following actions to publish a list of events to your topic endpoint:

    a. Add the following line of code to create a new variable named endpoint of type Uri, using the topicEndpoint string constant as a constructor parameter:

     Uri endpoint = new Uri(topicEndpoint);
    

    b. Add the following line of code to create a new variable named credential of type AzureKeyCredential, using the topicKey string constant as a constructor parameter:

     AzureKeyCredential credential = new AzureKeyCredential(topicKey);
    

    c. Add the following line of code to create a new variable named client of type EventGridPublisherClient, using the endpoint and credential variables as constructor parameters:

     EventGridPublisherClient client = new EventGridPublisherClient(endpoint, credential);
    

    d. Add the following block of code to create a new variable named firstEvent of type EventGridEvent and populate that variable with sample data:

     EventGridEvent firstEvent = new EventGridEvent(
         subject: $"New Employee: Alba Sutton",
         eventType: "Employees.Registration.New",
         dataVersion: "1.0",
         data: new
         {
             FullName = "Alba Sutton",
             Address = "4567 Pine Avenue, Edison, WA 97202"
          }
      );
    

    e. Add the following block of code to create a new variable named secondEvent of type EventGridEvent and populate that variable with sample data:

         EventGridEvent secondEvent = new EventGridEvent(
             subject: $"New Employee: Alexandre Doyon",
             eventType: "Employees.Registration.New",
             dataVersion: "1.0",
             data: new
             {
                 FullName = "Alexandre Doyon",
                 Address = "456 College Street, Bow, WA 98107"
             }
         );
    

    f. Add the following line of code to asynchronously invoke the EventGridPublisherClient.SendEventAsync method using the firstEvent variable as a parameter:

      await client.SendEventAsync(firstEvent);
    

    g. Add the following line of code to render the "First event published" message to the console:

      Console.WriteLine("First event published");
    

    h. Add the following line of code to asynchronously invoke the EventGridPublisherClient.SendEventAsync method using the secondEvent variable as a parameter:

      await client.SendEventAsync(secondEvent);
    

    i. Add the following line of code to render the "Second event published" message to the console:

      Console.WriteLine("Second event published");
    
  2. Review the Main method, which should now include:

     public static async Task Main(string[] args)
     {
         Uri endpoint = new Uri(topicEndpoint);
         AzureKeyCredential credential = new AzureKeyCredential(topicKey);
         EventGridPublisherClient client = new EventGridPublisherClient(endpoint, credential);        
         EventGridEvent firstEvent = new EventGridEvent(
             subject: $"New Employee: Alba Sutton",
             eventType: "Employees.Registration.New",
             dataVersion: "1.0",
             data: new
             {
                 FullName = "Alba Sutton",
                 Address = "4567 Pine Avenue, Edison, WA 97202"
             }
         );
         EventGridEvent secondEvent = new EventGridEvent(
             subject: $"New Employee: Alexandre Doyon",
             eventType: "Employees.Registration.New",
             dataVersion: "1.0",
             data: new
             {
                 FullName = "Alexandre Doyon",
                 Address = "456 College Street, Bow, WA 98107"
             }
         );
         await client.SendEventAsync(firstEvent);
         Console.WriteLine("First event published");
         await client.SendEventAsync(secondEvent);
         Console.WriteLine("Second event published");
     }
    
  3. Save the Program.cs file.

  4. In the Visual Studio Code window, activate the shortcut menu for the Explorer pane, and then select Open in Integrated Terminal.

  5. Run the following command to run the .NET web application:

     dotnet run
    

    Note: If there are any build errors, review the Program.cs file in the $HOME\training-az204\Labs\09\Solution\EventPublisher folder.

  6. Observe the success message output from the currently running console application.

  7. Select Kill Terminal or the Recycle Bin icon to close the currently open terminal and any associated processes.

Task 4: Observe published events

  1. Return to the browser window with the Azure Event Grid Viewer web application.

  2. Review the Employees.Registration.New events that were created by your console application.

  3. Select any of the events and review its JSON content.

  4. Return to the Azure portal.

Review

In this exercise, you published new events to your Event Grid topic by using a .NET console application.

results matching ""

    No results matching ""