Ejemplo n.º 1
0
        protected virtual async Task HandleEvent(DomainEvent domainEvent, CancellationToken cancellationToken)
        {
            var eventId  = domainEvent.GetType().FullName;
            var criteria = new SubscriptionSearchCriteria()
            {
                EventIds = new[] { eventId },
                Skip     = 0,
                Take     = int.MaxValue,
            };
            var searchResult = await _subscriptionSearchService.SearchAsync(criteria);

            if (searchResult.TotalCount > 0)
            {
                var entities = domainEvent.GetObjectsWithDerived <IEntity>()
                               .Select(x => new EventData {
                    ObjectId = x.Id, ObjectType = x.GetType().FullName, EventId = eventId
                })
                               .ToArray();
                var valueObjects = domainEvent.GetObjectsWithDerived <ValueObject>()
                                   .Select(x => new EventData {
                    ObjectId = x.GetCacheKey(), ObjectType = x.GetType().FullName, EventId = eventId
                })
                                   .ToArray();
                var eventData = entities.Union(valueObjects).ToArray();

                var activeSubscritions = new List <SubscriptionInfo>();

                foreach (var subscription in searchResult.Results)
                {
                    var provider = _eventBusFactory.CreateProvider(subscription.Provider);

                    if (provider != null)
                    {
                        var result = await provider.SendEventAsync(subscription, eventData);

                        subscription.Status = result.Status;
                        if (!string.IsNullOrEmpty(result.ErrorMessage))
                        {
                            subscription.ErrorMessage = new string(result.ErrorMessage.Take(1024).ToArray());
                        }

                        activeSubscritions.Add(subscription);
                    }
                }

                await _subscriptionService.SaveChangesAsync(activeSubscritions.ToArray());
            }
        }
        public async Task <ActionResult <SubscriptionSearchResult> > Search([FromBody] SubscriptionSearchCriteria searchCriteria)
        {
            var searchResult = await _subscriptionSearchService.SearchAsync(searchCriteria);

            return(Ok(searchResult));
        }