Click here to Skip to main content
12,746,681 members (28,615 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 4 Jul 2014

2 Simple Ways to Configure WCF Binding

, 4 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Here are 2 simple ways to configure WCF binding

In one of previous WCF Tutorial for beginners, I discussed about the ABCs of WCF Service Endpoint. An Endpoint of Service has the following three parts also known as ABC (Address, Binding, Contract):

  1. Address defines Where: Where is the location of WCF Service?
  2. Binding defines How: How we can access the WCF Service?
  3. Contract defines What: What a WCF Service expose?

As Binding in Windows Communication Foundation defines how to communicate with a WCF Service, it specifies the following details:

  • Communication protocol
  • Encoding method
  • Other optional factors as transactions, reliable sessions and security, etc.

In order to get in-depth and detailed understanding of WCF Binding, you can follow other WCF Tutorial on “Understanding WCF Binding and Channel Stack“.

In this WCF Tutorial, we are going to discuss how we can configure WCF Binding using two different approaches? We can configure our WCF Service administratively (through configuration file) as well as programmatically.

Consider we have a WCF Service (say StudentService) and it’s hosted in IIS. There are a number of built-in WCF Bindings and for this implementation, we are using “wsHttpBinding” as binding. All built-in bindings have its default settings and we normally don’t change them. But for a specific requirement, we can modify it accordingly.

WCF Binding Configuration – Configuration file Approach

While configuring WCF through configuration file, we can easily understand how the above discussed concepts are configured for a WCF Service? How ABCs of a Service endpoint are configured? How WCF Bindings are defined to communicate a Service? etc.

             <service name="StudentService">

                                             <dns value="localhost"/>
                        <endpoint address="mex"

Using the above configuration, wsHttpBinding will be used with its default settings but if we need to change these default settings, we can do that by adding “bindingName" parameter (bindingName="studentWSConfiguration") to endpoint and provide the following configuration:

             <binding name="studentWSConfiguration" 
                                 MaxReceivedMessageSize ="70000″ 
                                 MessageEncoding="Text" />

WCF Binding Configuration – Programmatical Approach

Although it’s recommended to use the Configuration file approach for WCF binding configuration because we don’t need to change the code and compile again during deployment, still we can achieve the above programmatically as follows:

ServiceHost studentServiceHost = null;
       //Base Address for StudentService
       Uri httpBaseAddress = new Uri(“http://localhost:4321/StudentService");

       //Instantiate ServiceHost
       studentServiceHost = new ServiceHost(typeof(StudentService.StudentService),

       //Create Binding to add to end point
       WSHttpBinding studentWSConfiguration = new WSHttpBinding();
       wshttpbind.MaxReceivedMessageSize = 70000;
       wshttpbind.MessageEncoding = WSMessageEncoding.Text;

       //Add Endpoint to Host
       (StudentService.IStudentService), studentWSConfiguration, “");

       //Metadata Exchange
       ServiceMetadataBehavior serviceBehavior = new ServiceMetadataBehavior();
       serviceBehavior.HttpGetEnabled = true;

       Console.WriteLine(“Service is live now at : {0}", httpBaseAddress);
  catch (Exception ex)
       studentServiceHost = null;
       Console.WriteLine(“There is an issue with StudentService" + ex.Message);

This is how we can configure WCF Bindings, but remember WCF (Windows Communication Foundation) is extensible so we can define our custom binding also for a very specific need.

Other Related Articles

The post 2 simple ways to configure WCF binding appeared first on WCF Tutorial.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Imran Abdul Ghani
Software Developer (Senior) Emaratech
United Arab Emirates United Arab Emirates
Imran Abdul Ghani has more than 10 years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET(MCSD.NET) since 2005. You can reach his blogging at WCF Tutorials, Web Development, SharePoint for Dummies.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170215.1 | Last Updated 5 Jul 2014
Article Copyright 2014 by Imran Abdul Ghani
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid