Configuring Default Endpoints

When programming with WCF, we will often need to create some simple WCF services for testing our ServiceContracts. These services often use very simple and typical endpoint and binding definitions. However, every time we need to set up such a service, we have to define the same endpoint and binding settings again and again, which really adds much duplicated work. Fortunately, WCF 4.0 introduces the Default Endpoint feature which saves us from defining common endpoint/binding settings repeatedly.

How to do it...

The steps for using a default endpoint are quite straightforward:

  1. Create a new Console Application project in Visual Studio 2010 targeting .NET Framework 4.0.
    How to do it...
  2. Add the ServiceContract in the service project and implementation types we need in the service project. We can use any valid ServiceContract and its corresponding implementation class here. For example, the following IHelloService service containing a single SayHello operation is used in our sample service application here:
    [ServiceContract]
        public interface IHelloService
        {
            [OperationContract]
            string SayHello(string user);
        }
  3. Add service configuration entries for the service type (defined in step 2) in the app.config file. We do not need to define any endpoint and binding configuration here. The following screenshot demonstrates the necessary service configuration elements:
    How to do it...
  4. Start a ServiceHost and specify the service type we want to run (see the following code):
    using (ServiceHost host = new ServiceHost(typeof(HelloService)))
    {
    host.Open();
    Console.ReadLine();
    }
    

How it works...

In the previous service definition and hosting code, we haven’t added any endpoint and binding configuration. The magic behind scene is the Default Endpoints feature. When we start a WCF 4.0 service host, if the runtime cannot find any endpoint defined (via app.config or code), it will automatically create a default endpoint for each ServiceContract implemented for the service class. The default endpoints will choose the proper binding based on its endpoint address (the URL scheme) by looking up a protocolMapping list predefined in the system configuration store (within the .NET 4 Machine.config.comments file). The following screenshot shows the protocolMapping list:

How it works...

For our previous example, since the endpoint address uses the HTTP scheme (derives from the baseAddress), the runtime will choose the BasicHttpBinding according to the protocolMapping list.

By using the DumpEndpoint function, we can confirm that Default Endpoint with BasicHttpBinding has been correctly set up in the previous case (refer to the next screenshot).

host.Open();
DumpEndpoint(host.Description.Endpoints);
…………….
}

private static void DumpEndpoint(ServiceEndpointCollection endpoints)
{
 foreach (ServiceEndpoint sep in endpoints)
 {
 Console.Write(“Address:{0}\nBinding:{1}\nContract:{2}\n”,sep.Address, sep.Binding.Name, sep.Contract);
 Console.WriteLine(“Binding Stack:”);
 foreach (BindingElement be in sep.Binding.CreateBindingElements())
 {
 Console.WriteLine(be.ToString());
 }
 }
}

The next screenshot shows the auto-configured default endpoint in the sample service.

How it works...

There’s more...

In addition to Default Endpoint, WCF 4.0 also provides the Default Binding feature which can save the life of developers who want to define a common binding setting for multiple endpoints. For example, we define the following anonymous binding configuration, which does not have an explicit name. Any endpoint that uses BasicHttpBinding will adopt the setting in this anonymous binding configuration.

There’s more...

See also

  • Complete source code for this recipe can be found in the \Chapter 2\recipe1\ folder