Esempio n. 1
0
        private void btnRename_Click(object sender, EventArgs e)
        {
            var ns  = serviceBusHelper.ServiceBusNamespaces[cboServiceBusNamespace.Text];
            var key = cboServiceBusNamespace.Text;

            using (var parameterForm = new ParameterForm("Enter the new key for the Service Bus namespace",
                                                         new List <string> {
                "Key"
            },
                                                         new List <string> {
                key
            },
                                                         new List <bool> {
                false
            }))
            {
                if (parameterForm.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                var newKey = parameterForm.ParameterValues[0];
                if (newKey == key)
                {
                    MainForm.StaticWriteToLog("The new key of the Service Bus namespace was the same as before.");
                    return;
                }

                var existingKeys = serviceBusHelper.ServiceBusNamespaces.Keys;
                if (existingKeys.Contains(newKey))
                {
                    MainForm.StaticWriteToLog("A Service Bus namespace key must be unique");
                    return;
                }

                if (string.IsNullOrWhiteSpace(newKey))
                {
                    MainForm.StaticWriteToLog("The key of the Service Bus namespace cannot be null.");
                    return;
                }

                var itemIndex = cboServiceBusNamespace.SelectedIndex;
                ConfigurationHelper.UpdateServiceBusNamespace(configFileUse, key, newKey, newValue: null, MainForm.StaticWriteToLog);

                ignoreSelectedIndexChange = true;
                serviceBusHelper.ServiceBusNamespaces.Remove(key);
                serviceBusHelper.ServiceBusNamespaces[newKey] = ns;
                cboServiceBusNamespace.Items[itemIndex]       = newKey.GetHashCode();
                cboServiceBusNamespace.Items[itemIndex]       = newKey;
                cboServiceBusNamespace.Text = newKey;
                ignoreSelectedIndexChange   = false;

                MainForm.StaticWriteToLog($"Renamed '{key}' to '{newKey}'");
            }
        }
Esempio n. 2
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                txtUri.Text = txtUri.Text.Trim();
                if (string.IsNullOrWhiteSpace(txtUri.Text))
                {
                    MainForm.StaticWriteToLog("The connection string of the Service Bus namespace cannot be null.");
                    return;
                }
                ServiceBusConnectionStringBuilder serviceBusConnectionStringBuilder;
                try
                {
                    serviceBusConnectionStringBuilder = new ServiceBusConnectionStringBuilder(txtUri.Text);
                }
                catch (Exception)
                {
                    MainForm.StaticWriteToLog("The format of the connection string is invalid.");
                    return;
                }

                if (serviceBusConnectionStringBuilder.Endpoints == null ||
                    serviceBusConnectionStringBuilder.Endpoints.Count == 0)
                {
                    MainForm.StaticWriteToLog("The connection string does not contain any endpoint.");
                    return;
                }

                var host = serviceBusConnectionStringBuilder.Endpoints.ToArray()[0].Host;

                var index = host.IndexOf(".", StringComparison.Ordinal);

                var key = index > 0 ? CultureInfo.CurrentCulture.TextInfo.ToTitleCase(host.Substring(0, index)) : "MyNamespace";

                using (var parameterForm = new ParameterForm("Enter the key for the Service Bus namespace",
                                                             new List <string> {
                    "Key"
                },
                                                             new List <string> {
                    key
                },
                                                             new List <bool> {
                    false
                }))
                {
                    if (parameterForm.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }
                    key = parameterForm.ParameterValues[0];
                    if (string.IsNullOrWhiteSpace(key))
                    {
                        MainForm.StaticWriteToLog("The key of the Service Bus namespace cannot be null.");
                        return;
                    }
                    var value = txtUri.Text;

                    try
                    {
                        ConfigurationHelper.AddServiceBusNamespace(configFileUse, key, value, MainForm.StaticWriteToLog);
                    }
                    catch (ArgumentNullException ex)
                    {
                        MainForm.StaticWriteToLog(ex.Message);
                    }

                    serviceBusHelper.ServiceBusNamespaces.Add(key, ServiceBusNamespace.GetServiceBusNamespace(key, value, MainForm.StaticWriteToLog));
                    cboServiceBusNamespace.Items.Clear();
                    cboServiceBusNamespace.Items.Add(SelectServiceBusNamespace);
                    cboServiceBusNamespace.Items.Add(EnterConnectionString);

                    // ReSharper disable once CoVariantArrayConversion
                    cboServiceBusNamespace.Items.AddRange(serviceBusHelper.ServiceBusNamespaces.Keys.OrderBy(s => s).ToArray());
                    cboServiceBusNamespace.Text = key;
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                txtUri.Text = txtUri.Text.Trim();
                if (string.IsNullOrWhiteSpace(txtUri.Text))
                {
                    MainForm.StaticWriteToLog("The connection string of the Service Bus namespace cannot be null.");
                    return;
                }
                ServiceBusConnectionStringBuilder serviceBusConnectionStringBuilder;
                try
                {
                    serviceBusConnectionStringBuilder = new ServiceBusConnectionStringBuilder(txtUri.Text);
                }
                catch (Exception)
                {
                    MainForm.StaticWriteToLog("The format of the connection string is invalid.");
                    return;
                }

                if (serviceBusConnectionStringBuilder.Endpoints == null ||
                    serviceBusConnectionStringBuilder.Endpoints.Count == 0)
                {
                    MainForm.StaticWriteToLog("The connection string does not contain any endpoint.");
                    return;
                }

                var host = serviceBusConnectionStringBuilder.Endpoints.ToArray()[0].Host;

                var index = host.IndexOf(".", StringComparison.Ordinal);

                var key = index > 0 ? CultureInfo.CurrentCulture.TextInfo.ToTitleCase(host.Substring(0, index)) : "MyNamespace";

                using (var parameterForm = new ParameterForm("Enter the key for the Service Bus namespace",
                                                             new List <string> {
                    "Key"
                },
                                                             new List <string> {
                    key
                },
                                                             new List <bool> {
                    false
                }))
                {
                    if (parameterForm.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }
                    key = parameterForm.ParameterValues[0];
                    if (string.IsNullOrWhiteSpace(key))
                    {
                        MainForm.StaticWriteToLog("The key of the Service Bus namespace cannot be null.");
                        return;
                    }
                    var value                = txtUri.Text;
                    var configuration        = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    var configurationSection = configuration.Sections["serviceBusNamespaces"];
                    var directory            = Path.GetDirectoryName(configuration.FilePath);
                    if (string.IsNullOrEmpty(directory))
                    {
                        MainForm.StaticWriteToLog("The directory of the configuration file cannot be null.");
                        return;
                    }
                    var appConfig = Path.Combine(directory, "..\\..\\App.config");
                    configurationSection.SectionInformation.ForceSave = true;
                    var xml         = configurationSection.SectionInformation.GetRawXml();
                    var xmlDocument = new XmlDocument();
                    xmlDocument.LoadXml(xml);
                    var node = xmlDocument.CreateElement("add");
                    node.SetAttribute("key", key);
                    node.SetAttribute("value", value);
                    xmlDocument.DocumentElement?.AppendChild(node);
                    configurationSection.SectionInformation.SetRawXml(xmlDocument.OuterXml);
                    configuration.Save(ConfigurationSaveMode.Modified);
                    ConfigurationManager.RefreshSection("serviceBusNamespaces");

                    if (File.Exists(appConfig))
                    {
                        var exeConfigurationFileMap = new ExeConfigurationFileMap()
                        {
                            ExeConfigFilename = appConfig
                        };
                        configuration        = ConfigurationManager.OpenMappedExeConfiguration(exeConfigurationFileMap, ConfigurationUserLevel.None);
                        configurationSection = configuration.Sections["serviceBusNamespaces"];
                        configurationSection.SectionInformation.ForceSave = true;
                        xml         = configurationSection.SectionInformation.GetRawXml();
                        xmlDocument = new XmlDocument();
                        xmlDocument.LoadXml(xml);
                        node = xmlDocument.CreateElement("add");
                        node.SetAttribute("key", key);
                        node.SetAttribute("value", value);
                        xmlDocument.DocumentElement?.AppendChild(node);
                        configurationSection.SectionInformation.SetRawXml(xmlDocument.OuterXml);
                        configuration.Save(ConfigurationSaveMode.Modified);
                        ConfigurationManager.RefreshSection("serviceBusNamespaces");
                    }

                    serviceBusHelper.ServiceBusNamespaces.Add(key, MainForm.GetServiceBusNamespace(key, value));
                    cboServiceBusNamespace.Items.Clear();
                    cboServiceBusNamespace.Items.Add(SelectServiceBusNamespace);
                    cboServiceBusNamespace.Items.Add(EnterConnectionString);

                    // ReSharper disable once CoVariantArrayConversion
                    cboServiceBusNamespace.Items.AddRange(serviceBusHelper.ServiceBusNamespaces.Keys.OrderBy(s => s).ToArray());
                    cboServiceBusNamespace.Text = key;
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }