Пример #1
0
        private async Task OnMessageReceived(IReceiverMessage message)
        {
            if (message.Headers.TryGetValue("operation", out string operation))
            {
                if (operation == "create")
                {
                    await Database.CreateAsync(message.StringPayload);

                    await message.AcknowledgeAsync();
                }
                else if (operation == "update")
                {
                    await Database.UpdateAsync(message.StringPayload);

                    await message.AcknowledgeAsync();
                }
                else if (operation == "delete")
                {
                    await Database.DeleteAsync(message.StringPayload);

                    await message.AcknowledgeAsync();
                }
                else
                {
                    // TODO: Send error log - invalid message
                    await message.RejectAsync();
                }
            }
            else
            {
                // TODO: Send error log - invalid message
                await message.RejectAsync();
            }
        }
        private async Task OnMessageReceivedAsync(IReceiverMessage message)
        {
            var newSettings = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            try
            {
                JsonConvert.PopulateObject(message.StringPayload, newSettings);
            }
            catch
            {
                await message.RejectAsync().ConfigureAwait(false);

                return;
            }

            var changed = Data.Count != newSettings.Count;

            if (!changed)
            {
                foreach (var newSetting in newSettings)
                {
                    if (Data.ContainsKey(newSetting.Key))
                    {
                        if (Data[newSetting.Key] != newSetting.Value)
                        {
                            changed = true;
                            break;
                        }
                    }
                    else
                    {
                        changed = true;
                        break;
                    }
                }
            }

            if (changed)
            {
                IDictionary <string, string> previousData = null;

                if (message.Headers.TryGetValue("RevertAfterMilliseconds", out int milliseconds) && milliseconds > 0)
                {
                    previousData = Data;
                }

                Data = newSettings;
                OnReload();

                if (previousData != null)
                {
                    await message.AcknowledgeAsync().ConfigureAwait(false);
                    await RevertDataAfterDelay(previousData, milliseconds).ConfigureAwait(false);

                    return;
                }
            }

            await message.AcknowledgeAsync().ConfigureAwait(false);
        }
        /// <summary>
        /// Indicates that the message could not be successfully processed and should
        /// not be redelivered.
        /// </summary>
        /// <param name="receiverMessage">The message to reject.</param>
        public static Task RejectAsync(this IReceiverMessage receiverMessage)
        {
            if (receiverMessage is null)
            {
                throw new ArgumentNullException(nameof(receiverMessage));
            }

            return(receiverMessage.RejectAsync(CancellationToken.None));
        }
        private async Task OnMessageReceivedAsync(IReceiverMessage message)
        {
            var newSettings = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            try
            {
                JsonConvert.PopulateObject(message.StringPayload, newSettings);
            }
            catch
            {
                await message.RejectAsync().ConfigureAwait(false);

                return;
            }

            if (IsChanged(newSettings, message.Headers))
            {
                Data = newSettings;
                OnReload();
            }

            await message.AcknowledgeAsync().ConfigureAwait(false);
        }
 /// <summary>
 /// Indicates that the message could not be successfully processed and should
 /// not be redelivered.
 /// </summary>
 /// <param name="receiverMessage">The message to reject.</param>
 public static Task RejectAsync(this IReceiverMessage receiverMessage) =>
 receiverMessage.RejectAsync(CancellationToken.None);
 public static void Reject(this IReceiverMessage receiverMessage) =>
 Sync(() => receiverMessage.RejectAsync());