/// ----------------------------------------------------------------------------------------------------------- /// Module : SampleAdapterBindingElementExtensionElement.cs /// Description : This class is provided to surface Adapter as a binding element, so that it /// can be used within a user-defined WCF "Custom Binding". /// In configuration file, it is defined under /// /// /// /// /// /// /// /// ----------------------------------------------------------------------------------------------------------- #region Using Directives using System; using System.Collections.Generic; using System.Text; using System.ServiceModel; using System.ServiceModel.Configuration; using System.ServiceModel.Channels; using System.Configuration; using System.Globalization; using Microsoft.ServiceModel.Channels.Common; #endregion namespace My.Samples.CodeSnippets { using System; using System.Configuration; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Configuration; public class SampleAdapterBindingElementExtensionElement : BindingElementExtensionElement { #region Constructor /// /// Default constructor /// public SampleAdapterBindingElementExtensionElement() { } #endregion Constructor #region Custom Generated Properties [System.Configuration.ConfigurationProperty("overrideWsdlBuilder", DefaultValue = false)] public bool OverrideWsdlBuilder { get { return ((bool)(base["overrideWsdlBuilder"])); } set { base["overrideWsdlBuilder"] = value; } } [System.Configuration.ConfigurationProperty("usePredefinedSchemas", DefaultValue = false)] public bool UsePredefinedSchemas { get { return ((bool)(base["usePredefinedSchemas"])); } set { base["usePredefinedSchemas"] = value; } } [System.Configuration.ConfigurationProperty("count", DefaultValue = 4)] public int Count { get { return ((int)(base["count"])); } set { base["count"] = value; } } [System.Configuration.ConfigurationProperty("enablePerfCounters", DefaultValue = false)] public bool EnablePerfCounters { get { return ((bool)(base["enablePerfCounters"])); } set { base["enablePerfCounters"] = value; } } [System.Configuration.ConfigurationProperty("enableConnectionPooling", DefaultValue = true)] public bool EnableConnectionPooling { get { return ((bool)(base["enableConnectionPooling"])); } set { base["enableConnectionPooling"] = value; } } #endregion Custom Generated Properties #region BindingElementExtensionElement Methods /// /// Return the type of the adapter (binding element) /// public override Type BindingElementType { get { return typeof(SampleAdapter); } } /// /// Returns a collection of the configuration properties /// protected override ConfigurationPropertyCollection Properties { get { ConfigurationPropertyCollection configProperties = base.Properties; configProperties.Add(new ConfigurationProperty("overrideWsdlBuilder", typeof(System.Boolean), false, null, null, ConfigurationPropertyOptions.None)); configProperties.Add(new ConfigurationProperty("usePredefinedSchemas", typeof(System.Boolean), false, null, null, ConfigurationPropertyOptions.None)); configProperties.Add(new ConfigurationProperty("count", typeof(System.Int32), 4, null, null, ConfigurationPropertyOptions.None)); configProperties.Add(new ConfigurationProperty("enablePerfCounters", typeof(System.Boolean), false, null, null, ConfigurationPropertyOptions.None)); configProperties.Add(new ConfigurationProperty("enableConnectionPooling", typeof(System.Boolean), true, null, null, ConfigurationPropertyOptions.None)); return configProperties; } } /// /// Instantiate the adapter. /// /// protected override BindingElement CreateBindingElement() { SampleAdapter adapter = new SampleAdapter(); this.ApplyConfiguration(adapter); return adapter; } /// /// Apply the configuration properties to the adapter. /// /// public override void ApplyConfiguration(BindingElement bindingElement) { base.ApplyConfiguration(bindingElement); SampleAdapter adapterBinding = ((SampleAdapter)(bindingElement)); adapterBinding.OverrideWsdlBuilder = (System.Boolean)this["overrideWsdlBuilder"]; adapterBinding.UsePredefinedSchemas = (System.Boolean)this["usePredefinedSchemas"]; adapterBinding.Count = (System.Int32)this["count"]; adapterBinding.EnablePerfCounters = (System.Boolean)this["enablePerfCounters"]; adapterBinding.EnableConnectionPooling = (System.Boolean)this["enableConnectionPooling"]; } /// /// Initialize the binding properties from the adapter. /// /// protected override void InitializeFrom(BindingElement bindingElement) { base.InitializeFrom(bindingElement); SampleAdapter adapterBinding = ((SampleAdapter)(bindingElement)); this["overrideWsdlBuilder"] = adapterBinding.OverrideWsdlBuilder; this["usePredefinedSchemas"] = adapterBinding.UsePredefinedSchemas; this["count"] = adapterBinding.Count; this["enablePerfCounters"] = adapterBinding.EnablePerfCounters; this["enableConnectionPooling"] = adapterBinding.EnableConnectionPooling; } /// /// Copy the properties to the custom binding /// /// public override void CopyFrom(ServiceModelExtensionElement from) { base.CopyFrom(from); SampleAdapterBindingElementExtensionElement adapterBinding = ((SampleAdapterBindingElementExtensionElement)(from)); this["overrideWsdlBuilder"] = adapterBinding.OverrideWsdlBuilder; this["usePredefinedSchemas"] = adapterBinding.UsePredefinedSchemas; this["count"] = adapterBinding.Count; this["enablePerfCounters"] = adapterBinding.EnablePerfCounters; this["enableConnectionPooling"] = adapterBinding.EnableConnectionPooling; } #endregion BindingElementExtensionElement Methods } }