예제 #1
0
        /// <summary>
        /// Unconfigures the feature
        /// </summary>
        /// <param name="configurationDom"></param>
        public void UnConfigure(System.Xml.XmlDocument configurationDom)
        {
            // This is a complex configuration so here we go.
            XmlElement configSectionNode = configurationDom.SelectSingleNode("//*[local-name() = 'configSections']/*[local-name() = 'section'][@name = 'marc.hi.ehrs.cr.messaging.admin']") as XmlElement,
                       configRoot        = configurationDom.SelectSingleNode("//*[local-name() = 'marc.hi.ehrs.cr.messaging.admin']") as XmlElement,
                       wcfRoot           = configurationDom.SelectSingleNode("//*[local-name() = 'system.serviceModel']") as XmlElement,
                       multiNode         = configurationDom.SelectSingleNode(String.Format("//*[local-name() = 'marc.hi.ehrs.svc.messaging.multi']//*[local-name() = 'add'][@type = '{0}']", typeof(ClientRegistryAdminInterface).AssemblyQualifiedName)) as XmlElement;

            // Remove the sections
            if (configSectionNode != null)
            {
                configSectionNode.ParentNode.RemoveChild(configSectionNode);
            }
            if (configRoot != null)
            {
                configRoot.ParentNode.RemoveChild(configRoot);
            }
            if (multiNode != null)
            {
                multiNode.ParentNode.RemoveChild(multiNode);
            }
            X509Certificate2 serviceCert         = null;
            StoreLocation    certificateLocation = StoreLocation.LocalMachine;
            StoreName        certificateStore    = StoreName.My;
            // Lookup the service information
            XmlElement serviceNode = wcfRoot.SelectSingleNode(String.Format(".//*[local-name() = 'service'][@name = '{0}']", this.m_configuration.WcfServiceName)) as XmlElement;

            if (serviceNode == null)
            {
                return;
            }
            if (serviceNode.Attributes["behaviorConfiguration"] != null)
            {
                XmlElement behavior = wcfRoot.SelectSingleNode(String.Format(".//*[local-name() = 'behavior'][@name = '{0}']", serviceNode.Attributes["behaviorConfiguration"].Value)) as XmlElement;
                if (behavior != null)
                {
                    XmlElement serviceCertificateNode = behavior.SelectSingleNode(".//*[local-name() = 'serviceCertificate']") as XmlElement;
                    if (serviceCertificateNode != null)
                    {
                        certificateStore    = (StoreName)Enum.Parse(typeof(StoreName), serviceCertificateNode.Attributes["storeName"].Value);
                        certificateLocation = (StoreLocation)Enum.Parse(typeof(StoreLocation), serviceCertificateNode.Attributes["storeLocation"].Value);
                        X509Store store = new X509Store(
                            certificateStore,
                            certificateLocation
                            );
                        try
                        {
                            store.Open(OpenFlags.ReadOnly);
                            var cert = store.Certificates.Find((X509FindType)Enum.Parse(typeof(X509FindType), serviceCertificateNode.Attributes["x509FindType"].Value), serviceCertificateNode.Attributes["findValue"].Value, false);
                            if (cert.Count > 0)
                            {
                                serviceCert = cert[0];
                            }
                        }
                        catch (System.Exception e)
                        {
                            MessageBox.Show("Cannot retrieve certification information");
                        }
                        finally
                        {
                            store.Close();
                        }
                    }

                    behavior.ParentNode.RemoveChild(behavior);
                }
            }

            // Remove the bindings
            XmlNodeList endpoints = serviceNode.SelectNodes(".//*[local-name() = 'endpoint']");

            foreach (XmlElement ep in endpoints)
            {
                if (ep.Attributes["bindingConfiguration"] != null)
                {
                    var binding = wcfRoot.SelectSingleNode(String.Format(".//*[local-name() = 'binding'][@name = '{0}']", ep.Attributes["bindingConfiguration"].Value)) as XmlElement;

                    if (binding != null)
                    {
                        binding.ParentNode.RemoveChild(binding);
                    }
                }

                // Un-bind the certificate
                if (serviceCert != null)
                {
                    Uri address = new Uri(ep.Attributes["address"].Value);
                    // Reserve the SSL certificate on the IP address
                    if (address.HostNameType == UriHostNameType.Dns)
                    {
                        var ipAddresses = Dns.GetHostAddresses(address.Host);
                        HttpSslTool.RemoveCertificate(ipAddresses[0], address.Port, serviceCert.GetCertHash(), certificateStore, certificateLocation);
                    }
                    else
                    {
                        HttpSslTool.RemoveCertificate(IPAddress.Parse(address.Host), address.Port, serviceCert.GetCertHash(), certificateStore, certificateLocation);
                    }
                }
            }
            serviceNode.ParentNode.RemoveChild(serviceNode);



            this.m_needsSync = true;
        }
