예제 #1
0
        public void ThenNoOp()
        {
            var data           = new StorageBlobDeletedEventData();
            var eventGridEvent = new EventGridEvent(id: "", subject: "", data: data, eventType: "Microsoft.Storage.BlobDeleted", eventTime: DateTime.UtcNow, dataVersion: "");

            Action action = () => EventGridPump.Run(eventGridEvent, out var output, new StubLogger());

            action.Should().Throw <Exception>("Expected to be triggered by an Microsoft.Storage.BlobCreated but received Microsoft.Storage.BlobDeleted.");
        }
        /// <summary>
        /// Delete an image post corresponding to the blob deleted in the grid event.
        /// </summary>
        /// <param name="gridEvent">The grid event reporting the deleted blob.</param>
        /// <param name="token">The authentication token for the blob delete.</param>
        /// <returns>True/false for success.</returns>
        /// <exception cref="InvalidTokenException">Thrown if token is invalid.</exception>
        public async Task <bool> DeleteImagePost(EventGridEvent gridEvent, string token)
        {
            if (token != _correctToken)
            {
                throw new InvalidTokenException(token);
            }

            StorageBlobDeletedEventData eventData = JsonConvert.DeserializeObject <StorageBlobDeletedEventData>(gridEvent.Data.ToString());

            return(await _imagePostService.DeleteByUri(eventData.Url));
        }
        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
        }
예제 #4
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
        }