Search This Blog

2021-05-02

My Azure Study Note - 14 - Messaging- Event Grid

Event Grid

• Allows building event-based architectures

• Publishes events to interested parties

• No queue / no order

• Strong integration with many Azure services

• Cost effective, simple pricing

• No tiers, HA is built in


 


SLA:
• 99.99%
• Max event size:
• 1MB

Event Grid

• Performance:

• 10,000,000 events / sec

• 5,000 events / sec / topic

• Latency:

• Subsecond end-to-end latency in the 99th percentile 

Event Grid Pricing

• Based on:

• Number of operations

• First 100K operations are free 

 Create Event Grid

The event will be triggered when some item will be added to the storage account and write to the cosmos db


Install package- Microsoft.Azure.Webjobs.Extensions.EventGrid

Copy the connection strings




ProcessOrderCosmos.cs

using Microsoft.Azure.WebJobs;

using Microsoft.Azure.WebJobs.Extensions.Http;

using Microsoft.AspNetCore.Http;

using Microsoft.Extensions.Logging;

using System.Threading.Tasks;

using System;

using System.IO;

using Newtonsoft.Json;

using System.Collections.Generic;

using Microsoft.Azure.WebJobs.Extensions.EventGrid;

using Microsoft.Azure.EventGrid.Models;

using Microsoft.Azure.Storage.Blob;


namespace AzureCourse.Function

{

    public static class CosmosOrderFunction

    {

        [FunctionName("ProcessOrderCosmos")]

        public static void Run(

            [EventGridTrigger]EventGridEvent eventGridEvent,

            [Blob("neworders", FileAccess.Write, Connection = "StorageConnectionString")] CloudBlobContainer container,

            [CosmosDB(databaseName: "readit-orders", collectionName: "orders", ConnectionStringSetting = "CosmosDBConnection")]out Order order,

            ILogger log)            

        {        


            order=null;


            try  {    

                log.LogInformation("Function started");

                log.LogInformation($"Event details: Topic: {eventGridEvent.Topic}");

                log.LogInformation($"Event data: {eventGridEvent.Data.ToString()}");


                string eventBody = eventGridEvent.Data.ToString(); 


                log.LogInformation("Deserializing to StorageBlobCreatedEventData...");

                var storageData=JsonConvert.DeserializeObject<StorageBlobCreatedEventData>(eventBody);  

                log.LogInformation("Done");


                log.LogInformation("Get the name of the new blob...");

                var blobName=Path.GetFileName(storageData.Url);

                log.LogInformation($"Name of file: {blobName}");


                log.LogInformation("Get blob from storage...");

                var blockBlob=container.GetBlockBlobReference(blobName);

                var orderText=blockBlob.DownloadText();

                log.LogInformation("Done");

                log.LogInformation($"Order text: {orderText}");

                

                order=JsonConvert.DeserializeObject<Order>(orderText);  

            }

            catch (Exception ex)  {

                log.LogError(ex, "Error in function");

            }            

        }

    }   

}

Create Event Subscription




 Now Upload a file azure storage account->Container, check the function app log and check the cosmos DB





Content of the uploaded json file as below



No comments: