Exemplo n.º 1
0
        /// <summary>
        /// Publish the event.
        /// </summary>
        /// <param name="event">The event to publish.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
        /// <param name="currentRequest">The current request. Pass null if there is not parent request.</param>
        /// <returns>The result of the event.</returns>
        internal async Task PublishAsync(IEvent @event, CancellationToken cancellationToken, HandlerRequest currentRequest)
        {
            if (@event == null)
            {
                throw Error.ArgumentNull("@event");
            }

            if (this.disposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            if (currentRequest == null)
            {
                throw Error.InvalidOperation("The event '{0}' cannot be publish directly. Process a {1} then publish this event into the 'Handle' method.", @event.GetType().Name, typeof(ICommand).Name);
            }

            IEventStore eventStore = this.Configuration.Services.GetServiceOrThrow <IEventStore>();

            await eventStore.StoreAsync(@event, cancellationToken);

            IEventWorker eventWorker = this.Configuration.Services.GetEventWorker();

            using (EventHandlerRequest request = new EventHandlerRequest(this.Configuration, @event, currentRequest))
            {
                request.Processor = new MessageProcessorWrapper(this, request);
                await eventWorker.PublishAsync(request, cancellationToken);
            }
        }