示例#1
0
        /// <summary>
        /// Register asynchronously an event to be processed by the bus.
        /// </summary>
        /// <param name="event">Event to register.</param>
        /// <param name="context">Context associated to the event..</param>
        public Task <Result> PublishEventAsync(IDomainEvent @event, IEventContext context = null)
        {
            try
            {
                if (@event != null)
                {
                    var queue = Helpers.GetMessageQueue(_configuration.QueueName);

                    var      evtCfg     = _configuration.EventsLifetime.FirstOrDefault(t => new TypeEqualityComparer().Equals(t.Type, @event.GetType()));
                    TimeSpan?expiration = null;
                    if (evtCfg.Expiration.TotalMilliseconds > 0)
                    {
                        expiration = evtCfg.Expiration;
                    }

                    var serializedEvent = _serializer.SerializeEvent(@event);
                    var enveloppe       = expiration.HasValue
                        ? new Enveloppe(serializedEvent, @event.GetType(), _emiter, true, expiration.Value)
                        : new Enveloppe(serializedEvent, @event.GetType(), _emiter);

                    var message = new Message(enveloppe)
                    {
                        TimeToBeReceived = enveloppe.Expiration,
                        Formatter        = new JsonMessageFormatter()
                    };
                    queue.Send(message);
                }
                return(Task.FromResult(Result.Ok()));
            }
            catch
            {
                return(Task.FromResult(Result.Fail()));
            }
        }
示例#2
0
        public async Task <Result> PublishEventAsync(IDomainEvent @event, IEventContext context = null)
        {
            try
            {
                var eventType = @event.GetType();
                var lifetime  = _configuration
                                .EventsLifetime
                                .FirstOrDefault(e => new TypeEqualityComparer().Equals(e.EventType, eventType))
                                .LifeTime;
                await _queueClient.SendAsync(new Message
                {
                    ContentType = @event.GetType().AssemblyQualifiedName,
                    Body        = Encoding.UTF8.GetBytes(_dispatcherSerializer.SerializeEvent(@event)),
                    TimeToLive  = lifetime.TotalSeconds > 0 ? lifetime : TimeSpan.FromDays(1),
                    ReplyTo     = _emiter.ToString(),
                }).ConfigureAwait(false);

                return(Result.Ok());
            }
            catch // TODO Log exception
            {
                return(Result.Fail());
            }
        }
示例#3
0
        private Enveloppe GetEnveloppeFromEvent(IDomainEvent @event)
        {
            var      eventType  = @event.GetType();
            var      evtCfg     = _configuration.EventsLifetime.FirstOrDefault(t => new TypeEqualityComparer().Equals(t.EventType, @event.GetType()));
            TimeSpan?expiration = null;

            if (evtCfg.LifeTime.TotalMilliseconds > 0)
            {
                expiration = evtCfg.LifeTime;
                _logger.LogDebug(() => $"RabbitMQClientBus : Defining {evtCfg.LifeTime} lifetime for event of type {eventType.FullName}");
            }
            var serializedEvent = _serializer.SerializeEvent(@event);

            if (expiration.HasValue)
            {
                return(new Enveloppe(serializedEvent, eventType, _configuration.Emiter, true, expiration.Value));
            }
            return(new Enveloppe(serializedEvent, eventType, _configuration.Emiter));
        }