コード例 #1
0
        /// <summary>
        /// Handles the publisher.
        /// </summary>
        /// <param name="publisher">The publisher.</param>
        /// <param name="register">true to register publications, false to unregister them.</param>
        /// <param name="eventInfo">The published event..</param>
        /// <param name="attr">The attribute</param>
        /// <param name="eventTopicHost">The event topic host.</param>
        private void HandlePublisher(
            object publisher,
            bool register,
            EventInfo eventInfo,
            EventPublicationAttribute attr,
            IEventTopicHost eventTopicHost)
        {
            IEventTopic topic = eventTopicHost.GetEventTopic(attr.Topic);

            if (register)
            {
                List <IPublicationMatcher> matchers = new List <IPublicationMatcher>();
                foreach (Type type in attr.MatcherTypes)
                {
                    matchers.Add(this.factory.CreatePublicationMatcher(type));
                }

                topic.AddPublication(
                    publisher,
                    eventInfo,
                    attr.HandlerRestriction,
                    matchers);
            }
            else
            {
                topic.RemovePublication(publisher, eventInfo);
            }
        }
コード例 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyPublication"/> class.
        /// </summary>
        /// <param name="topic">The event topic this publication belongs to.</param>
        /// <param name="publisher">The publisher.</param>
        /// <param name="eventInfo">The <see cref="EventInfo"/> of the publisher that registers this event topic.</param>
        /// <param name="handlerRestriction">The handler restriction.</param>
        /// <param name="publicationMatchers">The publication matchers.</param>
        public PropertyPublication(
            IEventTopic topic,
            object publisher,
            EventInfo eventInfo,
            HandlerRestriction handlerRestriction,
            IList<IPublicationMatcher> publicationMatchers) : 
                base(topic, publisher, handlerRestriction, publicationMatchers)
        {
            this.eventInfo = eventInfo;

            if (this.eventInfo.EventHandlerType == null)
            {
                throw new Exception("EventHandlerType on published event must not be null (internal EventBroker failure).");
            }

            ThrowIfInvalidEventHandler(this.eventInfo);
            ThrowIfEventIsStatic(this.eventInfo);

            this.eventArgsType = this.eventInfo.EventHandlerType == typeof(EventHandler)
                                     ? typeof(EventArgs)
                                     : this.eventInfo.EventHandlerType.GetGenericArguments()[0];

            Delegate handler = Delegate.CreateDelegate(
                this.eventInfo.EventHandlerType,
                this,
                GetType().GetMethod("PublicationHandler"));
            this.eventInfo.AddEventHandler(publisher, handler);
        }
コード例 #3
0
 /// <summary>
 /// 入队列
 /// </summary>
 /// <param name="t_IEventTopic"></param>
 public void Enqueue(IEventTopic t_IEventTopic, object t_Sender, BaseEventArgs t_EventArgs)
 {
     m_EventTopicQueue.Enqueue(new EventTopicQueueData()
     {
         EventTopic = t_IEventTopic, Sender = t_Sender, EventArgs = t_EventArgs
     });
 }
コード例 #4
0
        /// <summary>
        /// Gets a event topic. Result is never null. Event topic is created if it does not yet exist.
        /// </summary>
        /// <param name="topic">The topic URI identifying the event topic to return.</param>
        /// <returns>A non-null event topic. Event topic is created if it does not yet exist.</returns>
        /// <remarks>
        /// Returns a non null instance of the dictionary.
        /// </remarks>
        /// <value>The event topics.</value>
        public IEventTopic GetEventTopic(string topic)
        {
            if (this.eventTopics.ContainsKey(topic))
            {
                return(this.eventTopics[topic]);
            }

            lock (this)
            {
                // recheck inside monitor
                if (this.eventTopics.ContainsKey(topic))
                {
                    return(this.eventTopics[topic]);
                }

                IEventTopic eventTopic = this.factory.CreateEventTopicInternal(topic, this.globalMatchersProvider);

                // copy
                this.eventTopics = new Dictionary <string, IEventTopic>(this.eventTopics)
                {
                    { topic, eventTopic }
                };

                this.extensionHost.ForEach(extension => extension.CreatedTopic(eventTopic));

                return(eventTopic);
            }
        }
