- 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 DynamicWCFFactory
{
/// <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.
}
}
}
}
#endregion
#region Public methods
/// <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);
}
#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.