WCF Series: Config Files

In this article we will be Learning about the Config Files in WCF.

In WCF you can define EndPoints in Code and also by using the Config file.

Defining EndPoints:

  1. In code:

    Gives you complete control and ensures that no one can change the Endpoint once deployed.

    To change the endpoints, you need to recompile the application.
     
  2. In configuration files:

    Enables you to change the EndPoints without rebuilding the application.

    Enables you to add a Service Reference to a Self Hosted service, i.e. If you define an EndPoint config file, you can add a Service Reference to a Service hosted in a Console Application or a Windows Service.

    Configuration Files can be written manually or by using the Service Configuration Editor which makes life easy for the Developers.

Let's take a look at the Configuration that is part of the WCF Configuration File:

ServiceModel Cofiguration:

<system.serviceModel> contains configuration settings.

Core Elements:

<services> contains service type definitions i.e List of all the Services. You typically have one Service defined in a Config File, although you could have more than one.

<endpoint> contains an address, binding and contract for a service.

<bindings> contains one or more binding sections.

- Here you Configure individual bindings

<behaviours> contains definition of service or client behaviours.

- This is the way you could configure a service or client

<services> Element:

The Services Element contains one or more individual <service> elements.
The <service> element provides definitions of the service type and includes base addresses and endpoints for that type.

The below screenshot shows an example of how the <services> element is defined:

WCF Series:Config Files

As you can see in the screenshot, there are two endpoints defined. One is the normal endpoint which gets added for the service. The Metadata Endpoint with binding mexHttpBinding and the contract IMetadataExchange, enables us to add a Service Reference. When the Client adds a Service Reference the metadata needs to be downloaded and the presence of this endpoint helps us download the metadata.

Multiple EndPoints:

You could define multiple endpoints for a service.

You might provide a HTTP endpoint for external use and a TCP endpoint for internal use.

Config2.gif

As seen in the screenshot above I can easily create an internal and external endpoint for my service. The Contract remains the same in both the Endpoints because we are using the same service; what changed is the Binding.


Lets see some code examples:

In this sample we have created a HttpEndPoint and TCPEndPoint as shown below:

The HTTPEndPoint uses a BasicHTTPBinding whereas TCPEndPoint uses NetTcpBinding.

Config3.gif


In the next post I will show you an example of creating endpoints in the application and Configuration File.


Similar Articles