コード例 #5
0
        /// <summary>
        /// Adds a publication. Use this to register publications by code instead of using attributes.
        /// </summary>
        /// <typeparam name="TEventArgs">The type of the event arguments.</typeparam>
        /// <param name="topic">The topic.</param>
        /// <param name="publisher">The publisher.</param>
        /// <param name="publishedEvent">The published event.</param>
        /// <param name="handlerRestriction">The handler restriction.</param>
        /// <param name="matchers">The matchers.</param>
        public void AddPublication <TEventArgs>(string topic, object publisher, ref EventHandler <TEventArgs> publishedEvent, HandlerRestriction handlerRestriction, params IPublicationMatcher[] matchers) where TEventArgs : EventArgs
        {
            IEventTopic  eventTopic  = this.eventTopicHost.GetEventTopic(topic);
            IPublication publication = this.factory.CreatePublication(eventTopic, publisher, ref publishedEvent, handlerRestriction, matchers);

            eventTopic.AddPublication(publication);
        }
コード例 #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyPublication"/> class.
        /// </summary>
        /// <param name="topic">The event topic this publication belongs to.</param>
        /// <param name="publisher">The publisher.</param>
        /// <param name="eventInfo">The <see cref="EventInfo"/> of the publisher that registers this event topic.</param>
        /// <param name="handlerRestriction">The handler restriction.</param>
        /// <param name="publicationMatchers">The publication matchers.</param>
        public PropertyPublication(
            IEventTopic topic,
            object publisher,
            EventInfo eventInfo,
            HandlerRestriction handlerRestriction,
            IList <IPublicationMatcher> publicationMatchers) :
            base(topic, publisher, handlerRestriction, publicationMatchers)
        {
            this.eventInfo = eventInfo;

            if (this.eventInfo.EventHandlerType == null)
            {
                throw new EventBrokerException("EventHandlerType on published event must not be null (internal EventBroker failure).");
            }

            ThrowIfInvalidEventHandler(this.eventInfo);
            ThrowIfEventIsStatic(this.eventInfo);

            this.eventArgsType = this.eventInfo.EventHandlerType == typeof(EventHandler)
                                     ? typeof(EventArgs)
                                     : this.eventInfo.EventHandlerType.GetGenericArguments()[0];

            Delegate handler = Delegate.CreateDelegate(
                this.eventInfo.EventHandlerType,
                this,
                this.GetType().GetMethod("PublicationHandler"));

            this.eventInfo.AddEventHandler(publisher, handler);
        }
コード例 #7
0
        /// <summary>
        /// Handles the subscriber.
        /// </summary>
        /// <param name="subscriber">The subscriber.</param>
        /// <param name="register">true to register subscriptions, false to unregister them.</param>
        /// <param name="methodInfo">The handler method.</param>
        /// <param name="attr">The subscription attribute.</param>
        /// <param name="eventTopicHost">The event topic host.</param>
        private void HandleSubscriber(
            object subscriber,
            bool register,
            MethodInfo methodInfo,
            EventSubscriptionAttribute attr,
            IEventTopicHost eventTopicHost)
        {
            IEventTopic topic = eventTopicHost.GetEventTopic(attr.Topic);

            if (register)
            {
                List <ISubscriptionMatcher> matchers = new List <ISubscriptionMatcher>();
                foreach (Type type in attr.MatcherTypes)
                {
                    matchers.Add(this.factory.CreateSubscriptionMatcher(type));
                }

                topic.AddSubscription(
                    subscriber,
                    methodInfo,
                    this.factory.CreateHandler(attr.HandlerType),
                    matchers);
            }
            else
            {
                topic.RemoveSubscription(subscriber, methodInfo);
            }
        }
コード例 #8
0
        /// <summary>
        /// Removes a subscription.
        /// </summary>
        /// <typeparam name="TEventArgs">The type of the event arguments.</typeparam>
        /// <param name="topic">The topic.</param>
        /// <param name="subscriber">The subscriber.</param>
        /// <param name="handlerMethod">The handler method.</param>
        public void RemoveSubscription <TEventArgs>(string topic, object subscriber, EventHandler <TEventArgs> handlerMethod) where TEventArgs : EventArgs
        {
            Guard.AgainstNullArgument(nameof(handlerMethod), handlerMethod);

            IEventTopic eventTopic = this.eventTopicHost.GetEventTopic(topic);

            eventTopic.RemoveSubscription(subscriber, handlerMethod.Method);
        }
