public static MapStringTypeToCLRType ( string type, object value ) : object | ||
type | string | |
value | object | |
리턴 | object |
/// <summary> /// Reads a message from an XML file in the current directory. /// </summary> /// <returns>The message read from the XML file.</returns> public static List <MessagePropertyInfo> ReadProperties() { try { if (!File.Exists(propertiesFilePath)) { return(null); } using (var reader = new StreamReader(propertiesFilePath)) { using (var xmlReader = XmlReader.Create(reader)) { XNamespace ns = Namespace; var root = XElement.Load(xmlReader); var elements = root.Elements(ns + Property); return(elements.Where(element => !string.IsNullOrEmpty(element.Attribute(Key).Value.Trim()) && !string.IsNullOrEmpty(element.Attribute(Type).Value.Trim()) && !string.IsNullOrEmpty(element.Attribute(Value).Value.Trim())). Select(element => new MessagePropertyInfo { Key = element.Attribute(Key).Value.Trim(), Type = element.Attribute(Type).Value.Trim(), Value = ConversionHelper.MapStringTypeToCLRType(element.Attribute(Type).Value.Trim(), element.Attribute(Value).Value.Trim()) }).ToList()); } } } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex.Message); } return(null); }
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); } }
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); } }