ARTICLE

Programmatically Finding Binding Elements in WCF Binding

Posted by Dhananjay Kumar Articles | WCF with C# February 26, 2011
How to enumerate the binding elements in a binding.
Reader Level:

Binding is one of the important aspects of WCF Programming. It deals with, "How Service and client will talk to each other?" Binding is made up of mutiple binding elements. When different binding elements are stacked together they form a Binding. Different binding types are made up of different binding elements. 

Always the question arises, "What are the different binding elements of a particular binding?"

And we can find the solution of the above question through code. What all we need to do: 
  1. Create instance of Binding 
  2. Enumerate through Binding elements. 
We are going to create a console application to enumerate through all the binding elements in a binding. 

Add the following references to the console application project:

WCFBinding1.gif
 
Next we are going to write a function. This function will take an object for a Binding as it's input parameter and then prints all the binding elements. 

WCFBinding2.gif
 
The above function is very simple; it just enumerates above all the binding elements. 

We may call the above function as below: 

WCFBinding3.gif

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels; 

namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {

            EnumerateBindingElements(new BasicHttpBinding());
            EnumerateBindingElements(new NetMsmqBinding());
            EnumerateBindingElements(new WSHttpBinding());
            Console.ReadKey(true);

        }

        static void EnumerateBindingElements(Binding binding)
        {
            Console.WriteLine("Binding : " + binding.GetType().Name);
            BindingElementCollection bindingElements = binding.CreateBindingElements();
            foreach (BindingElement e in bindingElements)
            {
                Console.WriteLine(e.GetType().FullName);
            }          

        }
    }
}
Output 
 
WCFBinding4.gif

erver'>
Login to add your contents and source code to this article
post comment
     
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.