コード例 #9
0
        /// <summary>
        /// Removes a subscription.
        /// </summary>
        /// <typeparam name="TEventArgs">The type of the event arguments.</typeparam>
        /// <param name="topic">The topic.</param>
        /// <param name="subscriber">The subscriber.</param>
        /// <param name="handlerMethod">The handler method.</param>
        public void RemoveSubscription <TEventArgs>(string topic, object subscriber, EventHandler <TEventArgs> handlerMethod) where TEventArgs : EventArgs
        {
            Ensure.ArgumentNotNull(handlerMethod, "handlerMethod");

            IEventTopic eventTopic = this.eventTopicHost.GetEventTopic(topic);

            eventTopic.RemoveSubscription(subscriber, handlerMethod.Method);
        }
コード例 #10
0
        /// <summary>
        /// Fires the specified topic directly on the <see cref="IEventBroker"/> without a real publisher.
        /// This is useful when temporarily created objects need to fire events.
        /// The event is fired globally but can be matched with <see cref="Matchers.ISubscriptionMatcher"/>.
        /// </summary>
        /// <param name="topic">The topic URI.</param>
        /// <param name="publisher">The publisher (for event flow and logging).</param>
        /// <param name="handlerRestriction">The handler restriction.</param>
        /// <param name="sender">The sender (passed to the event handler).</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        public void Fire(string topic, object publisher, HandlerRestriction handlerRestriction, object sender, EventArgs e)
        {
            IEventTopic eventTopic = this.eventTopicHost.GetEventTopic(topic);

            eventTopic.Fire(
                sender,
                e,
                new SpontaneousPublication(eventTopic, publisher, e.GetType(), handlerRestriction, new List <IPublicationMatcher>()));
        }
コード例 #11
0
        private void UnregisterPropertySubscriptions(object subscriber, IEnumerable <PropertySubscriptionScanResult> propertySubscriptions)
        {
            foreach (PropertySubscriptionScanResult propertySubscription in propertySubscriptions)
            {
                IEventTopic topic = this.eventTopicHost.GetEventTopic(propertySubscription.Topic);

                topic.RemoveSubscription(subscriber, propertySubscription.Method);
            }
        }
コード例 #12
0
 /// <summary>
 /// Creates a new publication
 /// </summary>
 /// <param name="eventTopic">The event topic.</param>
 /// <param name="publisher">The publisher.</param>
 /// <param name="eventInfo">The event info.</param>
 /// <param name="handlerRestriction">The handler restriction.</param>
 /// <param name="publicationMatchers">The publication matchers.</param>
 /// <returns>A newly created publication</returns>
 public virtual IPublication CreatePublication(
     IEventTopic eventTopic,
     object publisher,
     EventInfo eventInfo,
     HandlerRestriction handlerRestriction,
     IList <IPublicationMatcher> publicationMatchers)
 {
     return(new PropertyPublication(eventTopic, publisher, eventInfo, handlerRestriction, publicationMatchers));
 }
コード例 #13
0
 /// <summary>
 /// Creates a new publication.
 /// </summary>
 /// <typeparam name="TEventArgs">The type of the event arguments.</typeparam>
 /// <param name="eventTopic">The event topic.</param>
 /// <param name="publisher">The publisher.</param>
 /// <param name="eventHandler">The event handler.</param>
 /// <param name="handlerRestriction">The handler restriction.</param>
 /// <param name="publicationMatchers">The matchers.</param>
 /// <returns>A newly created publication</returns>
 public virtual IPublication CreatePublication <TEventArgs>(
     IEventTopic eventTopic,
     object publisher,
     ref EventHandler <TEventArgs> eventHandler,
     HandlerRestriction handlerRestriction,
     IList <IPublicationMatcher> publicationMatchers) where TEventArgs : EventArgs
 {
     return(new CodePublication <TEventArgs>(eventTopic, publisher, ref eventHandler, handlerRestriction, publicationMatchers));
 }
コード例 #14
0
        /// <summary>
        /// Adds a subscription. Use this to register subscriptions by code instead of using attributes.
        /// </summary>
        /// <typeparam name="TEventArgs">The type of the event arguments.</typeparam>
        /// <param name="topic">The topic.</param>
        /// <param name="subscriber">The subscriber.</param>
        /// <param name="handlerMethod">The handler method.</param>
        /// <param name="handler">The handler.</param>
        /// <param name="matchers">The subscription matchers.</param>
        public void AddSubscription <TEventArgs>(string topic, object subscriber, EventHandler <TEventArgs> handlerMethod, IHandler handler, params ISubscriptionMatcher[] matchers) where TEventArgs : EventArgs
        {
            IEventTopic eventTopic = this.eventTopicHost.GetEventTopic(topic);

            eventTopic.AddSubscription(
                subscriber,
                handlerMethod.Method,
                handler,
                matchers != null ? new List <ISubscriptionMatcher>(matchers) : new List <ISubscriptionMatcher>());
        }
