コード例 #1
0
        private void OnServiceClientMessageReceived(object sender, PublishedEventArgs e)
        {
            if (e.Message == null)
            {
                return;
            }

            var messageType = e.Message.GetType();

            if (!this.messageHandlers.ContainsKey(messageType))
            {
                return;
            }

            var handlers = this.messageHandlers[messageType];

            foreach (var handler in handlers)
            {
                handler.Invoke(e.Message);
            }
        }
コード例 #2
0
        /// <summary>
        /// Called when a new command message is published.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="PublishedEventArgs" /> instance containing the event data.</param>
        private void OnMessagePublished(object sender, PublishedEventArgs e)
        {
            var messageType = e.Message.GetType();

            IEnumerable <IServerMessageHandler> handlers;

            try
            {
                handlers = this.messageHandlerResolver.GetHandlers(e.Message, this.response);
            }
            catch (ArgumentException ex)
            {
                if (ex.Message != null && ex.Message.Contains("Could not locate any server message handlers"))
                {
                    return;
                }

                throw;
            }
            catch (OutOfMemoryException ome)
            {
                this.log.Error(ome);
                this.log.Error(Environment.StackTrace);
                //throw; testing
                return;
            }
            catch (Exception ex)
            {
                this.log.Error(ex);
                this.log.Error(Environment.StackTrace);
                //throw; testing
                return;
            }

            var isMessageHandled = false;

            foreach (var handler in handlers)
            {
                try
                {
                    handler.Handle(e.Message, this.response);
                    isMessageHandled = true;
                }
                catch (Exception exception)
                {
                    this.log.Error($"{nameof(handler)} has an error in the handle function when receiving a WCF message. ", exception);
                }
            }

            if (!isMessageHandled)
            {
                return;
            }

            try
            {
                var messageTypeString = messageType.ToString();
                if (!this.messageSubscribers.ContainsKey(messageTypeString))
                {
                    return;
                }

                lock (this.padlock)
                {
                    var subscribers   = this.messageSubscribers[messageTypeString];
                    var unsubscribers = new List <ICommandCallbackContract>();
                    foreach (var subscriber in subscribers)
                    {
                        try
                        {
                            subscriber.HandleSubscribe(e.Message);
                        }
                        catch (CommunicationObjectAbortedException ee)
                        {
                            this.log.Debug(ee.ToString());
                            unsubscribers.Add(subscriber);
                        }
                    }

                    foreach (var unsubscriber in unsubscribers)
                    {
                        subscribers.Remove(unsubscriber);
                    }
                }
            }
            catch (Exception ex)
            {
                this.log.Error(ex);
            }
        }