private string GetEndpointName() { string name = string.Empty; ExtensionElementCollection elements = null; Type type = base.GetType(); elements = ExtensionsSection.UnsafeLookupCollection("endpointExtensions", ConfigurationHelpers.GetEvaluationContext(this)); if (elements == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigExtensionCollectionNotFound", new object[] { "endpointExtensions" }), base.ElementInformation.Source, base.ElementInformation.LineNumber)); } for (int i = 0; i < elements.Count; i++) { ExtensionElement element = elements[i]; if (element.Type.Equals(type.AssemblyQualifiedName, StringComparison.Ordinal)) { name = element.Name; break; } Type o = Type.GetType(element.Type, false); if ((null != o) && type.Equals(o)) { name = element.Name; break; } } if (string.IsNullOrEmpty(name)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigExtensionTypeNotRegisteredInCollection", new object[] { type.AssemblyQualifiedName, "endpointExtensions" }), base.ElementInformation.Source, base.ElementInformation.LineNumber)); } return(name); }
internal bool CanAdd(string extensionCollectionName, ContextInformation evaluationContext) { bool retVal = false; ExtensionElementCollection collection = ExtensionsSection.UnsafeLookupCollection(extensionCollectionName, evaluationContext); if (null != collection && collection.Count != 0) { string thisAssemblyQualifiedName = ThisType.AssemblyQualifiedName; string thisTypeName = ExtensionElement.GetTypeName(thisAssemblyQualifiedName); foreach (ExtensionElement extensionElement in collection) { string extensionTypeName = extensionElement.Type; if (extensionTypeName.Equals(thisAssemblyQualifiedName, StringComparison.Ordinal)) { retVal = true; break; } if (extensionElement.TypeName.Equals(thisTypeName, StringComparison.Ordinal)) { Type extensionType = Type.GetType(extensionTypeName, false); if (extensionType != null && extensionType.Equals(ThisType)) { retVal = true; break; } } } if (!retVal && DiagnosticUtility.ShouldTraceWarning) { TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.ConfiguredExtensionTypeNotFound, SR.GetString(SR.TraceCodeConfiguredExtensionTypeNotFound), this.CreateCanAddRecord(extensionCollectionName), this, null); } } else if (DiagnosticUtility.ShouldTraceWarning) { int traceCode; string traceDescription; if (collection != null && collection.Count == 0) { traceCode = TraceCode.ExtensionCollectionIsEmpty; traceDescription = SR.GetString(SR.TraceCodeExtensionCollectionIsEmpty); } else { traceCode = TraceCode.ExtensionCollectionDoesNotExist; traceDescription = SR.GetString(SR.TraceCodeExtensionCollectionDoesNotExist); } TraceUtility.TraceEvent(TraceEventType.Warning, traceCode, traceDescription, this.CreateCanAddRecord(extensionCollectionName), this, null); } return(retVal); }
internal static void TraceExtensionTypeNotFound(ExtensionElement extensionElement) { if (DiagnosticUtility.ShouldTraceWarning) { Dictionary <string, string> dictionary = new Dictionary <string, string>(2); dictionary.Add("ExtensionName", extensionElement.Name); dictionary.Add("ExtensionType", extensionElement.Type); DictionaryTraceRecord extendedData = new DictionaryTraceRecord(dictionary); TraceUtility.TraceEvent(TraceEventType.Warning, 0x80045, System.ServiceModel.SR.GetString("TraceCodeExtensionTypeNotFound"), extendedData, null, null); } }
private string GetConfigurationElementName() { string name = string.Empty; ExtensionElementCollection elements = null; Type thisType = this.ThisType; ContextInformation containingEvaluationContext = this.ContainingEvaluationContext; if (containingEvaluationContext == null) { containingEvaluationContext = ConfigurationHelpers.GetEvaluationContext(this); } if (string.IsNullOrEmpty(this.extensionCollectionName)) { if (DiagnosticUtility.ShouldTraceWarning) { TraceUtility.TraceEvent(TraceEventType.Warning, 0x8001b, System.ServiceModel.SR.GetString("TraceCodeExtensionCollectionNameNotFound"), this, (Exception)null); } elements = ExtensionsSection.UnsafeLookupAssociatedCollection(this.ThisType, containingEvaluationContext, out this.extensionCollectionName); } else { elements = ExtensionsSection.UnsafeLookupCollection(this.extensionCollectionName, containingEvaluationContext); } if (elements == null) { if (string.IsNullOrEmpty(this.extensionCollectionName)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigNoExtensionCollectionAssociatedWithType", new object[] { thisType.AssemblyQualifiedName }), base.ElementInformation.Source, base.ElementInformation.LineNumber)); } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigExtensionCollectionNotFound", new object[] { this.extensionCollectionName }), base.ElementInformation.Source, base.ElementInformation.LineNumber)); } for (int i = 0; i < elements.Count; i++) { ExtensionElement element = elements[i]; if (element.Type.Equals(thisType.AssemblyQualifiedName, StringComparison.Ordinal)) { name = element.Name; break; } Type type = Type.GetType(element.Type, false); if ((null != type) && thisType.Equals(type)) { name = element.Name; break; } } if (string.IsNullOrEmpty(name)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigExtensionTypeNotRegisteredInCollection", new object[] { thisType.AssemblyQualifiedName, this.extensionCollectionName }), base.ElementInformation.Source, base.ElementInformation.LineNumber)); } return(name); }
public void AddBindingElementExtension() { string filepath = DocLoader.CreateTempMachineConfig(); WcfMachineConfiguration config = new WcfMachineConfiguration(filepath); string name = new Random().Next().ToString(); ExtensionElement extension1; extension1 = new ExtensionElement(name, this.GetType().AssemblyQualifiedName); Assert.IsFalse(config.TryGetBindingElementExtension(name, out extension1)); extension1 = new ExtensionElement(name, this.GetType().AssemblyQualifiedName); config.UpdateBindingElementExtension(extension1); Assert.IsTrue(config.TryGetBindingElementExtension(name, out extension1)); }
string GetEndpointName() { string configuredSectionName = String.Empty; ExtensionElementCollection collection = null; Type extensionSectionType = this.GetType(); collection = ExtensionsSection.UnsafeLookupCollection(ConfigurationStrings.EndpointExtensions, ConfigurationHelpers.GetEvaluationContext(this)); if (null == collection) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigExtensionCollectionNotFound, ConfigurationStrings.EndpointExtensions), this.ElementInformation.Source, this.ElementInformation.LineNumber)); } for (int i = 0; i < collection.Count; i++) { ExtensionElement collectionElement = collection[i]; // Optimize for assembly qualified names. if (collectionElement.Type.Equals(extensionSectionType.AssemblyQualifiedName, StringComparison.Ordinal)) { configuredSectionName = collectionElement.Name; break; } // Check type directly for the case that the extension is registered with something less than // an full assembly qualified name. Type collectionElementType = Type.GetType(collectionElement.Type, false); if (null != collectionElementType && extensionSectionType.Equals(collectionElementType)) { configuredSectionName = collectionElement.Name; break; } } if (String.IsNullOrEmpty(configuredSectionName)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigExtensionTypeNotRegisteredInCollection, extensionSectionType.AssemblyQualifiedName, ConfigurationStrings.EndpointExtensions), this.ElementInformation.Source, this.ElementInformation.LineNumber)); } return(configuredSectionName); }
internal static void TraceExtensionTypeNotFound(ExtensionElement extensionElement) { if (DiagnosticUtility.ShouldTraceWarning) { Dictionary <string, string> values = new Dictionary <string, string>(2); values.Add("ExtensionName", extensionElement.Name); values.Add("ExtensionType", extensionElement.Type); DictionaryTraceRecord traceRecord = new DictionaryTraceRecord(values); TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.ExtensionTypeNotFound, SR.GetString(SR.TraceCodeExtensionTypeNotFound), traceRecord, null, (Exception)null); } }
private Type GetExtensionType(ContextInformation evaluationContext, string name) { ExtensionElementCollection elements = ExtensionsSection.UnsafeLookupCollection(this.extensionCollectionName, evaluationContext); if (!elements.ContainsKey(name)) { return(null); } ExtensionElement element = elements[name]; Type type = Type.GetType(element.Type, false); if (null == type) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigInvalidType", new object[] { element.Type, element.Name }), base.ElementInformation.Source, base.ElementInformation.LineNumber)); } return(type); }
internal override BindingElementExtensionElement DeserializeExtensionElement(string elementName, XmlReader reader) { //ExtensionElementCollection extensions = ((ExtensionsSection) EvaluationContext.GetSection ("system.serviceModel/extensions")).BindingElementExtensions; ExtensionElementCollection extensions = ConfigUtil.ExtensionsSection.BindingElementExtensions; ExtensionElement extension = extensions [elementName]; if (extension == null) { throw new ConfigurationErrorsException("Invalid element in configuration. The extension name '" + reader.LocalName + "' is not registered in the collection at system.serviceModel/extensions/bindingElementExtensions"); } BindingElementExtensionElement element = (BindingElementExtensionElement)Activator.CreateInstance(Type.GetType(extension.Type)); element.DeserializeElementInternal(reader, false); return(element); }
Type GetExtensionType(ContextInformation evaluationContext, string name) { ExtensionElementCollection collection = ExtensionsSection.UnsafeLookupCollection(this.extensionCollectionName, evaluationContext); if (collection.ContainsKey(name)) { ExtensionElement element = collection[name]; Type elementType = Type.GetType(element.Type, false); if (null == elementType) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigInvalidType, element.Type, element.Name), this.ElementInformation.Source, this.ElementInformation.LineNumber)); } return(elementType); } return(null); }
/// <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.0.14.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"; } } } }
internal static void TraceExtensionTypeNotFound(ExtensionElement extensionElement) { if (DiagnosticUtility.ShouldTraceWarning) { Dictionary<string, string> values = new Dictionary<string, string>(2); values.Add("ExtensionName", extensionElement.Name); values.Add("ExtensionType", extensionElement.Type); DictionaryTraceRecord traceRecord = new DictionaryTraceRecord(values); TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.ExtensionTypeNotFound, SR.GetString(SR.TraceCodeExtensionTypeNotFound), traceRecord, null, (Exception)null); } }
public bool TryGetBindingElementExtension(string name, out ExtensionElement bindingElement) { Trace.WriteLineIf(Tracing.LBSwitch.TraceVerbose, "WcfMachineConfiguration:TryGetBindingElementExtension for " + name); ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(_config); ExtensionElementCollection elementExtensions = serviceModel.Extensions.BindingElementExtensions; lock (_syncLock) { if (elementExtensions.ContainsKey(name)) { bindingElement = elementExtensions[name]; return true; } } bindingElement = null; return false; }
public void UpdateBindingExtension(ExtensionElement extensionElement) { Trace.WriteLineIf(Tracing.LBSwitch.TraceVerbose, "WcfMachineConfiguration:UpdateBindingExtension for " + extensionElement.Name); ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(_config); ExtensionElementCollection bindingExtensions = serviceModel.Extensions.BindingExtensions; lock (_syncLock) { if (bindingExtensions.ContainsKey(extensionElement.Name)) { bindingExtensions.RemoveAt(extensionElement.Name); } bindingExtensions.Add(extensionElement); _config.Save(); } }
//private class BindingElementExtension //{ // public BindingElementExtension(WcfMachineConfiguration config, string name) // { // ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(config._config); // ExtensionElementCollection bindingElementExtensions = serviceModel.Extensions.BindingElementExtensions; // if (bindingElementExtensions.ContainsKey(name)==false) // { // } // } //} /// <summary> /// When not present the extensionElement will be added to the configuration. /// When present it will be replaced. /// </summary> /// <param name="extensionElement"></param> public void UpdateBindingElementExtension(ExtensionElement extensionElement) { ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(_config); ExtensionElementCollection bindingElementExtensions = serviceModel.Extensions.BindingElementExtensions; lock (_syncLock) { if (bindingElementExtensions.ContainsKey(extensionElement.Name)) { bindingElementExtensions.RemoveAt(extensionElement.Name); } bindingElementExtensions.Add(extensionElement); _config.Save(); } }
string GetConfigurationElementName() { string configurationElementName = String.Empty; ExtensionElementCollection collection = null; Type extensionSectionType = ThisType; ContextInformation evaluationContext = this.ContainingEvaluationContext; if (evaluationContext == null) { evaluationContext = ConfigurationHelpers.GetEvaluationContext(this); } if (String.IsNullOrEmpty(this.extensionCollectionName)) { #if DESKTOP if (DiagnosticUtility.ShouldTraceWarning) { TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.ExtensionCollectionNameNotFound, SR.GetString(SR.TraceCodeExtensionCollectionNameNotFound), this, (Exception)null); } #endif collection = ExtensionsSection.UnsafeLookupAssociatedCollection(ThisType, evaluationContext, out this.extensionCollectionName); } else { collection = ExtensionsSection.UnsafeLookupCollection(this.extensionCollectionName, evaluationContext); } if (null == collection) { if (String.IsNullOrEmpty(this.extensionCollectionName)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigNoExtensionCollectionAssociatedWithType, extensionSectionType.AssemblyQualifiedName), this.ElementInformation.Source, this.ElementInformation.LineNumber)); } else { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigExtensionCollectionNotFound, this.extensionCollectionName), this.ElementInformation.Source, this.ElementInformation.LineNumber)); } } for (int i = 0; i < collection.Count; i++) { ExtensionElement collectionElement = collection[i]; // Optimize for assembly qualified names. if (collectionElement.Type.Equals(extensionSectionType.AssemblyQualifiedName, StringComparison.Ordinal)) { configurationElementName = collectionElement.Name; break; } // Check type directly for the case that the extension is registered with something less than // an full assembly qualified name. Type collectionElementType = Type.GetType(collectionElement.Type, false); if (null != collectionElementType && extensionSectionType.Equals(collectionElementType)) { configurationElementName = collectionElement.Name; break; } } if (String.IsNullOrEmpty(configurationElementName)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigExtensionTypeNotRegisteredInCollection, extensionSectionType.AssemblyQualifiedName, this.extensionCollectionName), this.ElementInformation.Source, this.ElementInformation.LineNumber)); } return(configurationElementName); }
internal static void TraceExtensionTypeNotFound(ExtensionElement extensionElement) { if (DiagnosticUtility.ShouldTraceWarning) { Dictionary<string, string> dictionary = new Dictionary<string, string>(2); dictionary.Add("ExtensionName", extensionElement.Name); dictionary.Add("ExtensionType", extensionElement.Type); DictionaryTraceRecord extendedData = new DictionaryTraceRecord(dictionary); TraceUtility.TraceEvent(TraceEventType.Warning, 0x80045, System.ServiceModel.SR.GetString("TraceCodeExtensionTypeNotFound"), extendedData, null, null); } }