コード例 #15
0
        /// <summary>
        /// Adds a publication. Use this to register publications by code instead of using attributes.
        /// </summary>
        /// <param name="topic">The topic.</param>
        /// <param name="publisher">The publisher.</param>
        /// <param name="publishedEvent">The published event of the <paramref name="publisher"/>.</param>
        /// <param name="handlerRestriction">The handler restriction.</param>
        /// <param name="matchers">The matchers.</param>
        public void AddPublication(string topic, object publisher, ref EventHandler publishedEvent, HandlerRestriction handlerRestriction, params IPublicationMatcher[] matchers)
        {
            IEventTopic eventTopic = this.eventTopicHost.GetEventTopic(topic);

            eventTopic.AddPublication(
                publisher,
                ref publishedEvent,
                handlerRestriction,
                matchers);
        }
コード例 #16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Publication"/> class.
 /// </summary>
 /// <param name="topic">The event topic this publication belongs to.</param>
 /// <param name="publisher">The publisher.</param>
 /// <param name="handlerRestriction">The handler restriction.</param>
 /// <param name="publicationMatchers">The publication matchers.</param>
 protected Publication(
     IEventTopic topic,
     object publisher,
     HandlerRestriction handlerRestriction,
     IList <IPublicationMatcher> publicationMatchers)
 {
     this.topic               = topic;
     this.publisher           = new WeakReference(publisher);
     this.handlerRestriction  = handlerRestriction;
     this.publicationMatchers = publicationMatchers;
 }
コード例 #17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Publication"/> class.
 /// </summary>
 /// <param name="topic">The event topic this publication belongs to.</param>
 /// <param name="publisher">The publisher.</param>
 /// <param name="handlerRestriction">The handler restriction.</param>
 /// <param name="publicationMatchers">The publication matchers.</param>
 protected Publication(
     IEventTopic topic,
     object publisher,
     HandlerRestriction handlerRestriction,
     IList<IPublicationMatcher> publicationMatchers)
 {
     this.topic = topic;
     this.publisher = new WeakReference(publisher);
     this.handlerRestriction = handlerRestriction;
     this.publicationMatchers = publicationMatchers;
 }
コード例 #18
0
        public void RemovePublication(string topic, object publisher, string eventName)
        {
            IEventTopic eventTopic = this.eventTopicHost.GetEventTopic(topic);

            IPublication publication = eventTopic.RemovePublication(publisher, eventName);

            if (publication != null)
            {
                publication.Dispose();
            }
        }
コード例 #19
0
 private void CallWithoutThreadSwitch(IEventTopic eventTopic, Delegate subscriptionHandler, object sender, EventArgs e)
 {
     try
     {
         subscriptionHandler.DynamicInvoke(sender, e);
     }
     catch (TargetInvocationException exception)
     {
         this.HandleSubscriberMethodException(exception, eventTopic);
     }
 }
コード例 #20
0
 /// <summary>
 /// Executes the subscription synchronously on the user interface thread.
 /// </summary>
 /// <param name="eventTopic">The event topic.</param>
 /// <param name="sender">The sender.</param>
 /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
 /// <param name="subscriptionHandler">The subscription handler.</param>
 public override void Handle(IEventTopic eventTopic, object sender, EventArgs e, Delegate subscriptionHandler)
 {
     if (this.RunningOnUserInterfaceThread())
     {
         this.CallWithoutThreadSwitch(eventTopic, subscriptionHandler, sender, e);
     }
     else
     {
         this.CallWithThreadSwitch(eventTopic, subscriptionHandler, sender, e);
     }
 }
コード例 #21
0
 private void CallWithoutThreadSwitch(IEventTopic eventTopic, Delegate subscriptionHandler, object sender, EventArgs e)
 {
     try
     {
         subscriptionHandler.DynamicInvoke(sender, e);
     }
     catch (TargetInvocationException exception)
     {
         this.HandleSubscriberMethodException(exception, eventTopic);
     }
 }
コード例 #22
0
 /// <summary>
 /// Executes the subscription synchronously on the user interface thread.
 /// </summary>
 /// <param name="eventTopic">The event topic.</param>
 /// <param name="sender">The sender.</param>
 /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
 /// <param name="subscriptionHandler">The subscription handler.</param>
 public override void Handle(IEventTopic eventTopic, object sender, EventArgs e, Delegate subscriptionHandler)
 {
     if (this.RunningOnUserInterfaceThread())
     {
         this.CallWithoutThreadSwitch(eventTopic, subscriptionHandler, sender, e);
     }
     else
     {
         this.CallWithThreadSwitch(eventTopic, subscriptionHandler, sender, e);
     }
 }
コード例 #23
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CodePublication{TEventArgs}"/> class.
        /// </summary>
        /// <param name="topic">The topic.</param>
        /// <param name="publisher">The publisher.</param>
        /// <param name="eventHandler">The event handler.</param>
        /// <param name="handlerRestriction">The handler restriction.</param>
        /// <param name="publicationMatchers">The publication matchers.</param>
        public CodePublication(
            IEventTopic topic,
            object publisher,
            ref EventHandler <TEventArgs> eventHandler,
            HandlerRestriction handlerRestriction,
            IList <IPublicationMatcher> publicationMatchers) :
            base(topic, publisher, handlerRestriction, publicationMatchers)
        {
            eventHandler += this.PublicationHandler;

            this.eventArgsType = eventHandler.GetType().GetGenericArguments()[0];
        }
コード例 #24
0
        /// <summary>
        /// Adds a subscription. Use this to register subscriptions by code instead of using attributes.
        /// </summary>
        /// <param name="topic">The topic.</param>
        /// <param name="subscriber">The subscriber.</param>
        /// <param name="handlerMethod">The handler method.</param>
        /// <param name="handler">The handler.</param>
        /// <param name="matchers">The subscription matchers.</param>
        public void AddSubscription(string topic, object subscriber, EventHandler handlerMethod, IHandler handler, params ISubscriptionMatcher[] matchers)
        {
            Ensure.ArgumentNotNull(handlerMethod, "handlerMethod");

            IEventTopic eventTopic = this.eventTopicHost.GetEventTopic(topic);

            eventTopic.AddSubscription(
                subscriber,
                handlerMethod.Method,
                handler,
                matchers != null ? new List <ISubscriptionMatcher>(matchers) : new List <ISubscriptionMatcher>());
        }
