private void createIoTHubListenerMenuItem_Click(object sender, EventArgs e)
 {
     try
     {
         using (var parameterForm = new ParameterForm("Enter IoT Hub Connection String and Consumer Group", 
                                                      new List<string> {"IoT Hub Connection String", "Consumer Group"}, 
                                                      new List<string>{null, "$Default"},
                                                      new List<bool>{false, false}))
         {
             if (parameterForm.ShowDialog() != DialogResult.OK)
             {
                 return;
             }
             if (string.IsNullOrWhiteSpace(parameterForm.ParameterValues[0]))
             {
                 WriteToLog("The IoT Hub Connection string parameter cannot be null.");
                 return;
             }
             if (string.IsNullOrWhiteSpace(parameterForm.ParameterValues[1]))
             {
                 WriteToLog("The Consumer Group parameter cannot be null.");
                 return;
             }
             var form = new ContainerForm(this, parameterForm.ParameterValues[0], parameterForm.ParameterValues[1]);
             form.Show();
         }
     }
     catch (Exception ex)
     {
         HandleException(ex);
     }
 }
Beispiel #2
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                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();
                    // ReSharper disable once CoVariantArrayConversion
                    cboServiceBusNamespace.Items.AddRange(serviceBusHelper.ServiceBusNamespaces.Keys.OrderBy(s => s).ToArray());
                    cboServiceBusNamespace.Text = key;
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }