Example #1
0
        public static void SetFormattedMessage(ServiceBusHelper serviceBusHelper, EventData message, FastColoredTextBox textBox)
        {
            if (serviceBusHelper == null)
            {
                throw new ArgumentNullException(nameof(serviceBusHelper), $"{nameof(serviceBusHelper)} parameter cannot be null");
            }

            if (message == null)
            {
                throw new ArgumentNullException(nameof(message), $"{nameof(message)} parameter cannot be null");
            }

            if (textBox == null)
            {
                throw new ArgumentNullException(nameof(textBox), $"{nameof(textBox)} parameter cannot be null");
            }

            InternalSetFormattedMessage(serviceBusHelper.GetMessageText(message, out _), textBox);
        }
Example #2
0
        public MessageForm(BrokeredMessage brokeredMessage, ServiceBusHelper serviceBusHelper, WriteToLogDelegate writeToLog)
        {
            this.brokeredMessage  = brokeredMessage;
            this.serviceBusHelper = serviceBusHelper;
            this.writeToLog       = writeToLog;
            InitializeComponent();

            cboBodyType.SelectedIndex = 0;

            messagePropertyGrid.SelectedObject = brokeredMessage;

            BodyType bodyType;

            txtMessageText.Text = XmlHelper.Indent(serviceBusHelper.GetMessageText(brokeredMessage, out bodyType));

            // Initialize the DataGridView.
            bindingSource.DataSource = new BindingList <MessagePropertyInfo>(brokeredMessage.Properties.Select(p => new MessagePropertyInfo(p.Key,
                                                                                                                                            p.Value.GetType().ToString().Substring(7),
                                                                                                                                            p.Value)).ToList());
            propertiesDataGridView.AutoGenerateColumns = false;
            propertiesDataGridView.AutoSize            = true;
            propertiesDataGridView.DataSource          = bindingSource;
            propertiesDataGridView.ForeColor           = SystemColors.WindowText;

            // Create the Name column
            var textBoxColumn = new DataGridViewTextBoxColumn
            {
                DataPropertyName = PropertyKey,
                Name             = PropertyKey,
                Width            = 138
            };

            propertiesDataGridView.Columns.Add(textBoxColumn);

            // Create the Type column
            var comboBoxColumn = new DataGridViewComboBoxColumn
            {
                DataSource       = types,
                DataPropertyName = PropertyType,
                Name             = PropertyType,
                Width            = 90,
                FlatStyle        = FlatStyle.Flat
            };

            propertiesDataGridView.Columns.Add(comboBoxColumn);

            // Create the Value column
            textBoxColumn = new DataGridViewTextBoxColumn
            {
                DataPropertyName = PropertyValue,
                Name             = PropertyValue,
                Width            = 138
            };
            propertiesDataGridView.Columns.Add(textBoxColumn);

            // Set Grid style
            propertiesDataGridView.EnableHeadersVisualStyles = false;

            // Set the selection background color for all the cells.
            propertiesDataGridView.DefaultCellStyle.SelectionBackColor = Color.FromArgb(92, 125, 150);
            propertiesDataGridView.DefaultCellStyle.SelectionForeColor = SystemColors.Window;

            // Set RowHeadersDefaultCellStyle.SelectionBackColor so that its default
            // value won't override DataGridView.DefaultCellStyle.SelectionBackColor.
            propertiesDataGridView.RowHeadersDefaultCellStyle.SelectionBackColor = Color.FromArgb(153, 180, 209);

            // Set the background color for all rows and for alternating rows.
            // The value for alternating rows overrides the value for all rows.
            propertiesDataGridView.RowsDefaultCellStyle.BackColor = SystemColors.Window;
            propertiesDataGridView.RowsDefaultCellStyle.ForeColor = SystemColors.ControlText;
            //propertiesDataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.White;
            //propertiesDataGridView.AlternatingRowsDefaultCellStyle.ForeColor = SystemColors.ControlText;

            // Set the row and column header styles.
            propertiesDataGridView.RowHeadersDefaultCellStyle.BackColor    = Color.FromArgb(215, 228, 242);
            propertiesDataGridView.RowHeadersDefaultCellStyle.ForeColor    = SystemColors.ControlText;
            propertiesDataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(215, 228, 242);
            propertiesDataGridView.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.ControlText;

            // Get Brokered Message Inspector classes
            cboSenderInspector.Items.Add(SelectBrokeredMessageInspector);
            cboSenderInspector.SelectedIndex = 0;

            if (serviceBusHelper.BrokeredMessageInspectors == null)
            {
                return;
            }
            foreach (var key in serviceBusHelper.BrokeredMessageInspectors.Keys)
            {
                cboSenderInspector.Items.Add(key);
            }
        }
