Click here to Skip to main content
13,451,496 members (44,441 online)
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 15 Dec 2011

No More Pain to Configure WCF 4 Services

, 15 Dec 2011
Rate this:
Please Sign up or sign in to vote.
WCF configuration is much more complex compared to ASP.NET Web Services.

Developers who have worked with ASP.NET Web Services (ASMX) and WCF always feel that using a predecessor was much more easier. This is because WCF configuration is much more complex compared to ASP.NET Web Services.


With ASMX, you were able to define a [WebMethod] operation and the runtime automatically provides a default configuration for the underlying communications. When moving to WCF 3.x, on the other hand, developers have to know enough about the various WCF configuration options to define at least one endpoint.

In an effort to make the overall WCF experience just as easy as ASMX, WCF 4 comes with a new “default configuration” model that completely removes the need for any WCF configurations. If you don’t provide any WCF configuration for a particular service, the WCF 4 runtime automatically configures your service with some standard endpoints and default binding/behavior configurations. This makes it much easier to get a WCF service up and running, especially for those who aren’t familiar with the various WCF configuration options.

Let’s discuss some of the standard configuration options that WCF 4 supports:

  1. Default Endpoints
  2. Default Protocol Mapping
  3. Default Binding Configurations
  4. Default Behavior Configurations

1. Default Endpoints

With WCF 3.X, if you try to host a service without configured endpoints, the ServiceHost instance will throw an exception informing you that you need to configure at least one endpoint. With WCF 4, this is no longer the case because the runtime automatically adds one or more ‘default endpoints’ for you.

Now a question comes to mind, How is this done in WCF 4?

The answer to that is: When the host application calls the Open method on the ServiceHost instance, it builds an internal service description from the application configuration file. Then it checks the count of configured endpoints. If it is still zero, then it will call the AddDefaultEndpoints public method and the method will add a default endpoint per base address for each service contract implemented by the service.

Clear or Confused??

Let us take an example to be more clear on it.

If the service implements two service contracts and you configure the host with a single base address, AddDefaultEndpoints will configure the service with two default endpoints (one for each service contract). However, if the service implements two service contracts and the host is configured with two base addresses (one for HTTP and one for TCP), AddDefaultEndpoints will configure the service with four default endpoints.

I hope now it is clear….if not…please go through the link provided for more details on it: New features in WCF 4 that will instantly make you more productive:

2. Default Protocol Mapping

In .NET Framework 4.0, the default protocol mapping between the transport protocol schemes and the built-in WCF bindings are as follows:

    <add scheme="http" binding="basicHttpBinding" 

           bindingConfiguration="" />
    <add scheme="net.tcp" binding="netTcpBinding" 

    <add scheme="net.pipe" binding="netNamedPipeBinding" 

    <add scheme="net.msmq" binding="netMsmqBinding" 


You can override these mappings at the machine level by adding this section to the machine.config file and modifying the bindings as per your needs.

If you want to override this mapping at application level, then you can override the above section in the application/web config file.

3. Default Binding Configurations

In WCF 3.x, binding can be done like this:

        <binding name="BasicBindingMtom" messageEncoding="Mtom"/>
      <service name="HelloService">
        <endpoint address="mtom" binding="basicHttpBinding"



Here, the BasicBindingMtom binding configuration overrides the defaults for BasicHttpBinding by changing the message encoding to Mtom. However, this binding will take effect only when you apply it to a specific endpoint through the bindingConfiguration attribute.

With WCF 4, binding can be done like this:

    <binding messageEncoding="Mtom"/>

No name attribute is required. This feature gives you a simple mechanism to define a standard set of binding defaults that you can use across all your services without imposing any complexities of binding configurations.

4. Default Behavior Configurations

With WCF 4, it is possible to define the default behavior configurations for services and endpoints.

      <serviceMetadata httpGetEnabled="true"/>

The above configuration turns on the metadata for any service that doesn’t come with explicit behavior configuration.

In WCF 4, behavior configuration also supports an inheritance model. It means that if the application defines a behavior using the same name as the one already defined in machine.config, the application specific behavior configuration will get merged with the machine configuration.

With these new additions in WCF 4, it will be easier for developers to configure services. I am sure that many developers will feel relaxed by having these new features in WCF and will also start using them. That is all from my side for this post.

Hope this helps!


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


About the Author

Sandeepkumar Ramani
Technical Lead
India India
I write software using Microsoft web technologies since 2008. I have successfully delivered software products for Fortune 500 companies and startups.

Microsoft Certified Technologies Specialist - Web Applications Development with Microsoft .NET Framework 4.

Awarded as Microsoft Community Contributor of the year 2011.

Received several awards at various forums and my various articles got listed as "Article of the day" at ASP.NET Microsoft Official Website

Visit My Blog:

Area of Expertise:
C#, ASP.NET, Web Services, WCF, ASP.NET MVC, SQL Server, WEB API, AngularJS, jQuery

You may also be interested in...


Comments and Discussions

GeneralMy vote of 3 Pin
abdurahman ibn hattab16-Dec-11 0:25
memberabdurahman ibn hattab16-Dec-11 0:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180318.3 | Last Updated 15 Dec 2011
Article Copyright 2011 by Sandeepkumar Ramani
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid