Example #1
0
        private async Task <List <EventGridWebHookResult> > HandleCloudEvent(string jsonContent, CancellationToken token)
        {
            var tasksExeptions = new List <EventGridWebHookResult>();

            EventGridWebHookResult result = null;

            try
            {
                var @event = JsonConvert.DeserializeObject <CloudEvent <object> >(jsonContent);

                object messageEventData = null;

                var webhook = _options.WebHooksRegistrations.FirstOrDefault(x => {
                    try
                    {
                        messageEventData = JsonConvert.DeserializeObject(@event.Data.ToString(), x.EventType, new JsonSerializerSettings {
                            MissingMemberHandling = MissingMemberHandling.Error
                        });
                    }
                    catch
                    {
                        return(false);
                    }

                    return(messageEventData != null && x.EventType == messageEventData.GetType() && x.EventGridType == @event.EventType);
                });

                if (webhook == null)
                {
                    throw new ArgumentException($"Can't find a Event Grid Webhook for this grid event: {@event.EventType}");
                }

                var service = webhook.Factory(_serviceProvider);

                var method = webhook.WebhookType.GetMethod("ProcessEventAsync");

                result = await(Task <EventGridWebHookResult>) method.Invoke(service, parameters: new object[] { messageEventData, token });
            }
            catch (Exception ex)
            {
                tasksExeptions.Add(new EventGridWebHookResult(ex));
            }
            finally
            {
                if (_options.ViewerHubContextEnabled)
                {
                    var details = JsonConvert.DeserializeObject <CloudEvent <dynamic> >(jsonContent);

                    // CloudEvents schema and mapping to
                    // Event Grid: https://docs.microsoft.com/en-us/azure/event-grid/cloudevents-schema
                    await _hubContext.Clients.All.SendAsync(
                        "gridupdate",
                        details.EventId,
                        details.EventType,
                        details.Source,
                        details.EventTime,
                        jsonContent,
                        result?.Exception.Message ?? "success"
                        );
                }

                if (result?.Exception != null)
                {
                    tasksExeptions.Add(result);
                }
            }
            return(tasksExeptions);
        }
Example #2
0
        private async Task <EventGridWebHookResult> ProcessGridEvent(JToken jtEvent, CancellationToken token)
        {
            EventGridWebHookResult result = null;

            try
            {
                var @event = JsonConvert.DeserializeObject <GridEvent <object> >(jtEvent.ToString());

                object messageEventData = null;

                var webhook = _options.WebHooksRegistrations.FirstOrDefault(x =>
                {
                    try
                    {
                        messageEventData = JsonConvert.DeserializeObject(@event.Data.ToString(), x.EventType, new JsonSerializerSettings {
                            MissingMemberHandling = MissingMemberHandling.Error
                        });
                    }
                    catch
                    {
                        return(false);
                    }

                    return(messageEventData != null && x.EventType == messageEventData.GetType() && x.EventGridType == @event.EventType);
                });

                if (webhook == null)
                {
                    throw new ArgumentException($"Can't find a Event Grid Webhook for this grid event: {@event.EventType}");
                }

                var service = webhook.Factory(_serviceProvider);

                var method = webhook.WebhookType.GetMethod("ProcessEventAsync");

                result = await(Task <EventGridWebHookResult>) method.Invoke(service, parameters: new object[] { messageEventData, token });
            }
            catch (Exception ex)
            {
                result = new EventGridWebHookResult(ex);
            }
            finally
            {
                if (_options.ViewerHubContextEnabled)
                {
                    // Invoke a method on the clients for
                    // an event grid notification.
                    var details = JsonConvert.DeserializeObject <GridEvent <dynamic> >(jtEvent.ToString());
                    await _hubContext.Clients.All.SendAsync(
                        "gridupdate",
                        details.Id,
                        details.EventType,
                        details.Subject,
                        details.EventTime.ToLongTimeString(),
                        jtEvent.ToString(),
                        result?.Exception.Message ?? "success");
                }
            }

            if (result?.Exception != null)
            {
                return(result);
            }
            return(null);
        }