Example #3
0
        public EventDataForm(EventData eventData, ServiceBusHelper serviceBusHelper, WriteToLogDelegate writeToLog)
        {
            this.eventData        = eventData;
            this.serviceBusHelper = serviceBusHelper;
            this.writeToLog       = writeToLog;
            InitializeComponent();

            messagePropertyGrid.SelectedObject = eventData;
            var messageText = serviceBusHelper.GetMessageText(eventData, out _);

            if (JsonSerializerHelper.IsJson(messageText))
            {
                txtMessageText.Language = Language.JSON;
                txtMessageText.Text     = JsonSerializerHelper.Indent(messageText);
            }
            else if (XmlHelper.IsXml(messageText))
            {
                txtMessageText.Language = Language.HTML;
                txtMessageText.Text     = XmlHelper.Indent(messageText);
            }
            else
            {
                txtMessageText.Language = Language.Custom;
                txtMessageText.Text     = string.IsNullOrEmpty(messageText) ? "": messageText;
            }

            // Initialize the DataGridView.
            bindingSource.DataSource = new BindingList <MessagePropertyInfo>(eventData.Properties.Select(p => new MessagePropertyInfo(p.Key,
                                                                                                                                      p.Value.GetType().ToString().Substring(7),
                                                                                                                                      p.Value)).ToList());
            propertiesDataGridView.AutoGenerateColumns = false;
            propertiesDataGridView.AutoSize            = true;
            propertiesDataGridView.DataSource          = bindingSource;
            propertiesDataGridView.ForeColor           = SystemColors.WindowText;

            // Create the Name column
            var textBoxColumn = new DataGridViewTextBoxColumn
            {
                DataPropertyName = PropertyKey,
                Name             = PropertyKey,
                Width            = 138
            };

            propertiesDataGridView.Columns.Add(textBoxColumn);

            // Create the Type column
            var comboBoxColumn = new DataGridViewComboBoxColumn
            {
                DataSource       = types,
                DataPropertyName = PropertyType,
                Name             = PropertyType,
                Width            = 90,
                FlatStyle        = FlatStyle.Flat
            };

            propertiesDataGridView.Columns.Add(comboBoxColumn);

            // Create the Value column
            textBoxColumn = new DataGridViewTextBoxColumn
            {
                DataPropertyName = PropertyValue,
                Name             = PropertyValue,
                Width            = 138
            };
            propertiesDataGridView.Columns.Add(textBoxColumn);

            // Set Grid style
            propertiesDataGridView.EnableHeadersVisualStyles = false;

            // Set the selection background color for all the cells.
            propertiesDataGridView.DefaultCellStyle.SelectionBackColor = Color.FromArgb(92, 125, 150);
            propertiesDataGridView.DefaultCellStyle.SelectionForeColor = SystemColors.Window;

            // Set RowHeadersDefaultCellStyle.SelectionBackColor so that its default
            // value won't override DataGridView.DefaultCellStyle.SelectionBackColor.
            propertiesDataGridView.RowHeadersDefaultCellStyle.SelectionBackColor = Color.FromArgb(153, 180, 209);

            // Set the background color for all rows and for alternating rows.
            // The value for alternating rows overrides the value for all rows.
            propertiesDataGridView.RowsDefaultCellStyle.BackColor = SystemColors.Window;
            propertiesDataGridView.RowsDefaultCellStyle.ForeColor = SystemColors.ControlText;
            //propertiesDataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.White;
            //propertiesDataGridView.AlternatingRowsDefaultCellStyle.ForeColor = SystemColors.ControlText;

            // Set the row and column header styles.
            propertiesDataGridView.RowHeadersDefaultCellStyle.BackColor    = Color.FromArgb(215, 228, 242);
            propertiesDataGridView.RowHeadersDefaultCellStyle.ForeColor    = SystemColors.ControlText;
            propertiesDataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(215, 228, 242);
            propertiesDataGridView.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.ControlText;
        }
