Exemplo n.º 1
0
        private static void ApplyServiceBehaviorConfiguration(ServiceHost serviceHost, ServiceConfiguration config)
        {
            if (!string.IsNullOrEmpty(config.ServiceBehaviorXML))
            {
                var doc = new XmlDocument();
                doc.LoadXml(config.ServiceBehaviorXML);
                var customBehaviorElements = new List <BehaviorExtensionElement>();
                foreach (XmlNode node in doc.FirstChild.ChildNodes)
                {
                    var customBehaviorTypeDesc = ServiceConfigurationStore.GetCustomBehaviorType(node.Name);
                    if (customBehaviorTypeDesc != null)
                    {
                        var customBehaviorElementType =
                            Type.GetType(customBehaviorTypeDesc.ConfigurationElementTypeClassName);
                        if (customBehaviorElementType == null)
                        {
                            throw new ConfigurationErrorsException(
                                      string.Format(
                                          "Specified service behavior configuration element type - {0} could not be loaded!",
                                          customBehaviorTypeDesc.ConfigurationElementTypeClassName));
                        }
                        var customBehaviorElement =
                            Activator.CreateInstance(customBehaviorElementType) as BehaviorExtensionElement;
                        if (customBehaviorElement == null)
                        {
                            throw new ConfigurationErrorsException(
                                      string.Format(
                                          "Specified service behavior configuration element type - {0} could not be initialized!",
                                          customBehaviorTypeDesc.ConfigurationElementTypeClassName));
                        }
                        customBehaviorElement.DeserializeElement(node.OuterXml);
                        customBehaviorElements.Add(customBehaviorElement);
                        node.ParentNode.RemoveChild(node);
                    }
                }
                var serviceBehaviorElement = new ServiceBehaviorElement();
                serviceBehaviorElement.DeserializeElement(doc.OuterXml);
                foreach (var item in serviceBehaviorElement)
                {
                    serviceHost.Description.Behaviors.Add(item.CreateServiceBehavior());
                }
                foreach (var item in customBehaviorElements)
                {
                    serviceHost.Description.Behaviors.Add(item.CreateServiceBehavior());
                }
            }

            if (!IsBehaviorConfigured <ServiceMetadataBehavior>(serviceHost))
            {
                var smb = new ServiceMetadataBehavior();
                serviceHost.Description.Behaviors.Add(smb);
            }
        }
Exemplo n.º 2
0
        internal static void ApplyEndpointBehaviorConfiguration(ServiceEndpoint endpoint, EndpointConfiguration config)
        {
            if (string.IsNullOrEmpty(config.EndpointBehaviorXML))
            {
                return;
            }

            var doc = new XmlDocument();

            doc.LoadXml(config.EndpointBehaviorXML);
            var customBehaviorElements = new List <BehaviorExtensionElement>();

            foreach (XmlNode node in doc.FirstChild.ChildNodes)
            {
                var customBehaviorTypeDesc = ServiceConfigurationStore.GetCustomBehaviorType(node.Name);
                if (customBehaviorTypeDesc != null)
                {
                    var customBehaviorElementType = Type.GetType(customBehaviorTypeDesc.ConfigurationElementTypeClassName);
                    if (customBehaviorElementType == null)
                    {
                        throw new ConfigurationErrorsException(string.Format("Specified endpoint behavior configuration element type - {0} could not be loaded!", customBehaviorTypeDesc.ConfigurationElementTypeClassName));
                    }
                    var customBehaviorElement = Activator.CreateInstance(customBehaviorElementType) as BehaviorExtensionElement;
                    if (customBehaviorElement == null)
                    {
                        throw new ConfigurationErrorsException(string.Format("Specified endpoint behavior configuration element type - {0} could not be initialized!", customBehaviorTypeDesc.ConfigurationElementTypeClassName));
                    }
                    customBehaviorElement.DeserializeElement(node.OuterXml);
                    customBehaviorElements.Add(customBehaviorElement);
                    node.ParentNode.RemoveChild(node);
                }
            }
            var endpointBehaviorElement = new EndpointBehaviorElement();

            endpointBehaviorElement.DeserializeElement(doc.OuterXml);
            foreach (var item in endpointBehaviorElement)
            {
                endpoint.Behaviors.Add(item.CreateEndpointBehavior());
            }
            foreach (var item in customBehaviorElements)
            {
                endpoint.Behaviors.Add(item.CreateEndpointBehavior());
            }
        }