private void ValidateSection() { ContextInformation evaluationContext = ConfigurationHelpers.GetEvaluationContext(this); if (evaluationContext != null) { foreach (ChannelEndpointElement element in this.Endpoints) { if (string.IsNullOrEmpty(element.Kind)) { if (!string.IsNullOrEmpty(element.EndpointConfiguration)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigInvalidAttribute", new object[] { "endpointConfiguration", "endpoint", "kind" }))); } if (string.IsNullOrEmpty(element.Binding)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("RequiredAttributeMissing", new object[] { "binding", "endpoint" }))); } if (string.IsNullOrEmpty(element.Contract)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("RequiredAttributeMissing", new object[] { "contract", "endpoint" }))); } } if (string.IsNullOrEmpty(element.Binding) && !string.IsNullOrEmpty(element.BindingConfiguration)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigInvalidAttribute", new object[] { "bindingConfiguration", "endpoint", "binding" }))); } BehaviorsSection.ValidateEndpointBehaviorReference(element.BehaviorConfiguration, evaluationContext, element); BindingsSection.ValidateBindingReference(element.Binding, element.BindingConfiguration, evaluationContext, element); StandardEndpointsSection.ValidateEndpointReference(element.Kind, element.EndpointConfiguration, evaluationContext, element); } } }
void ValidateSection() { ContextInformation context = ConfigurationHelpers.GetEvaluationContext(this); if (context != null) { foreach (ServiceElement service in this.Services) { BehaviorsSection.ValidateServiceBehaviorReference(service.BehaviorConfiguration, context, service); foreach (ServiceEndpointElement endpoint in service.Endpoints) { if (string.IsNullOrEmpty(endpoint.Kind)) { if (!string.IsNullOrEmpty(endpoint.EndpointConfiguration)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigInvalidAttribute, "endpointConfiguration", "endpoint", "kind"))); } if (string.IsNullOrEmpty(endpoint.Binding)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.RequiredAttributeMissing, "binding", "endpoint"))); } } if (string.IsNullOrEmpty(endpoint.Binding) && !string.IsNullOrEmpty(endpoint.BindingConfiguration)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigInvalidAttribute, "bindingConfiguration", "endpoint", "binding"))); } BehaviorsSection.ValidateEndpointBehaviorReference(endpoint.BehaviorConfiguration, context, endpoint); BindingsSection.ValidateBindingReference(endpoint.Binding, endpoint.BindingConfiguration, context, endpoint); StandardEndpointsSection.ValidateEndpointReference(endpoint.Kind, endpoint.EndpointConfiguration, context, endpoint); } } } }
internal static void ValidateServiceBehaviorReference(string behaviorConfiguration, ContextInformation evaluationContext, ConfigurationElement configurationElement) { // ValidateBehaviorReference built on assumption that evaluationContext is valid. // This should be protected at the callers site. If assumption is invalid, then // configuration system is in an indeterminate state. Need to stop in a manner that // user code can not capture. if (null == evaluationContext) { Fx.Assert("ValidateBehaviorReference() should only called with valid ContextInformation"); DiagnosticUtility.FailFast("ValidateBehaviorReference() should only called with valid ContextInformation"); } #if DESKTOP if (!String.IsNullOrEmpty(behaviorConfiguration)) { BehaviorsSection behaviors = (BehaviorsSection)ConfigurationHelpers.UnsafeGetAssociatedSection(evaluationContext, ConfigurationStrings.BehaviorsSectionPath); if (!behaviors.ServiceBehaviors.ContainsKey(behaviorConfiguration)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigInvalidServiceBehavior, behaviorConfiguration), configurationElement.ElementInformation.Source, configurationElement.ElementInformation.LineNumber)); } } #endif }
public ServicesConfiguration() { System.Configuration.Configuration appConfig; if (HttpContext.Current != null) appConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~"); else appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var serviceGroup = ServiceModelSectionGroup.GetSectionGroup(appConfig); if (serviceGroup != null) { _behaviors = serviceGroup.Behaviors; } }
internal static void ValidateServiceBehaviorReference(string behaviorConfiguration, ContextInformation evaluationContext, ConfigurationElement configurationElement) { if (evaluationContext == null) { DiagnosticUtility.FailFast("ValidateBehaviorReference() should only called with valid ContextInformation"); } if (!string.IsNullOrEmpty(behaviorConfiguration)) { BehaviorsSection section = (BehaviorsSection)ConfigurationHelpers.UnsafeGetAssociatedSection(evaluationContext, ConfigurationStrings.BehaviorsSectionPath); if (!section.ServiceBehaviors.ContainsKey(behaviorConfiguration)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(System.ServiceModel.SR.GetString("ConfigInvalidServiceBehavior", new object[] { behaviorConfiguration }), configurationElement.ElementInformation.Source, configurationElement.ElementInformation.LineNumber)); } } }
/// <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"; } } } }