Пример #1
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);
            }
        }
Пример #2
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);
                        }
                        outboundMessage.Properties.Clear();
                        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);
            }
        }