Example #4
0
        private async void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                using (var form = new SelectEntityForm(SelectEntityDialogTitle, SelectEntityGrouperTitle, SelectEntityLabelText))
                {
                    if (form.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }
                    if (string.IsNullOrWhiteSpace(form.Path))
                    {
                        return;
                    }
                    BodyType bodyType;
                    if (!Enum.TryParse(cboBodyType.Text, true, out bodyType))
                    {
                        bodyType = BodyType.Stream;
                    }
                    var messageSender = serviceBusHelper.MessagingFactory.CreateMessageSender(form.Path);
                    var messages      = brokeredMessages != null ?
                                        new List <BrokeredMessage>(brokeredMessages) :
                                        new List <BrokeredMessage>(new [] { brokeredMessage });
                    var outboundMessages = new List <BrokeredMessage>();
                    foreach (var message in messages)
                    {
                        BrokeredMessage outboundMessage;
                        if (bodyType == BodyType.Wcf)
                        {
                            var wcfUri = serviceBusHelper.IsCloudNamespace ?
                                         new Uri(serviceBusHelper.NamespaceUri, messageSender.Path) :
                                         new UriBuilder
                            {
                                Host   = serviceBusHelper.NamespaceUri.Host,
                                Path   = $"{serviceBusHelper.NamespaceUri.AbsolutePath}/{messageSender.Path}",
                                Scheme = "sb"
                            }.Uri;
                            outboundMessage = serviceBusHelper.CreateMessageForWcfReceiver(message.Clone(txtMessageText.Text),
                                                                                           0,
                                                                                           false,
                                                                                           false,
                                                                                           wcfUri);
                        }
                        else
                        {
                            if (brokeredMessage != null)
                            {
                                // For body type ByteArray cloning is not an option. When cloned, supplied body can be only of a string or stream types, but not byte array :(
                                outboundMessage = bodyType == BodyType.ByteArray ?
                                                  brokeredMessage.CloneWithByteArrayBodyType(txtMessageText.Text) :
                                                  brokeredMessage.Clone(txtMessageText.Text);
                            }
                            else
                            {
                                var messageText = serviceBusHelper.GetMessageText(message, out bodyType);

                                // For body type ByteArray cloning is not an option. When cloned, supplied body can be only of a string or stream types, but not byte array :(
                                outboundMessage = bodyType == BodyType.ByteArray ?
                                                  message.CloneWithByteArrayBodyType(messageText) :
                                                  message.Clone(messageText);
                            }

                            outboundMessage = serviceBusHelper.CreateMessageForApiReceiver(outboundMessage,
                                                                                           0,
                                                                                           false,
                                                                                           false,
                                                                                           false,
                                                                                           bodyType,
                                                                                           cboSenderInspector.SelectedIndex > 0 ?
                                                                                           Activator.CreateInstance(serviceBusHelper.BrokeredMessageInspectors[cboSenderInspector.Text]) as IBrokeredMessageInspector :
                                                                                           null);
                        }
                        var warningCollection = new ConcurrentBag <string>();
                        foreach (var messagePropertyInfo in bindingSource.Cast <MessagePropertyInfo>())
                        {
                            try
                            {
                                if (string.Compare(messagePropertyInfo.Key, "DeadLetterReason",
                                                   StringComparison.InvariantCultureIgnoreCase) == 0 ||
                                    string.Compare(messagePropertyInfo.Key, "DeadLetterErrorDescription",
                                                   StringComparison.InvariantCultureIgnoreCase) == 0)
                                {
                                    continue;
                                }
                                messagePropertyInfo.Key = messagePropertyInfo.Key.Trim();
                                if (messagePropertyInfo.Type != StringType && messagePropertyInfo.Value == null)
                                {
                                    warningCollection.Add(string.Format(CultureInfo.CurrentUICulture, PropertyValueCannotBeNull, messagePropertyInfo.Key));
                                }
                                else
                                {
                                    if (outboundMessage.Properties.ContainsKey(messagePropertyInfo.Key))
                                    {
                                        outboundMessage.Properties[messagePropertyInfo.Key] = ConversionHelper.MapStringTypeToCLRType(messagePropertyInfo.Type, messagePropertyInfo.Value);
                                    }
                                    else
                                    {
                                        outboundMessage.Properties.Add(messagePropertyInfo.Key, ConversionHelper.MapStringTypeToCLRType(messagePropertyInfo.Type, messagePropertyInfo.Value));
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                warningCollection.Add(string.Format(CultureInfo.CurrentUICulture, PropertyConversionError, messagePropertyInfo.Key, ex.Message));
                            }
                        }
                        if (warningCollection.Count <= 0)
                        {
                            outboundMessages.Add(outboundMessage);
                            continue;
                        }
                        var builder  = new StringBuilder(WarningHeader);
                        var warnings = warningCollection.ToArray <string>();
                        for (var i = 0; i < warningCollection.Count; i++)
                        {
                            builder.AppendFormat(WarningFormat, warnings[i]);
                        }
                        writeToLog(builder.ToString());
                    }
                    if (!outboundMessages.Any())
                    {
                        return;
                    }
                    var sent      = outboundMessages.Count;
                    var stopwatch = new Stopwatch();
                    stopwatch.Start();
                    await messageSender.SendBatchAsync(outboundMessages);

                    stopwatch.Stop();
                    writeToLog(string.Format(MessageSentMessage, sent, messageSender.Path, stopwatch.ElapsedMilliseconds));
                    if (brokeredMessages != null)
                    {
                        Close();
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }
Example #5
0
        private async void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                using (var form = CreateSelectEntityForm())
                {
                    if (form.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }
                    if (string.IsNullOrWhiteSpace(form.Path))
                    {
                        return;
                    }

                    Application.UseWaitCursor = true;
                    try
                    {
                        if (!Enum.TryParse <BodyType>(cboBodyType.Text, true, out var bodyType))
                        {
                            bodyType = BodyType.Stream;
                        }
                        var messageSender = serviceBusHelper.MessagingFactory.CreateMessageSender(form.Path);
                        var messages      = brokeredMessages != null ?
                                            new List <BrokeredMessage>(brokeredMessages) :
                                            new List <BrokeredMessage>(new[] { brokeredMessage });
                        var outboundMessages = new List <BrokeredMessage>();
                        var sequenceNumbers  = new List <long>(); // Only used when removing messages
                        foreach (var message in messages)
                        {
                            BrokeredMessage outboundMessage;
                            if (bodyType == BodyType.Wcf)
                            {
                                var wcfUri = serviceBusHelper.IsCloudNamespace ?
                                             new Uri(serviceBusHelper.NamespaceUri, messageSender.Path) :
                                             new UriBuilder
                                {
                                    Host   = serviceBusHelper.NamespaceUri.Host,
                                    Path   = $"{serviceBusHelper.NamespaceUri.AbsolutePath}/{messageSender.Path}",
                                    Scheme = "sb"
                                }.Uri;
                                outboundMessage = serviceBusHelper.CreateMessageForWcfReceiver(message.Clone(txtMessageText.Text),
                                                                                               0,
                                                                                               false,
                                                                                               false,
                                                                                               wcfUri);
                            }
                            else
                            {
                                if (brokeredMessage != null)
                                {
                                    // For body type ByteArray cloning is not an option. When cloned, supplied body can be only of a string or stream types, but not byte array :(
                                    outboundMessage = bodyType == BodyType.ByteArray ?
                                                      brokeredMessage.CloneWithByteArrayBodyType(txtMessageText.Text) :
                                                      brokeredMessage.Clone(txtMessageText.Text);
                                }
                                else
                                {
                                    var messageText = serviceBusHelper.GetMessageText(message, out bodyType);

                                    // For body type ByteArray cloning is not an option. When cloned, supplied body can be only of a string or stream types, but not byte array :(
                                    outboundMessage = bodyType == BodyType.ByteArray ?
                                                      message.CloneWithByteArrayBodyType(messageText) :
                                                      message.Clone(messageText);
                                }

                                outboundMessage = serviceBusHelper.CreateMessageForApiReceiver(outboundMessage,
                                                                                               0,
                                                                                               chkNewMessageId.Checked,
                                                                                               false,
                                                                                               bodyType,
                                                                                               cboSenderInspector.SelectedIndex > 0 ?
                                                                                               Activator.CreateInstance(serviceBusHelper.BrokeredMessageInspectors[cboSenderInspector.Text]) as IBrokeredMessageInspector :
                                                                                               null);
                            }

                            sequenceNumbers.Add(message.SequenceNumber);

                            var warningCollection = new ConcurrentBag <string>();
                            foreach (var messagePropertyInfo in bindingSource.Cast <MessagePropertyInfo>())
                            {
                                try
                                {
                                    if (string.Compare(messagePropertyInfo.Key, "DeadLetterReason",
                                                       StringComparison.InvariantCultureIgnoreCase) == 0 ||
                                        string.Compare(messagePropertyInfo.Key, "DeadLetterErrorDescription",
                                                       StringComparison.InvariantCultureIgnoreCase) == 0)
                                    {
                                        continue;
                                    }
                                    messagePropertyInfo.Key = messagePropertyInfo.Key.Trim();
                                    if (messagePropertyInfo.Type != StringType && messagePropertyInfo.Value == null)
                                    {
                                        warningCollection.Add(string.Format(CultureInfo.CurrentUICulture, PropertyValueCannotBeNull, messagePropertyInfo.Key));
                                    }
                                    else
                                    {
                                        if (outboundMessage.Properties.ContainsKey(messagePropertyInfo.Key))
                                        {
                                            outboundMessage.Properties[messagePropertyInfo.Key] = ConversionHelper.MapStringTypeToCLRType(messagePropertyInfo.Type, messagePropertyInfo.Value);
                                        }
                                        else
                                        {
                                            outboundMessage.Properties.Add(messagePropertyInfo.Key, ConversionHelper.MapStringTypeToCLRType(messagePropertyInfo.Type, messagePropertyInfo.Value));
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    warningCollection.Add(string.Format(CultureInfo.CurrentUICulture, PropertyConversionError, messagePropertyInfo.Key, ex.Message));
                                }
                            }
                            if (warningCollection.Count <= 0)
                            {
                                outboundMessages.Add(outboundMessage);
                                continue;
                            }
                            var builder  = new StringBuilder(WarningHeader);
                            var warnings = warningCollection.ToArray <string>();
                            for (var i = 0; i < warningCollection.Count; i++)
                            {
                                builder.AppendFormat(WarningFormat, warnings[i]);
                            }
                            writeToLog(builder.ToString());
                        }
                        if (!outboundMessages.Any())
                        {
                            return;
                        }
                        var sent      = outboundMessages.Count;
                        var stopwatch = new Stopwatch();
                        stopwatch.Start();
                        if (chkRemove.Checked)
                        {
                            var messageHandler = CreateDeadLetterMessageHandler();

                            var result = await messageHandler.MoveMessages(messageSender,
                                                                           sequenceNumbers, outboundMessages);

                            RemovedSequenceNumbers = result.DeletedSequenceNumbers;
                            stopwatch.Stop();

                            if (result.TimedOut)
                            {
                                var messageText = messageHandler.GetFailureExplanation(result, outboundMessages.Count, delete: false);
                                writeToLog(messageText);
                                Application.UseWaitCursor = false;
                                MessageBox.Show(messageText, "Not all selected messages were moved",
                                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                            else
                            {
                                writeToLog(string.Format(MessageMovedMessage, result.DeletedSequenceNumbers.Count,
                                                         messageSender.Path, stopwatch.ElapsedMilliseconds));
                            }
                        }
                        else
                        {
                            var messageIndex = 0;
                            try
                            {
                                while (messageIndex < outboundMessages.Count)
                                {
                                    await messageSender.SendAsync(outboundMessages[messageIndex++]);
                                }
                            }
                            catch (Exception exception)
                            {
                                Application.UseWaitCursor = false;
                                var messageText = $"{outboundMessages.Count} were selected but only" +
                                                  $" {messageIndex} messages were sent. The error message is: {exception.Message}";
                                MessageBox.Show(messageText, "Not all selected messages were sent",
                                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }

                            stopwatch.Stop();
                            writeToLog(string.Format(MessageSentMessage, sent, messageSender.Path, stopwatch.ElapsedMilliseconds));
                        }

                        if (brokeredMessages != null)
                        {
                            Close();
                        }
                    }
                    finally
                    {
                        Application.UseWaitCursor = false;
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }