Every service is associated with an address that defines where the service is, a binding that defines how to communicate with the service, and a contract that defines what the service does. This triumvirate governing the service is easy to remember as the ABC of the service.
In fact, WCF formalizes this relationship in the form on an endpoint. The endpoint is the fusion of the address, contract, and binding (see Figure 5
). Every endpoint must have all three, and the service exposes the endpoint. Logically, the endpoint is the service's interface, and is analogous to a CLR or COM interface.
|Figure 5. An Endpoint: The endpoint is the fusion of the address, contract, and binding|
Every service must expose at least one business endpoint and each endpoint has exactly one contract. All endpoints on a service have unique addresses, and a single service can expose multiple endpoints. These endpoints can use the same or different bindings and can expose the same or different contracts. It is important to point out that nothing in the service code pertains to its endpoints and they are always external to the service code. You can configure endpoints either administratively using a config file or programmatically.
Administrative Endpoint Configuration
Consider the following service definition:
Class MyService : IMyContract
shows the required entries in the host process config file. Administrative configuration is the option of choice in the majority of cases because it provides the flexibility to change the service address, binding, and even exposed contracts without rebuilding and redeploying the service.
shows a config file defining a single service that exposes multiple endpoints. Note that the endpoint must provide a base address that is consistent with the binding, such as HTTP with WSHttpBinding. A mismatch causes an exception at the service load time. You can configure multiple endpoints with the same base address as long as the URI is different:
<endpoint Address =
You can also omit the address, in which case, the service uses the base address registered with the host (and the host must provide a matching base address):
You can provide only a URI, in which case, the address is that relative address under the base address (and the host must provide a matching base address):
When providing a base address, the endpoint overrides any base address provided by the host:
Note that when hosting with IIS, the service must use the IIS base address (computer name + virtual directory over HTTP).
Programmatic Endpoint Configuration
Programmatic endpoint configuration is completely equivalent to administrative configuration yet it does not resort to a config file and instead, you make programmatic calls to add endpoints to the ServiceHost instance. Again, these calls are always outside the scope of the service code. ServiceHost provides overloaded versions of the AddServiceEndpoint()
public class ServiceHost : ServiceHostBase
public ServiceEndpoint AddServiceEndpoint(
Type implementedContract, Binding binding,
demonstrates programmatic configuration of the same endpoints as in Listing 3
. To rely on the host base address, provide just the URI as the address:
Uri tcpBaseAddress = new
ServiceHost serviceHost = new
Binding tcpBinding = new NetTcpBinding();
//Use base address as address
IMyContract) , tcpBinding,"");
//Add relative address
//Ignore base address