Пример #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 was successfully processed and should not
 /// be redelivered.
 /// </summary>
 /// <param name="receiverMessage">The message to acknowledge.</param>
 public static Task AcknowledgeAsync(this IReceiverMessage receiverMessage)
 {
     if (receiverMessage is null)
     {
         throw new ArgumentNullException(nameof(receiverMessage));
     }
     return(receiverMessage.AcknowledgeAsync(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);
        }
 public Task DataReceivedAsync(IReceiverMessage message)
 {
     WriteLine(FormatMessage(message.StringPayload));
     return(message.AcknowledgeAsync());
 }
 /// <summary>
 /// Indicates that the message was successfully processed and should not
 /// be redelivered.
 /// </summary>
 /// <param name="receiverMessage">The message to acknowledge.</param>
 public static Task AcknowledgeAsync(this IReceiverMessage receiverMessage) =>
 receiverMessage.AcknowledgeAsync(CancellationToken.None);
 public static void Acknowledge(this IReceiverMessage receiverMessage) =>
 Sync(() => receiverMessage.AcknowledgeAsync());
 private Task OnMessageReceived(IReceiverMessage message)
 {
     Console.WriteLine($"Received message: '{message.StringPayload}'");
     return(message.AcknowledgeAsync());
 }