コード例 #1
0
ファイル: InProcessEventBus.cs プロジェクト: t9mike/Mitten
        /// <summary>
        /// Publishes an event.
        /// </summary>
        /// <param name="eventData">The event data to publish.</param>
        public async void Publish(IEvent eventData)
        {
            foreach (IEventSubscriber subscriber in this.eventSubscribers)
            {
                try
                {
                    await Task.Run(
                        () =>
                    {
                        EventEnvelope eventEnvelope =
                            new EventEnvelope(
                                eventData.Name,
                                type =>
                        {
                            if (!type.IsAssignableFrom(eventData.GetType()))
                            {
                                throw new InvalidOperationException("Cannot assign event of Type (" + eventData.GetType().FullName + ") to Type (" + type.FullName + ").");
                            }

                            return(eventData);
                        });

                        subscriber.ProcessEvent(eventEnvelope);
                    })
                    .ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    // TODO: is it possible this would cause a stack overflow if a subscriber throws when handling an error event?
                    this.Publish(new SystemErrorEvent("An unhandled exception was thrown by event " + eventData.Name + ".", ex));
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Processes an received event.
        /// </summary>
        /// <param name="eventEnvelope">An event envelope.</param>
        public void ProcessEvent(EventEnvelope eventEnvelope)
        {
            ConcurrentBag <Action <EventEnvelope> > eventHandlers;

            if (this.handlers.TryGetValue(eventEnvelope.EventName, out eventHandlers))
            {
                foreach (Action <EventEnvelope> handler in eventHandlers)
                {
                    handler(eventEnvelope);
                }
            }
        }