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);
            }
        }