public AVD2ConfigFile(XmlDocument xmlDoc) { var avDump2Node = xmlDoc.FirstChild; if (avDump2Node.Name != "AVDump2") { throw new Exception("Invalid root node name"); } foreach (XmlNode node in avDump2Node.ChildNodes) { switch (node.Name) { case "Information": Information = new InformationSection(node); break; case "Extensions": Extensions = new ExtensionsSection(node); break; case "SettingsContainer": SettingsContainer = new SettingsContainerSection(node); break; default: break; } } }
/// <summary> /// This method adds the neccessary configuration elements to hook up /// Thinktecture.ServiceModel.Extensions.Metdata extension to service the /// service code being generated. /// </summary> private void AddMetadataServiceBehavior() { // Get a pointer to system.serviceModel section. ConfigurationSectionGroup csg = configuration.SectionGroups["system.serviceModel"]; // Notify if we get a null reference. Debug.Assert(csg != null, "system.serviceModel section could not be found in the configuration."); if (csg != null) { // Try to find the extensions element. ExtensionsSection extensionsSection = csg.Sections["extensions"] as ExtensionsSection; // Create it if it wasn't found. if (extensionsSection == null) { extensionsSection = new ExtensionsSection(); csg.Sections.Add("extensions", extensionsSection); } // Now create the new behavior extension. ExtensionElement metadataServiceExtensionElement = new ExtensionElement(); metadataServiceExtensionElement.Name = "metadataService"; //TODO: Make this more dynamic so it can discover the assembly version etc otherwise this will always throw exceptions // that the behavior extension was not found in the collection. metadataServiceExtensionElement.Type = "Thinktecture.ServiceModel.Extensions.Metadata.StaticMetadataBehaviorElement, Thinktecture.ServiceModel.Extensions.Metadata, Version=1.1.0.0, Culture=neutral, PublicKeyToken=20fb7cabbfb92df4"; // Add the newly created behavior extension to the extensions section. extensionsSection.BehaviorExtensions.Add(metadataServiceExtensionElement); // Try to find the behaviors element. BehaviorsSection behaviorsSection = csg.Sections["behaviors"] as BehaviorsSection; // Create it if it wasn't found. if (behaviorsSection == null) { behaviorsSection = new BehaviorsSection(); csg.Sections.Add("behaviors", behaviorsSection); } // Add the new service behavior. ServiceBehaviorElement serviceBehavior = new ServiceBehaviorElement(); serviceBehavior.Name = "metadataServiceExtension"; behaviorsSection.ServiceBehaviors.Add(serviceBehavior); StaticMetadataBehaviorElement behaviorExtensionElement = new StaticMetadataBehaviorElement(); behaviorExtensionElement.RootMetadataFileLocation = options.MetadataLocation; behaviorExtensionElement.MetadataUrl = "wsdl"; serviceBehavior.Add(behaviorExtensionElement); // Find the service section. ServicesSection servicesSection = csg.Sections["services"] as ServicesSection; if (servicesSection != null) { string fqServiceTypeName = GetFullyQulifiedTypeName(GetServiceTypeName()); ServiceElement serviceElement = servicesSection.Services[fqServiceTypeName] as ServiceElement; if (serviceElement != null) { serviceElement.BehaviorConfiguration = "metadataServiceExtension"; } } } }
private void Run() { //<snippet1> Configuration machine = ConfigurationManager.OpenMachineConfiguration(); //<snippet5> // Register our validator configuration element. ExtensionsSection extensions = machine.GetSection(@"system.serviceModel/extensions") as ExtensionsSection; if (extensions == null) { throw new Exception("not extensions section."); } ExtensionElement validator = new ExtensionElement( "internetClientValidator", "Microsoft.ServiceModel.Samples.InternetClientValidatorElement, InternetClientValidator, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" ); validator.LockItem = true; if (extensions.BehaviorExtensions.IndexOf(validator) < 0) { extensions.BehaviorExtensions.Add(validator); } //</snippet5> //<snippet6> // Add a new section for our validator and lock it down. // Behaviors for client applications must be endpoint behaviors. // Behaviors for service applications must be service behaviors. CommonBehaviorsSection commonBehaviors = machine.GetSection(@"system.serviceModel/commonBehaviors") as CommonBehaviorsSection; InternetClientValidatorElement internetValidator = new InternetClientValidatorElement(); internetValidator.LockItem = true; commonBehaviors.EndpointBehaviors.Add(internetValidator); //</snippet6> //<snippet7> // Write to disk. machine.SaveAs("newMachine.config"); // Write our new information. SectionInformation cBInfo = commonBehaviors.SectionInformation; Console.WriteLine(cBInfo.GetRawXml()); Console.WriteLine(extensions.SectionInformation.GetRawXml()); Console.Read(); //</snippet7> //</snippet1> /* * //<snippet4> * <extensions> * <behaviorExtensions> * <add * name="internetClientValidator" * type="Microsoft.ServiceModel.Samples.InternetClientValidatorElement, InternetClientValidator, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" * lockItem="true" /> * </behaviorExtensions> * </extensions> * <commonBehaviors> * <endpointBehaviors> * <internetClientValidator lockItem="true" /> * </endpointBehaviors> * </commonBehaviors> * //</snippet4> * */ }