Example #1
0
        /// <summary>
        ///     Publish_s the implementation.
        /// </summary>
        /// <param name="callback">The callback.</param>
        /// <param name="message">The message.</param>
        /// <param name="publishMethod">The publish method.</param>
        /// <param name="options">The options.</param>
        /// <param name="publishToOriginator">if set to <c>true</c> [publish to originator].</param>
        /// <param name="mergeAction">The merge action.</param>
        /// <returns></returns>
        private Task <long> Publish_Impl(Func <T, PublishOptions, bool, Task <long> > callback, T message, PublishMethod publishMethod, PublishOptions options = PublishOptions.None, bool publishToOriginator = false, Action <T, T> mergeAction = null)
        {
            if (Equals(message, default(T)))
            {
                return(null);
            }

            if (Suppression.IsActive( ))
            {
                return(null);
            }

            DeferredChannelMessageContext deferredMessageContext = null;

            try
            {
                /////
                // If running in a delayed context, use the current message store.
                /////
                if (publishMethod == PublishMethod.Deferred)
                {
                    deferredMessageContext = DeferredChannelMessageContext.GetContext( );

                    if (deferredMessageContext.ContextType != ContextType.Attached && !DeferredChannelMessageContext.SuppressNoContextWarning)
                    {
                        EventLog.Application.WriteWarning("Channel {0} has been created with PublishMethod.Deferred with no DeferredChannelMessageContext set. Messages will be sent immediately. Either set a DeferredChannelMessageContext or publish the message with PublishMethod.Immediate.", ChannelName);
                    }
                }

                if (deferredMessageContext != null &&
                    deferredMessageContext.ContextType == ContextType.Attached)
                {
                    deferredMessageContext.AddOrUpdateMessage(ChannelName, message, mergeAction);
                }
                else
                {
                    return(callback(message, options, publishToOriginator));
                }
            }
            finally
            {
                if (deferredMessageContext != null)
                {
                    deferredMessageContext.Dispose( );
                }
            }

            return(null);
        }
Example #2
0
        /// <summary>
        ///     Publishes the message.
        /// </summary>
        /// <param name="callback">The callback.</param>
        /// <param name="message">The message.</param>
        /// <param name="options">The options.</param>
        /// <param name="publishToOriginator">if set to <c>true</c>, the message will be published to the originator also.</param>
        /// <returns></returns>
        private Task <long> PublishMessage(Func <ISubscriber, byte[], CommandFlags, Task <long> > callback, T message, PublishOptions options, bool publishToOriginator = false)
        {
            if (Equals(message, default(T)))
            {
                return(null);
            }

            if (Suppression.IsActive( ))
            {
                return(null);
            }

            ISubscriber subscriber = Subscriber;

            if (subscriber != null)
            {
                var payload = new ChannelMessage <T>(message, publishToOriginator);

                if (subscriber.Multiplexer.IsConnected)
                {
                    try
                    {
                        var flags = CommandFlags.None;

                        if (options == PublishOptions.FireAndForget)
                        {
                            flags = CommandFlags.FireAndForget;
                        }

                        byte[] serialize = ChannelMessage <T> .Serialize(payload);

                        byte[] compress = CompressionHelper.Compress(serialize);

                        return(callback(subscriber, compress, flags));
                    }
                    catch (StackExchange.Redis.RedisConnectionException exc)
                    {
                        EventLog.Application.WriteWarning("Redis appears to be down. {0}", exc);
                    }
                }
            }

            return(null);
        }