Click here to Skip to main content
15,888,610 members
Articles / Programming Languages / XML

No More Pain to Configure WCF 4 Services

Rate me:
Please Sign up or sign in to vote.
3.50/5 (2 votes)
15 Dec 2011CPOL4 min read 22.7K   8   1
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.

Why???

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: http://www.code-magazine.com/Article.aspx?quickid=1006061.

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:

XML
<protocolMapping>
    <add scheme="http" binding="basicHttpBinding" 
           bindingConfiguration="" />
    <add scheme="net.tcp" binding="netTcpBinding" 
           bindingConfiguration=""/>
    <add scheme="net.pipe" binding="netNamedPipeBinding" 
           bindingConfiguration=""/>
    <add scheme="net.msmq" binding="netMsmqBinding" 
           bindingConfiguration=""/>
</protocolMapping>

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:

XML
<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicBindingMtom" messageEncoding="Mtom"/>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="HelloService">
        <endpoint address="mtom" binding="basicHttpBinding"
                  bindingConfiguration="BasicBindingMtom"
                  contract="IHello"/>
      </service>
    </services>
  </system.serviceModel>
</configuration>

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:

XML
<basicHttpBinding>
    <binding messageEncoding="Mtom"/>
</basicHttpBinding>

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.

XML
<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

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!

License

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


Written By
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 https://www.asp.net/

Visit My Blog:
https://ramanisandeep.wordpress.com/


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

Comments and Discussions

 
GeneralMy vote of 3 Pin
abdurahman ibn hattab16-Dec-11 0:25
abdurahman 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.