コード例 #25
0
        public ValueTask SendAsync <TTopic, TMessage>(
            TTopic topic,
            TMessage message,
            CancellationToken cancellationToken = default)
            where TTopic : notnull
        {
            IEventTopic eventTopic = _topics.GetOrAdd(topic, s => new EventTopic <TMessage>());

            if (eventTopic is EventTopic <TMessage> et)
            {
                et.TryWrite(message);
                return(default);
コード例 #26
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CodePublication{TEventArgs}"/> class.
        /// </summary>
        /// <param name="topic">The topic.</param>
        /// <param name="publisher">The publisher.</param>
        /// <param name="eventHandler">The event handler.</param>
        /// <param name="handlerRestriction">The handler restriction.</param>
        /// <param name="publicationMatchers">The publication matchers.</param>
        public CodePublication(
            IEventTopic topic,
            object publisher,
            ref EventHandler eventHandler,
            HandlerRestriction handlerRestriction,
            IList <IPublicationMatcher> publicationMatchers) :
            base(topic, publisher, handlerRestriction, publicationMatchers)
        {
            eventHandler += this.PublicationHandler;

            this.eventArgsType = typeof(EventArgs);
        }
コード例 #27
0
        /// <summary>
        /// Executes the subscription synchronously on the same thread as the publisher is currently running.
        /// </summary>
        /// <param name="eventTopic">The event topic.</param>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <param name="subscriptionHandler">The subscription handler.</param>
        public override void Handle(IEventTopic eventTopic, object sender, EventArgs e, Delegate subscriptionHandler)
        {
            Ensure.ArgumentNotNull(subscriptionHandler, "subscriptionHandler");

            try
            {
                subscriptionHandler.DynamicInvoke(sender, e);
            }
            catch (TargetInvocationException ex)
            {
                this.HandleSubscriberMethodException(ex, eventTopic);
            }
        }
コード例 #28
0
        /// <summary>
        /// Removes a publication. Publications added with <see cref="AddPublication(string,object,ref EventHandler,HandlerRestriction,IPublicationMatcher[])"/> have to be removed in order that the event broker can be disposed.
        /// </summary>
        /// <param name="topic">The topic.</param>
        /// <param name="publisher">The publisher.</param>
        /// <param name="publishedEvent">The published event.</param>
        public void RemovePublication <TEventArgs>(string topic, object publisher, ref EventHandler <TEventArgs> publishedEvent) where TEventArgs : EventArgs
        {
            IEventTopic eventTopic = this.eventTopicHost.GetEventTopic(topic);

            IPublication publication = eventTopic.RemovePublication(publisher, CodePublication <TEventArgs> .EventNameOfCodePublication);

            var codePublication = publication as CodePublication <TEventArgs>;

            if (codePublication != null)
            {
                codePublication.Unregister(ref publishedEvent);
            }
        }
コード例 #29
0
        /// <summary>
        /// Executes the subscription synchronously on the same thread as the publisher is currently running.
        /// </summary>
        /// <param name="eventTopic">The event topic.</param>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <param name="subscriptionHandler">The subscription handler.</param>
        public override void Handle(IEventTopic eventTopic, object sender, EventArgs e, Delegate subscriptionHandler)
        {
            Ensure.ArgumentNotNull(subscriptionHandler, "subscriptionHandler");

            try
            {
                subscriptionHandler.DynamicInvoke(sender, e);
            }
            catch (TargetInvocationException ex)
            {
                this.HandleSubscriberMethodException(ex, eventTopic);
            }
        }
コード例 #30
0
        /// <summary>
        /// Handles the subscriber.
        /// </summary>
        /// <param name="eventTopicHost">The event topic host.</param>
        /// <param name="topic">The topic.</param>
        /// <param name="register">if set to <c>true</c> [register].</param>
        /// <param name="subscriber">The subscriber.</param>
        /// <param name="handlerMethod">The handler method.</param>
        /// <param name="handler">The handler.</param>
        /// <param name="matchers">The matchers.</param>
        private void HandleSubscriber(IEventTopicHost eventTopicHost, string topic, bool register, object subscriber, MethodInfo handlerMethod, IHandler handler, ISubscriptionMatcher[] matchers)
        {
            IEventTopic eventTopic = eventTopicHost.GetEventTopic(topic);

            if (register)
            {
                eventTopic.AddSubscription(subscriber, handlerMethod, handler, matchers);
            }
            else
            {
                eventTopic.RemoveSubscription(subscriber, handlerMethod);
            }
        }
コード例 #31
0
        private void UnregisterPropertyPublications(object publisher, IEnumerable <PropertyPublicationScanResult> propertyPublications)
        {
            foreach (PropertyPublicationScanResult propertyPublication in propertyPublications)
            {
                IEventTopic topic = this.eventTopicHost.GetEventTopic(propertyPublication.Topic);

                IPublication publication = topic.RemovePublication(publisher, propertyPublication.Event.Name);

                if (publication != null)
                {
                    publication.Dispose();
                }
            }
        }
コード例 #32
0
        /// <summary>
        /// Fires the specified topic directly on the <see cref="IEventBroker"/> without a real publisher.
        /// This is useful when temporarily created objects need to fire events.
        /// The event is fired globally but can be matched with <see cref="Matchers.ISubscriptionMatcher"/>.
        /// </summary>
        /// <param name="topic">The topic URI.</param>
        /// <param name="publisher">The publisher (for event flow and logging).</param>
        /// <param name="handlerRestriction">The handler restriction.</param>
        /// <param name="sender">The sender (passed to the event handler).</param>
        /// <param name="eventArgs">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        public void Fire(string topic, object publisher, HandlerRestriction handlerRestriction, object sender, EventArgs eventArgs)
        {
            Ensure.ArgumentNotNull(eventArgs, "eventArgs");

            IEventTopic eventTopic = this.eventTopicHost.GetEventTopic(topic);

            using (var spontaneousPublication = new SpontaneousPublication(eventTopic, publisher, eventArgs.GetType(), handlerRestriction, new List <IPublicationMatcher>()))
            {
                eventTopic.AddPublication(spontaneousPublication);

                eventTopic.Fire(sender, eventArgs, spontaneousPublication);

                eventTopic.RemovePublication(spontaneousPublication);
            }
        }
コード例 #33
0
        private void AddSubscription(string topic, object subscriber, IHandler handler, IEnumerable <ISubscriptionMatcher> matchers, MethodInfo methodInfo)
        {
            IEventTopic eventTopic = this.eventTopicHost.GetEventTopic(topic);

            handler.Initialize(subscriber, methodInfo, this.extensionHost);

            DelegateWrapper delegateWrapper = GetDelegateWrapper(methodInfo);
            ISubscription   subscription    = this.factory.CreateSubscription(
                subscriber,
                delegateWrapper,
                handler,
                matchers != null ? new List <ISubscriptionMatcher>(matchers) : new List <ISubscriptionMatcher>());

            eventTopic.AddSubscription(subscription);
        }
コード例 #34
0
        /// <summary>
        /// Fires the specified topic directly on the <see cref="IEventBroker"/> without a real publisher.
        /// This is useful when temporarily created objects need to fire events.
        /// The event is fired globally but can be matched with <see cref="Matchers.ISubscriptionMatcher"/>.
        /// </summary>
        /// <param name="topic">The topic URI.</param>
        /// <param name="publisher">The publisher (for event flow and logging).</param>
        /// <param name="handlerRestriction">The handler restriction.</param>
        /// <param name="sender">The sender (passed to the event handler).</param>
        /// <param name="eventArgs">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        public void Fire(string topic, object publisher, HandlerRestriction handlerRestriction, object sender, EventArgs eventArgs)
        {
            Guard.AgainstNullArgument(nameof(eventArgs), eventArgs);

            IEventTopic eventTopic = this.eventTopicHost.GetEventTopic(topic);

            using (var spontaneousPublication = new SpontaneousPublication(eventTopic, publisher, eventArgs.GetType(), handlerRestriction, new List <IPublicationMatcher>()))
            {
                eventTopic.AddPublication(spontaneousPublication);

                eventTopic.Fire(sender, eventArgs, spontaneousPublication);

                eventTopic.RemovePublication(publisher, SpontaneousPublication.SpontaneousEventName);
            }
        }
コード例 #35
0
 /// <summary>
 /// Executes the subscription asynchronously on the user interface thread.
 /// </summary>
 /// <param name="eventTopic">The event topic.</param>
 /// <param name="sender">The sender.</param>
 /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
 /// <param name="subscriptionHandler">The subscription handler.</param>
 public override void Handle(IEventTopic eventTopic, object sender, EventArgs e, Delegate subscriptionHandler)
 {
     this.syncContextHolder.SyncContext.Post(
         delegate(object data)
             {
                 try
                 {
                     ((Delegate)data).DynamicInvoke(sender, e);
                 }
                 catch (TargetInvocationException exception)
                 {
                     this.HandleSubscriberMethodException(exception, eventTopic);
                 }
             }, 
             subscriptionHandler);
 }
コード例 #36
0
        /// <summary>
        /// Handles a subscriber method exception by passing it to all extensions and re-throwing the inner exception in case that none of the
        /// extensions handled it.
        /// </summary>
        /// <param name="targetInvocationException">The targetInvocationException.</param>
        /// <param name="eventTopic">The event topic.</param>
        protected void HandleSubscriberMethodException(TargetInvocationException targetInvocationException, IEventTopic eventTopic)
        {
            Ensure.ArgumentNotNull(targetInvocationException, "targetInvocationException");

            var innerException = targetInvocationException.InnerException;
            innerException.PreserveStackTrace();

            var context = new ExceptionHandlingContext();

            this.ExtensionHost.ForEach(extension => extension.SubscriberExceptionOccurred(eventTopic, innerException, context));
                
            if (!context.Handled)
            {
                throw innerException;
            }
        }
コード例 #37
0
 /// <summary>
 /// Executes the subscription asynchronously on the user interface thread.
 /// </summary>
 /// <param name="eventTopic">The event topic.</param>
 /// <param name="sender">The sender.</param>
 /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
 /// <param name="subscriptionHandler">The subscription handler.</param>
 public override void Handle(IEventTopic eventTopic, object sender, EventArgs e, Delegate subscriptionHandler)
 {
     this.syncContextHolder.SyncContext.Post(
         delegate(object data)
     {
         try
         {
             ((Delegate)data).DynamicInvoke(sender, e);
         }
         catch (TargetInvocationException exception)
         {
             this.HandleSubscriberMethodException(exception, eventTopic);
         }
     },
         subscriptionHandler);
 }
コード例 #38
0
 /// <summary>
 /// Executes the subscription on a thread pool worker thread.
 /// </summary>
 /// <param name="eventTopic">The event topic.</param>
 /// <param name="sender">The sender.</param>
 /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
 /// <param name="subscriptionHandler">The subscription handler.</param>
 public override void Handle(IEventTopic eventTopic, object sender, EventArgs e, Delegate subscriptionHandler)
 {
     ThreadPool.QueueUserWorkItem(
         delegate(object state)
             {
                 try
                 {
                     var args = (CallInBackgroundArguments)state;
                     args.Handler.DynamicInvoke(args.Sender, args.EventArgs);
                 }
                 catch (TargetInvocationException exception)
                 {
                     this.HandleSubscriberMethodException(exception, eventTopic);
                 }
             },
         new CallInBackgroundArguments(sender, e, subscriptionHandler));
 }
コード例 #39
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EventTopicException"/> class with the specified list of exceptions.
 /// </summary>
 /// <param name="topic">The <see cref="IEventTopic"/> instance whose subscribers incurred into an exception.</param>
 /// <param name="exceptions">The list of exceptions that occurred during the subscribers invocation.</param>
 public EventTopicException(IEventTopic topic, ReadOnlyCollection<Exception> exceptions)
     : base(string.Format(CultureInfo.InvariantCulture, "Exceptions occurred while firing the topic '{0}'.", topic != null ? topic.Uri : string.Empty))
 {
     this.topic = topic;
     this.exceptions = exceptions;
 }
コード例 #40
0
 private void CallWithThreadSwitch(IEventTopic eventTopic, Delegate subscriptionHandler, object sender, EventArgs e)
 {
     this.syncContextHolder.SyncContext.Send(
         delegate(object data)
             {
                 try
                 {
                     ((Delegate)data).DynamicInvoke(sender, e);
                 }
                 catch (TargetInvocationException exception)
                 {
                     this.HandleSubscriberMethodException(exception, eventTopic);
                 }
             },
         subscriptionHandler);
 }
コード例 #41
0
 /// <summary>
 /// Executes the subscription.
 /// </summary>
 /// <param name="eventTopic">The event topic.</param>
 /// <param name="sender">The sender.</param>
 /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
 /// <param name="subscriptionHandler">The subscription handler.</param>
 public abstract void Handle(IEventTopic eventTopic, object sender, EventArgs e, Delegate subscriptionHandler);
コード例 #42
0
ファイル: Subscription.cs プロジェクト: tiger2soft/bbv.Common
        /// <summary>
        /// Handler that is called when a topic is fired.
        /// </summary>
        /// <param name="eventTopic">The event topic.</param>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <param name="publication">The publication.</param>
        private void EventTopicFireHandler(IEventTopic eventTopic, object sender, EventArgs e, IPublication publication)
        {
            if (this.Subscriber == null)
            {
                return;
            }
            
            Delegate subscriptionHandler = this.CreateSubscriptionDelegate();
            if (subscriptionHandler == null)
            {
                return;
            }
            
            this.extensionHost.ForEach(extension => extension.RelayingEvent(eventTopic, publication, this, this.handler, sender, e));

            this.handler.Handle(eventTopic, sender, e, subscriptionHandler);

            this.extensionHost.ForEach(extension => extension.RelayedEvent(eventTopic, publication, this, this.handler, sender, e));
        }
コード例 #43
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SpontaneousPublication"/> class.
 /// </summary>
 /// <param name="topic">The topic.</param>
 /// <param name="publisher">The publisher.</param>
 /// <param name="eventArgsType">Type of the event args.</param>
 /// <param name="handlerRestriction">The handler restriction.</param>
 /// <param name="publicationMatchers">The publication matchers.</param>
 public SpontaneousPublication(IEventTopic topic, object publisher, Type eventArgsType, HandlerRestriction handlerRestriction, IList<IPublicationMatcher> publicationMatchers) :
     base(topic, publisher, handlerRestriction, publicationMatchers)
 {
     this.eventArgsType = eventArgsType;
 }
コード例 #44
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EventTopicException"/> class with the specified list of exceptions.
 /// </summary>
 /// <param name="topic">The <see cref="IEventTopic"/> instance whose subscribers incurred into an exception.</param>
 /// <param name="exceptions">The list of exceptions that occurred during the subscribers invocation.</param>
 public EventTopicException(IEventTopic topic, ReadOnlyCollection<Exception> exceptions)
     : base(string.Format("Exceptions occurred while firing the topic '{0}'.", topic.Uri))
 {
     this.topic = topic;
     this.exceptions = exceptions;
 }
コード例 #45
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EventTopicException"/> class.
 /// </summary>
 /// <param name="topic">The <see cref="IEventTopic"/> instance whose subscribers incurred into an exception.</param>
 /// <param name="innerException">The inner exception.</param>
 public EventTopicException(IEventTopic topic, Exception innerException)
     : base(string.Format("An exception occurred while firing the topic '{0}'.", topic.Uri), innerException)
 {
     this.topic = topic;
 }