This is the Day 11 article. If you have not read previous articles, please go through the following articles:
- Day 1 - WCF Introduction and Contracts
- Day 2 - WCF Fault Contracts
- Day 3 - WCF Message Exchange Patterns
- Day 4 - WCF DataContract
- Day 5 - WCF Difference between service application and service library
- Day 6 - WCF Serialization Part 1
- Day 7 - WCF Serialization Part 2
- Day 8 - WCF Opt-In VS Opt-Out
- Day 9 - WCF Message Contract
- Day 10 - WCF Address Binding & Contract
Introduction
In this article we will see how to configure a service using a XML-based configuration file i.e. web.config. We will also see definition of endpoints, multiple endpoints and publishing metadata.
To understand service configuration, first create a new project using the WCF Service Application template. Once you have completed the service application creation part, open the web.config file. The configuration information for a service is contained within a system.servicemodel element in the web.config file. Under the first level of the system.servicemodel element, there are behaviors, bindings and services. We can define various behaviors like endpoint behaviors and servicebehaviors under the behaviors element. In the binding section different bindings like basicHttpBinding, wsHttpBinding etc. In our example we define basicHttpBinding. Under the services element, there is a service element and under this element we can define endpoints. We must specify at least one endpoint, otherwise we will get an error during runtime.
The <system.serviceModel> Element
- system.serviceModel is a root element of all WCF configuration elements. The configuration information for a service is contained within a system.servicemodel element.
<behaviors> Element
- <endpointBehaviors>
This configuration section represents all the behaviors defined for a specific endpoint.
- <serviceBehaviors>
This configuration section represents all the behaviors defined for a specific service.
<bindings> Element
The element contains the specifications for all bindings that can be used by any endpoint defined in any service.
- <basicHttpBinding>
This element represents a binding that a WCF service can use to configure and expose endpoints.
- <binding>
The binding element can be a system provided binding or can be a custom binding.
<services> Element
The services element contains the specifications for all services the application hosts.
- <service>
This element contains two attributes, name and behaviorConfiguration. The Name specifies the type that provides an implementation of the ServiceContract and behaviorConfiguration specifies the name of the behavior elements found in the behaviors element.
- <endpoint>
Endpoint requires address, binding and contract. We have already seen these in my previous article.
Multiple Bindings
In the preceding example we are defining a single binding i.e. basicHttpBinding. Now you may ask, can we define multiple bindings? The answer is yes, we can define multiple bindings for different clients. To define multiple bindings we need to define multiple endpoints.
When creating multiple endpoints, we should remember that the address should be unique. If the two endpoints use the same address, an error will be thrown at runtime.
You can define the same address when you have a service that uses two interfaces. Here you can have two endpoints with the same address, but the contract name must be different.
Now let's see how to create multiple bindings.
Base Address
In the preceding example we specify an address as an absolute address. It is a very easy method to understand. We can also specify a relative address. When we are using multiple endpoints then it is an efficient approach to specify the relative address method.
Specify the base address under the host element for each service. We can also add multiple base addresses by using the add method.
In the previous example we define multiple endpoints and there is an address like "http://localhost:8000/BindingConfigService". This portion of address is common in the first two endpoints. So we can set this common portion as the base address under the host element.
Publishing Metadata
We can publish service metadata using the HTTP-GET protocol. To expose this metadata we need to create a metadata exchange endpoint. This endpoint can append "mex" to the HTTP address. The endpoint uses mex as the address, mexHttpBinding as the binding and IMetadataExchange interface as a contract. We also need to set the attribute of the servicemetadata i.e. HttpGetEnabled to true in the servicebehaviors.
The client can access this metadata using a HTTP-GET request with a ?wsdl query string appended.
Conclusion
It is normally a good practice to use a configuration file when specifying endpoints because we can make changes in endpoints without a code recompile.