- dynamicwcffactory.zip
- DynamicWCFFactory
- DynamicWCFFactory.gpState
- DynamicWCFFactory.sln
- DynamicWCFFactory.vsmdi
- DynamicWCFFactory
- DynamicWCFFactoryTests
- LocalTestRun.testrunconfig
- TestResults
- UDDIServiceFactory
- WcfService
|
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Reflection;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
namespace UDDIServiceFactory.Configuration
{
/// <summary>
/// Provides methods to discover and configure various aspects of a WCF client.
/// </summary>
public class BindingConfigurationHelper
{
#region Members
/// <summary>
/// The configuration from the application configuration file.
/// </summary>
private System.Configuration.Configuration _appConfig = null;
/// <summary>
/// The list of key-value pair, property name and property value, used to configure a <see cref="Binding"/>.
/// </summary>
public class BindingProperties : Dictionary<string, IConvertible>
{
}
#endregion
#region Private methods
/// <summary>
/// Sets the properties of the binding by applying the properties from the property list.
/// </summary>
/// <param name="binding">The <see cref="System.ServiceModel.Channels.Binding"/> to configure.</param>
/// <param name="properties">The list of properties to apply</param>
private void SetBindingProperties(Binding binding, BindingProperties properties)
{
CustomBinding customBinding = binding as CustomBinding;
if (customBinding != null)
{
foreach (BindingElement element in customBinding.Elements)
{
Type elementType = element.GetType();
foreach (KeyValuePair<string, IConvertible> _keyValue in properties)
{
// Retrieve the PropertyInfo for the specified property.
PropertyInfo _propertyInfo = elementType.GetProperty(_keyValue.Key, BindingFlags.Instance | BindingFlags.Public);
if (null != _propertyInfo)
{
try
{
_propertyInfo.SetValue(binding, _keyValue.Value, null);
}
catch
{
// HACK: the catch block is intentionally doing nothing to
// allow the loop to continue processing the property list.
}
}
}
}
}
// Retrieve the type of the binding.
Type _bindingType = binding.GetType();
// Process the collection of properties to set.
foreach (KeyValuePair<string, IConvertible> _keyValue in properties)
{
// Retrieve the PropertyInfo for the specified property.
PropertyInfo _propertyInfo = _bindingType.GetProperty(_keyValue.Key, BindingFlags.Instance | BindingFlags.Public);
if (null != _propertyInfo)
{
try
{
_propertyInfo.SetValue(binding, _keyValue.Value, null);
}
catch
{
// HACK: the catch block is intentionally doing nothing to
// allow the loop to continue processing the property list.
}
}
}
}
/// <summary>
/// Sets the properties of the <see cref="System.ServiceModel.Channels.Binding"/> by applying the configuration section from the config file.
/// </summary>
/// <param name="binding">The <see cref="System.ServiceModel.Channels.Binding"/> to configure.</param>
/// <param name="configurationElement">The configuration element from the config file.</param>
private void SetBindingProperties(Binding binding, IBindingConfigurationElement configurationElement)
{
// Retrieve the type of the binding.
Type _bindingType = binding.GetType();
// Process the collection of properties to set.
foreach (PropertyInfo _configProperty in configurationElement.GetType().GetProperties())
{
// Retrieve the PropertyInfo for the specified property.
PropertyInfo _bindingProperty = _bindingType.GetProperty(_configProperty.Name, BindingFlags.Instance | BindingFlags.Public);
if (null != _bindingProperty)
{
try
{
object _value = _configProperty.GetValue(configurationElement, null);
_bindingProperty.SetValue(binding, _value, null);
}
catch
{
// HACK: the catch block is intentionally doing nothing to
// allow the loop to continue processing the property list.
}
}
}
}
#endregion
#region Public methods
/// <summary>
/// Configures a new <see cref="System.ServiceModel.Channels.Binding"/> using the key-value pairs from the <see cref="BindingProperties"/> list.
/// </summary>
/// <typeparam name="TBinding">The type of <see cref="System.ServiceModel.Channels.Binding"/> to initialize and configure.</typeparam>
/// <param name="bindingName">The name to be given to the new <see cref="System.ServiceModel.Channels.Binding"/>.</param>
/// <param name="properties">The key-value pair of properties used to configure the new <see cref="System.ServiceModel.Channels.Binding"/>.</param>
/// <returns>A new <see cref="System.ServiceModel.Channels.Binding"/> configured.</returns>
public TBinding ConfigureClientBinding<TBinding>(string bindingName, BindingProperties properties)
where TBinding : Binding, new()
{
// Initialize a new instance of the binding class.
TBinding _binding = new TBinding();
_binding.Name = bindingName;
// Set the properties of the new binding.
SetBindingProperties(_binding, properties);
// Return the binding.
return _binding;
}
/// <summary>
/// Configures a new <see cref="System.ServiceModel.Channels.Binding"/> by retrieving the matching <see cref="IBindingConfigurationElement"/> from the configuration file.
/// </summary>
/// <typeparam name="TConfiguration">The type of the <see cref="IBindingConfigurationElement"/> element to retrieve from the config file.</typeparam>
/// <typeparam name="TBinding">The type of <see cref="System.ServiceModel.Channels.Binding"/> to initialize and configure.</typeparam>
/// <param name="bindingName">The name to be given to the new <see cref="System.ServiceModel.Channels.Binding"/>.</param>
/// <returns>A new <see cref="System.ServiceModel.Channels.Binding"/> configured.</returns>
public TBinding ConfigureClientBinding<TConfiguration, TBinding>(string bindingName)
where TConfiguration : class, new()
where TBinding : Binding, new()
{
// Initialize a new instance of the binding class.
TBinding _binding = new TBinding();
_binding.Name = bindingName;
// Retrieve the configured binding element from the configuration file.
TConfiguration _configurationElement = GetBindingTypeByName<TConfiguration>(bindingName);
// Set the properties of the binding.
SetBindingProperties(_binding, (IBindingConfigurationElement)_configurationElement);
// Return the binding.
return _binding;
}
/// <summary>
/// Configures a new <see cref="System.ServiceModel.Channels.Binding"/> by retrieving the matching <see cref="IBindingConfigurationElement"/> from the configuration file.
/// </summary>
/// <typeparam name="TConfiguration">The type of the <see cref="IBindingConfigurationElement"/> element to retrieve from the config file.</typeparam>
/// <param name="binding">The <see cref="System.ServiceModel.Channels.Binding"/> to configure.</param>
public void ConfigureClientBinding<TConfiguration>(Binding binding)
where TConfiguration : class, new()
{
// Retrieve the configured binding element from the configuration file.
TConfiguration _configurationElement = GetBindingTypeByName<TConfiguration>(binding.Name);
// Set the properties of the binding.
SetBindingProperties(binding, (IBindingConfigurationElement)_configurationElement);
}
/// <summary>
/// Configures the given <see cref="System.ServiceModel.Channels.Binding"/> using the key-value pairs from the <see cref="BindingProperties"/> list.
/// </summary>
/// <param name="binding">The <see cref="System.ServiceModel.Channels.Binding"/> to configure.</param>
/// <param name="properties">The key-value pair of properties used to configure the new <see cref="System.ServiceModel.Channels.Binding"/>.</param>
public void ConfigureClientBinding(Binding binding, BindingProperties properties)
{
// Set the properties of the new binding.
SetBindingProperties(binding, properties);
}
/// <summary>
/// Returns the requested <see cref="IBindingConfigurationElement"/> from the binding configuration section
/// </summary>
/// <typeparam name="T">The type of the <see cref="IBindingConfigurationElement"/> requested.</typeparam>
/// <param name="bindingName">The name of the new <see cref="System.ServiceModel.Channels.Binding"/>.</param>
/// <returns>Either the given <see cref="IBindingConfigurationElement"/> or a new <see cref="System.ServiceModel.Channels.Binding"/> for the requested T type.
/// <remarks>
/// The new <see cref="System.ServiceModel.Channels.Binding"/> element of type T is initialized using the default WCF configuration parameters.
/// </remarks>
/// </returns>
private T GetBindingTypeByName<T>(string bindingName) where T : class, new()
{
// Retrieve the ServiceModel configuration section group.
ServiceModelSectionGroup _serviceModel = ServiceModelSectionGroup.GetSectionGroup(_appConfig);
BindingsSection _bindings = _serviceModel.Bindings;
// Browse through all configuration groups.
foreach (ExtensionElement _bindingExtension in _serviceModel.Extensions.BindingExtensions)
{
// Selectively retrieve the requested configuration sections.
foreach (IBindingConfigurationElement _element in _bindings[_bindingExtension.Name].ConfiguredBindings)
{
// When the type and the name of the binding matches,
if ((_element.GetType() == typeof(T)) && (_element.Name == bindingName))
{
// return the result to the caller.
return (T)_element;
}
}
}
// No binding found.
return default(T);
}
#endregion
#region Ctor
/// <summary>
/// Initializes a new instance of the BindingConfigurator class.
/// <remarks>
/// The configuration file is opened for read-only access.
/// </remarks>
/// </summary>
public BindingConfigurationHelper()
{
// Get the application configuration file.
_appConfig = ConfigurationAgent.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
being IT-Engineer since 1995 I am freelancing since then. From networks to programming, from system administration to DBA, from AIX to Windows I offer a wide range of IT-Skill's without considering me the ultimate expert in each area.
I try to avoid complexity wherever I can and so my philosophy is to strictly follow KISS principles.