示例#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}'");
            }
        }
示例#2
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                var key = cboServiceBusNamespace.Text;
                var isNewServiceBusNamespace = (key == EnterConnectionString);
                ServiceBusConnectionStringBuilder serviceBusConnectionStringBuilder;
                try
                {
                    BuildCurrentConnectionString();
                    if (string.IsNullOrWhiteSpace(ConnectionString))
                    {
                        MainForm.StaticWriteToLog(ConnectionStringCannotBeNull);
                        return;
                    }
                    serviceBusConnectionStringBuilder = new ServiceBusConnectionStringBuilder(ConnectionString);
                }
                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;
                }

                if (serviceBusConnectionStringBuilder.EntityPath != null)
                {
                    MainForm.StaticWriteToLog(ConnectionStringCannotBeEntitySpecific);
                    MessageBox.Show(
                        this,
                        $"{ConnectionStringCannotBeEntitySpecific}\n\n{ConnectionStringCannotBeEntitySpecificDetails}",
                        "Save connection string",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Exclamation);
                    return;
                }

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

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


                if (isNewServiceBusNamespace)
                {
                    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 = ConnectionString;

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

                serviceBusHelper.ServiceBusNamespaces[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);
            }
        }