// Create and parametrize binding to address Mex EndPoint (Http or Tcp)
TransportBindingElement binding = null;
if ( this.MexEndPointAddress.StartsWith("http") )
{
binding = new HttpTransportBindingElement();
binding.MaxReceivedMessageSize *= 10;
}
else
binding = new TcpTransportBindingElement();
CustomBinding cusBinding = new CustomBinding(binding);
// Parametrize metadaLta download
MetadataExchangeClient client = new MetadataExchangeClient(cusBinding);
MetadataSet metadata = client.GetMetadata(new EndpointAddress(MexEndPointAddress));
MetadataImporter wsdlImporter = new WsdlImporter(metadata);
// Get all Contracts
Contracts = wsdlImporter.ImportAllContracts();
// Get all EndPoints
EndPoints = wsdlImporter.ImportAllEndpoints();
public Collection<ContractDescription> Contracts { get; set; }
public ServiceEndpointCollection EndPoints { get; set; }
public event EventHandler<ParametrizedEventArgs<Collection<ContractDescription>>> ContractsFilled;
public event EventHandler<ParametrizedEventArgs<ServiceEndpointCollection>> EndPointsFilled;
public class ParametrizedEventArgs<T> : EventArgs
public T Items;
// Client notification for Contracts
if (ContractsFilled != null)
ContractsFilled.Invoke(
this,
new ParametrizedEventArgs<Collection<ContractDescription>>() { Items = Contracts });
// Client notification for EndPoints
if (EndPointsFilled != null)
EndPointsFilled.Invoke(
new ParametrizedEventArgs<ServiceEndpointCollection>() { Items = EndPoints });
public class Factory
public static T GetObject<T>(System.ServiceModel.Channels.Binding binding, EndpointAddress epAddress)
ChannelFactory<T> factory = new ChannelFactory<T>(binding, epAddress);
return factory.CreateChannel();
private void Execute(Int32? productID)
ServiceEndpoint ep = (ServiceEndpoint)bindingSource2.Current;
if (ep != null)
System.ServiceModel.Channels.BindingElementCollection elements = ep.Binding.CreateBindingElements();
// Inside selected binding get the corresponding transport
System.ServiceModel.Channels.TransportBindingElement elem
= elements.Find<System.ServiceModel.Channels.TransportBindingElement>();
// Manage binding Element genericity
PropertyInfo prop = elem.GetType().GetProperty("MaxBufferSize");
if (prop != null)
prop.SetValue(elem, 65000000, null);
// Arbitrary increase of MaxReceivedMessageSize
elem.MaxReceivedMessageSize = 65000000;
// Get the encoding if existing to increase quotas and MaxArrayLength
System.ServiceModel.Channels.MessageEncodingBindingElement elem2
= elements.Find<System.ServiceModel.Channels.MessageEncodingBindingElement>();
prop = elem2.GetType().GetProperty("ReaderQuotas");
prop.PropertyType.GetProperty("MaxArrayLength").SetValue(prop.GetValue(elem2, null), 1000000, null);
// Re-create a custom binding with all these parameters to use it with our Factory
ep.Binding = new System.ServiceModel.Channels.CustomBinding(elements);
// Call through created channel by the factory
THB.Sample.ServiceContracts.IProductService service
= Factory.GetObject<ServiceContracts.IProductService>(ep.Binding, ep.Address);
if ( !productID.HasValue )
bindingSource1.DataSource = service.Get();
bindingSource1.DataSource = service.Get(productID.Value);
// Close the channel
((System.ServiceModel.Channels.IChannel)service).Close();
// Parametrize introspection
private void CreateIntrospector()
IntrospectorInstance = new Introspector();
IntrospectorInstance.ContractsFilled
+= new EventHandler<Introspector.ParametrizedEventArgs<Collection<ContractDescription>>>
(IntrospectorInstance_ContractsFilled);
IntrospectorInstance.EndPointsFilled
+= new EventHandler<Introspector.ParametrizedEventArgs<ServiceEndpointCollection>>
(IntrospectorInstance_EndPointsFilled);
void IntrospectorInstance_ContractsFilled(object sender
, Introspector.ParametrizedEventArgs<Collection<ContractDescription>> e)
try
treeView1.Invoke<System.Collections.ObjectModel.Collection<ContractDescription>>(FillContracts
, e.Items);
toolStripStatusLabel1.Text = "Getting EndPoints ...";
catch (Exception ex)
MessageBox.Show(ex.ToString());
void IntrospectorInstance_EndPointsFilled(object sender
, Introspector.ParametrizedEventArgs<ServiceEndpointCollection> e)
treeView1.Invoke<ServiceEndpointCollection>(FillEndPoints, e.Items);
private void FillContracts(System.Collections.ObjectModel.Collection<ContractDescription> items)
BuildTree(treeView1.Nodes.Add(SERVICES), items);
private void FillEndPoints(ServiceEndpointCollection items)
BuildTree(treeView1.Nodes.Add(ENDPOINTS), items);
toolStripStatusLabel1.Text = "";
public static class Helper
public static void Invoke<T>(this Control control, Action<T> action, T param)
if (control.InvokeRequired)
control.Invoke(action, param);
action(param);
private void BuildTree(TreeNode node, object o)
if (o != null)
Type t = o.GetType();
if (t.GetInterface("IEnumerable") != null)
foreach (object item in (IEnumerable)o)
BuildTree(node.Nodes.Add(item.ToString()), item);
foreach (PropertyInfo p in t.GetProperties())
if (p.PropertyType.GetInterface("IEnumerable") != null && p.PropertyType != typeof(String))
BuildTree(node.Nodes.Add(p.Name), p.GetValue(o, null));
object value = p.GetValue(o, null);
TreeNode n = node.Nodes.Add(p.Name, String.Format("{0} = {1}", p.Name, value));
n.Tag = value;