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:
- Default Endpoints
- Default Protocol Mapping
- Default Binding Configurations
- 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
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
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"
<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"/>
<endpoint address="mtom" binding="basicHttpBinding"
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
With WCF 4, binding can be done like this:
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.
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!