Beispiel #1
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);
            }
        }
Beispiel #2
0
        private 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);
                    BrokeredMessage outboundMessage;
                    if (bodyType == BodyType.Wcf)
                    {
                        var wcfUri = serviceBusHelper.IsCloudNamespace ?
                                     new Uri(serviceBusHelper.NamespaceUri, messageSender.Path) :
                                     new UriBuilder
                        {
                            Host   = serviceBusHelper.NamespaceUri.Host,
                            Path   = string.Format("{0}/{1}", serviceBusHelper.NamespaceUri.AbsolutePath, messageSender.Path),
                            Scheme = "sb"
                        }.Uri;
                        outboundMessage = serviceBusHelper.CreateMessageForWcfReceiver(brokeredMessage.Clone(txtMessageText.Text),
                                                                                       0,
                                                                                       false,
                                                                                       false,
                                                                                       wcfUri);
                    }
                    else
                    {
                        outboundMessage = serviceBusHelper.CreateMessageForApiReceiver(brokeredMessage.Clone(txtMessageText.Text),
                                                                                       0,
                                                                                       false,
                                                                                       false,
                                                                                       false,
                                                                                       bodyType,
                                                                                       cboSenderInspector.SelectedIndex > 0 ?
                                                                                       Activator.CreateInstance(serviceBusHelper.BrokeredMessageInspectors[cboSenderInspector.Text]) as IBrokeredMessageInspector :
                                                                                       null);
                    }
                    outboundMessage.Properties.Clear();
                    var warningCollection = new ConcurrentBag <string>();
                    foreach (var messagePropertyInfo in bindingSource.Cast <MessagePropertyInfo>())
                    {
                        try
                        {
                            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)
                    {
                        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());
                    }

                    long elapsedMilliseconds;
                    serviceBusHelper.SendMessage(messageSender,
                                                 outboundMessage,
                                                 0,
                                                 bodyType == BodyType.Wcf,
                                                 false,
                                                 true,
                                                 true,
                                                 out elapsedMilliseconds);
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }
Beispiel #3
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);
            }
        }