WCF Simple Example in Visual Studio 2010 – part 2

This topic is covered in multiple posts

The previous post was the most simple example of creating a WCF service and calling it from a client application. In this post you can read how you can achieve the exact same behavior, but now manually adding every bit of configuration. So we’ll basically override the default endpoints, configure our won, resulting in exactly the same behavior.

The idea behind this is that you understand what WCF does and that you can do it yourself. Besides that, the previous .NET Framework versions don’t support the default endpoints, meaning that the solution at the end of this article also works in Visual Studio 2008.

What will we do?

  1. Add BasicHttpBinding endpoint
  2. Add MEX endpoint
  3. Add Metadata behavior

Metadata behavior
We’ll start with bullet three, simply because almost everything is already in place. The solution from the previous weblog post ended with this in the application configuration from the ConsoleHost application.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior>
                    <serviceMetadata httpGetEnabled="True"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>
</configuration>

The service behavior has no name, making it the default for all services in WCF4. All we have to do is give it a name. Change line 6 into the following.

                <behavior name="MyBehavior">

Adding BasicHttpBinding endpoint
We’ll now add the first endpoint using the BasicHttpBinding. Right under <system.serviceModel> we’ll add the <services /> tag and add our service. Completely empty this will result in the following.

<services>
    <service name="">
        <endpoint 
            address="" 
            binding="" 
            contract="" />
    </service>
</services>

Now we have to fill in the blanks. More information can be found in the articles about the WCF ABC and hosting the service.

  • Name
    This is a bit misleading, as you can’t just enter any name, it’s actually the type of the service.
    This must be the fully qualified name of the implementation of our service. This means not the interface, but the class. Its name is EmailValidator but its full name includes the namespace, resulting in EmailService.EmailValidator.
  • Address
    The address we don’t have to fill in, because the base address in our code already defines it. You can also define this in configuration as you can read in the post Address.
  • Binding
    The binding is simply basicHttpBinding. Note the camelCasing.
  • Contract
    Here we should enter the contract, which is our interface. As with the name attribute, we need to enter the fully qualified name, resulting in EmailService.IEmailValidator.

You can view the end result at the end of this article and in the download, also at the end of this article.

Adding MEX endpoint
The MEX endpoint needs the metadata behavior configured, but we’ll get back to that. First we need to add the endpoint. Again the WCF ABC, an address, binding and contract.

  • Address
    Because we cannot have this endpoint at the same address as the BasicHttpBinding endpoint, we need to enter “mex” here.
  • Binding
    This is simple, it’s just mexHttpBinding
  • Contract
    This is simple as well, although a bit weird, it HAS to be IMetadataExchange. Not the fully qualified name or anything, it has to be just this.

Enabling the metadata behavior
Now all we have to do is add the metadata behavior. This is done at the root of the service, where we first entered the name. There’s another attribute there called behaviorConfiguration. We have to enter the name of our behavior configuration there, which is MyBehavior.

End result
This results in the following configuration.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <services>
            <service name="EmailService.EmailValidator" behaviorConfiguration="MyBehavior">
                <endpoint 
                    address="" 
                    binding="basicHttpBinding" 
                    contract="EmailService.IEmailValidator" />
                <endpoint 
                    address="mex" 
                    binding="mexHttpBinding" 
                    contract="IMetadataExchange" />
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="MyBehavior">
                    <serviceMetadata httpGetEnabled="True"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>
</configuration>

You can download the Visual Studio 2010 and the Visual Studio 2008 solution right here.

You may also like...

2 Responses

  1. anil says:

    It’s was a noce stub.. for WCF. Thanks..

  2. Wasa says:

    This is great help, Clean steps, Hoping lot more.
    Thank you!

Click on a tab to select how you'd like to leave your comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.