コード例 #1
0
        public async Task GenericReceiveAndDeserializeEventGridEvents()
        {
            // Example of a custom ObjectSerializer used to deserialize the event payload
            JsonObjectSerializer myCustomSerializer = new JsonObjectSerializer(
                new JsonSerializerOptions()
            {
                PropertyNameCaseInsensitive = true
            });

            var httpContent = new StreamContent(new BinaryData(jsonPayloadSampleTwo).ToStream());

            #region Snippet:CloudEventParseJson
            var bytes = await httpContent.ReadAsByteArrayAsync();

            // Parse the JSON payload into a list of events
            CloudEvent[] cloudEvents = CloudEvent.ParseMany(new BinaryData(bytes));
            #endregion

            // Iterate over each event to access event properties and data
            #region Snippet:DeserializePayloadUsingGenericGetData
            foreach (CloudEvent cloudEvent in cloudEvents)
            {
                switch (cloudEvent.Type)
                {
                case "Contoso.Items.ItemReceived":
                    // By default, ToObjectFromJson<T> uses System.Text.Json to deserialize the payload
                    ContosoItemReceivedEventData itemReceived = cloudEvent.Data.ToObjectFromJson <ContosoItemReceivedEventData>();
                    Console.WriteLine(itemReceived.ItemSku);
                    break;

                case "MyApp.Models.CustomEventType":
                    // One can also specify a custom ObjectSerializer as needed to deserialize the payload correctly
                    TestPayload testPayload = cloudEvent.Data.ToObject <TestPayload>(myCustomSerializer);
                    Console.WriteLine(testPayload.Name);
                    break;

                case SystemEventNames.StorageBlobDeleted:
                    // Example for deserializing system events using ToObjectFromJson<T>
                    StorageBlobDeletedEventData blobDeleted = cloudEvent.Data.ToObjectFromJson <StorageBlobDeletedEventData>();
                    Console.WriteLine(blobDeleted.BlobType);
                    break;
                }
            }
            #endregion
        }
コード例 #2
0
        public async Task GenericReceiveAndDeserializeEventGridEvents()
        {
            // Example of a custom ObjectSerializer used to deserialize the event payload
            JsonObjectSerializer myCustomSerializer = new JsonObjectSerializer(
                new JsonSerializerOptions()
            {
                AllowTrailingCommas         = true,
                PropertyNameCaseInsensitive = true
            });

            #region Snippet:CloudEventParseJson
            // Parse the JSON payload into a list of events using CloudEvent.Parse
            CloudEvent[] cloudEvents = CloudEvent.Parse(jsonPayloadSampleTwo);
            #endregion

            // Iterate over each event to access event properties and data
            #region Snippet:DeserializePayloadUsingGenericGetData
            foreach (CloudEvent cloudEvent in cloudEvents)
            {
                switch (cloudEvent.Type)
                {
                case "Contoso.Items.ItemReceived":
                    // By default, GetData uses JsonObjectSerializer to deserialize the payload
                    ContosoItemReceivedEventData itemReceived = cloudEvent.GetData <ContosoItemReceivedEventData>();
                    Console.WriteLine(itemReceived.ItemSku);
                    break;

                case "MyApp.Models.CustomEventType":
                    // One can also specify a custom ObjectSerializer as needed to deserialize the payload correctly
                    TestPayload testPayload = await cloudEvent.GetDataAsync <TestPayload>(myCustomSerializer);

                    Console.WriteLine(testPayload.Name);
                    break;

                case "Microsoft.Storage.BlobDeleted":
                    // Example for deserializing system events using GetData<T>
                    StorageBlobDeletedEventData blobDeleted = cloudEvent.GetData <StorageBlobDeletedEventData>();
                    Console.WriteLine(blobDeleted.BlobType);
                    break;
                }
            }
            #endregion
        }