예제 #2
0
        /// <summary>
        /// Configure the administrative interface
        /// </summary>
        /// <param name="configurationDom"></param>
        public void Configure(System.Xml.XmlDocument configurationDom)
        {
            XmlElement configSectionsNode = configurationDom.SelectSingleNode("//*[local-name() = 'configSections']") as XmlElement,
                       adminNode          = configurationDom.SelectSingleNode("//*[local-name() = 'marc.hi.ehrs.cr.messaging.admin']") as XmlElement,
                       multiNode          = configurationDom.SelectSingleNode("//*[local-name() = 'marc.hi.ehrs.svc.messaging.multi']") as XmlElement,
                       coreNode           = configurationDom.SelectSingleNode("//*[local-name() = 'marc.hi.ehrs.svc.core']") as XmlElement,
                       wcfNode            = configurationDom.SelectSingleNode("//*[local-name() = 'system.serviceModel']") as XmlElement;

            Type mmhType       = Type.GetType("MARC.HI.EHRS.SVC.Messaging.Multi.MultiMessageHandler, MARC.HI.EHRS.SVC.Messaging.Multi"),
                 mmhConfigType = Type.GetType("MARC.HI.EHRS.SVC.Messaging.Multi.Configuration.ConfigurationSectionHandler, MARC.HI.EHRS.SVC.Messaging.Multi");

            // Ensure the assembly is loaded and the provider registered
            if (coreNode == null)
            {
                coreNode = configurationDom.CreateElement("marc.hi.ehrs.svc.core");
                configurationDom.DocumentElement.AppendChild(coreNode);
            }
            // Config sections node
            if (configSectionsNode == null)
            {
                configSectionsNode = configurationDom.CreateElement("configSections");
                configurationDom.DocumentElement.PrependChild(configSectionsNode);
            }
            XmlElement configSectionNode = configSectionsNode.SelectSingleNode("./*[local-name() = 'section'][@name = 'marc.hi.ehrs.cr.messaging.admin']") as XmlElement;

            if (configSectionNode == null)
            {
                configSectionNode = configurationDom.CreateElement("section");
                configSectionNode.Attributes.Append(configurationDom.CreateAttribute("name"));
                configSectionNode.Attributes.Append(configurationDom.CreateAttribute("type"));
                configSectionNode.Attributes["name"].Value = "marc.hi.ehrs.cr.messaging.admin";
                configSectionNode.Attributes["type"].Value = typeof(ConfigurationSectionHandler).AssemblyQualifiedName;
                configSectionsNode.AppendChild(configSectionNode);
            }

            configSectionNode = configSectionsNode.SelectSingleNode("./*[local-name() = 'section'][@name = 'marc.hi.ehrs.svc.messaging.multi']") as XmlElement;
            if (configSectionNode == null && mmhConfigType != null)
            {
                configSectionNode = configurationDom.CreateElement("section");
                configSectionNode.Attributes.Append(configurationDom.CreateAttribute("name"));
                configSectionNode.Attributes.Append(configurationDom.CreateAttribute("type"));
                configSectionNode.Attributes["name"].Value = "marc.hi.ehrs.svc.messaging.multi";
                configSectionNode.Attributes["type"].Value = mmhConfigType.AssemblyQualifiedName;
                configSectionsNode.AppendChild(configSectionNode);
            }

            // Persistence section node
            if (adminNode == null)
            {
                adminNode = configurationDom.CreateElement("marc.hi.ehrs.cr.messaging.admin");
                configurationDom.DocumentElement.AppendChild(adminNode);
            }

            // Add wcf listen endpoint
            XmlElement listenNode = adminNode.SelectSingleNode("./*[local-name() = 'listen']") as XmlElement;

            if (listenNode == null)
            {
                listenNode = adminNode.AppendChild(configurationDom.CreateElement("listen")) as XmlElement;
            }
            if (listenNode.Attributes["wcfServiceName"] == null)
            {
                listenNode.Attributes.Append(configurationDom.CreateAttribute("wcfServiceName"));
            }
            listenNode.Attributes["wcfServiceName"].Value = this.m_configuration.WcfServiceName;

            XmlElement serviceAssemblyNode = coreNode.SelectSingleNode("./*[local-name() = 'serviceAssemblies']") as XmlElement,
                       serviceProviderNode = coreNode.SelectSingleNode("./*[local-name() = 'serviceProviders']") as XmlElement;

            if (serviceAssemblyNode == null)
            {
                serviceAssemblyNode = configurationDom.CreateElement("serviceAssemblies");
                coreNode.AppendChild(serviceAssemblyNode);
            }
            if (serviceProviderNode == null)
            {
                serviceProviderNode = configurationDom.CreateElement("serviceProviders");
                coreNode.AppendChild(serviceProviderNode);
            }

            // Add service provider (Multi if available, Everest if otherwise)
            XmlElement addServiceAsmNode  = serviceAssemblyNode.SelectSingleNode("./*[local-name() = 'add'][@assembly = 'MARC.HI.EHRS.CR.Messaging.Admin.dll']") as XmlElement,
                       addServiceProvNode = serviceProviderNode.SelectSingleNode(String.Format("./*[local-name() = 'add'][@type = '{0}']", (mmhType ?? typeof(ClientRegistryAdminInterface)).AssemblyQualifiedName)) as XmlElement;

            if (addServiceAsmNode == null)
            {
                addServiceAsmNode = configurationDom.CreateElement("add");
                addServiceAsmNode.Attributes.Append(configurationDom.CreateAttribute("assembly"));
                addServiceAsmNode.Attributes["assembly"].Value = "MARC.HI.EHRS.CR.Messaging.Admin.dll";
                serviceAssemblyNode.AppendChild(addServiceAsmNode);
            }
            if (addServiceProvNode == null)
            {
                addServiceProvNode = configurationDom.CreateElement("add");
                addServiceProvNode.Attributes.Append(configurationDom.CreateAttribute("type"));
                addServiceProvNode.Attributes["type"].Value = (mmhType ?? typeof(ClientRegistryAdminInterface)).AssemblyQualifiedName;
                serviceProviderNode.AppendChild(addServiceProvNode);
            }

            // Multi-message handler registration?
            if (mmhType != null)
            {
                XmlElement mmhNode = configurationDom.SelectSingleNode(".//*[local-name() = 'marc.hi.ehrs.svc.messaging.multi']") as XmlElement;
                if (mmhNode == null)
                {
                    mmhNode = configurationDom.DocumentElement.AppendChild(configurationDom.CreateElement("marc.hi.ehrs.svc.messaging.multi")) as XmlElement;
                }
                // Handler node
                XmlElement handlerNode = mmhNode.SelectSingleNode("./*[local-name() = 'handlers']") as XmlElement;
                if (handlerNode == null)
                {
                    handlerNode = mmhNode.AppendChild(configurationDom.CreateElement("handlers")) as XmlElement;
                }
                // Add node?
                if (handlerNode.SelectSingleNode(String.Format("./*[local-name() = 'add'][@type = '{0}']", typeof(ClientRegistryAdminInterface).AssemblyQualifiedName)) == null)
                {
                    var addNode = handlerNode.AppendChild(configurationDom.CreateElement("add"));
                    addNode.Attributes.Append(configurationDom.CreateAttribute("type")).Value = typeof(ClientRegistryAdminInterface).AssemblyQualifiedName;
                }
            }

            // WCF nodes
            if (wcfNode == null)
            {
                wcfNode = configurationDom.DocumentElement.AppendChild(configurationDom.CreateElement("system.serviceModel")) as XmlElement;
            }

            // Loop through enabled revisions and see if we need to configure them
            WcfServiceConfiguration(this.m_configuration.WcfServiceName, wcfNode);

            // Configure the URL / SSL
            try
            {
                if (this.m_panel.Address.StartsWith("https:"))
                {
                    Uri address = new Uri(this.m_panel.Address);
                    // Reserve the SSL certificate on the IP address
                    if (address.HostNameType == UriHostNameType.Dns)
                    {
                        var ipAddresses = Dns.GetHostAddresses(address.Host);
                        HttpSslTool.BindCertificate(ipAddresses[0], address.Port, this.m_panel.Certificate.GetCertHash(), this.m_panel.StoreName, this.m_panel.StoreLocation);
                    }
                    else
                    {
                        HttpSslTool.BindCertificate(IPAddress.Parse(address.Host), address.Port, this.m_panel.Certificate.GetCertHash(), this.m_panel.StoreName, this.m_panel.StoreLocation);
                    }
                }
            }
            catch (Win32Exception e)
            {
                throw new OperationCanceledException(String.Format("Error binding SSL certificate to address. Error was: {0:x} {1}", e.ErrorCode, e.Message), e);
            }


            this.m_needsSync = true;
        }