示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="domainEvent"></param>
        /// <returns></returns>
        public string Serialize(DomainEvent domainEvent)
        {
            if (domainEvent == null)
            {
                return("");
            }

            var attributes = domainEvent.ToPrimitives();

            attributes.Add("id", domainEvent.AggregateId);

            var domainClaims = _httpContextAccessor?.HttpContext?.User.Claims
                               .Select(c => new DomainClaim(c.Type, c.Value))
                               .ToList();

            return(JsonSerializer.Serialize(new Dictionary <string, Dictionary <string, object> >
            {
                { "headers", new Dictionary <string, object>
                  {
                      { "claims", domainClaims }
                  } },
                { "data", new Dictionary <string, object>
                  {
                      { "id", domainEvent.EventId },
                      { "type", domainEvent.GetEventName() },
                      { "occurred_on", domainEvent.OccurredOn },
                      { "attributes", attributes }
                  } },
                { "meta", new Dictionary <string, object>() }
            }));
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="domainEvent"></param>
        /// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
        /// <returns></returns>
        public Task Publish(DomainEvent domainEvent, CancellationToken cancellationToken)
        {
            var value = new DomainEventPrimitive
            {
                Id          = domainEvent.EventId,
                AggregateId = domainEvent.AggregateId,
                Body        = domainEvent.ToPrimitives(),
                Name        = domainEvent.GetEventName(),
                OccurredOn  = domainEvent.OccurredOn
            };

            return(_context.Set <DomainEventPrimitive>().AddAsync(value, cancellationToken).AsTask());
        }
示例#3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="domainEvent"></param>
        /// <returns></returns>
        public string Serialize(DomainEvent domainEvent)
        {
            if (domainEvent == null)
            {
                return("");
            }

            var attributes = domainEvent.ToPrimitives();

            attributes.Add("id", domainEvent.AggregateId);

            return(JsonSerializer.Serialize(new Dictionary <string, Dictionary <string, object> >
            {
                { "data", new Dictionary <string, object>
                  {
                      { "id", domainEvent.EventId },
                      { "type", domainEvent.GetEventName() },
                      { "occurred_on", domainEvent.OccurredOn },
                      { "attributes", attributes }
                  } },
                { "meta", new Dictionary <string, object>() }
            }));
        }
示例#4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="event"></param>
        /// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
        /// <returns></returns>
        public async Task Publish(DomainEvent @event, CancellationToken cancellationToken)
        {
            try
            {
                await _executeMiddlewaresService.ExecuteAsync(@event, cancellationToken);

                var serializedDomainEvent = _domainEventJsonSerializer.Serialize(@event);

                var channel = _config.Channel();
                channel.ExchangeDeclare(_rabbitMqParams.Value.ExchangeName, ExchangeType.Topic);

                var body       = Encoding.UTF8.GetBytes(serializedDomainEvent);
                var properties = channel.CreateBasicProperties();
                properties.Headers = new Dictionary <string, object> {
                    { HeaderReDelivery, 0 }
                };

                channel.BasicPublish(_rabbitMqParams.Value.ExchangeName, @event.GetEventName(), properties, body);
            }
            catch (RabbitMQClientException)
            {
                //await _failOverPublisher.Publish(new List<DomainEvent> {domainEvent}, cancellationToken);
            }
        }