Web services are an integral part of information dissemination amongst heterogenous information sources. Micosoft’s Windows Communication Foundation (WCF) services provide a quick and easy foundation from which to create rich web services. In this post I’m going to look at the main components of the App.config file and the web service contract and implementation.

WCF Endpoints

The three main parts of a WCF endpoint are the address, the binding and the contract. Understanding what an endpoint is made up of helps to understand the function of an endpoint.

Endpoints are used to describe to the client how it may connect to the service and what *endpoint* connections it may do this by. Depending on the binding and contract, the endpoint can be used for different purposes. A wsHttpBinding can be exposed for interacting with the web service and its business logic whilst a mex endpoint with appropriate paramaters can be used to describe the server web service to the client. Connecting to the mex endpoint you can see the WSDL for the web service.

Addresses

The address of the endpoint. When using the address attribute in the endpoint tag of your configuration file, the address is relative. A good example of this is typically the “mex” address which is relative to the base address of the web service. So typically http:///webservice/mex. The address is the location at which the service can be contacted.

Bindings

Bindings define how WCF should provide the underlying communication features of the web service. Their are different types of bindings depending on the purpose. Ad mentioned earlier, their are bindings for meta exchange and different bindings for various transport types (including HTTP and TCP).

Contract

The contract that the endpoint will use in its negotiations with a client. A contract states, what functionality the service will provide to its consumers. This includes how the services methods can be invoked, what parameters they can be invoked with and what types the invocation returns (if any).

namespace EnterpriseDotNet.MmoAccountService
{
    [ServiceContract]
    public interface IMmoAccount
    {
        [OperationContract]
        List<MmoCharacter> GetCharactersForAccount(string account);
    }
 
    [DataContract]
    public class MmoCharacter
    {
        [DataMember]
        public string Name { get; set; }
 
        [DataMember]
        public int Level { get; set; }
    }
}

This is an example of a contract for a ficticous MMO login server. The attributes are used to decorate the interface with meta data.

namespace EnterpriseDotNet.MmoAccountService
{
    public class MmoAccountService : IMmoAccount
    {
        public List<MmoCharacter> GetCharactersForAccount(string account)
        {
            List<MmoCharacter> ret = new List<MmoCharacter>();
 
            using (MmoDataContext context = new MmoDataContext(SQLConnectionString.Settings.Default.SQLServerConnectionString))
            {
                ret = (from c in context.MmoCharacters
                       where c.MmoAccount.Email.Equals(account)
                       select new MmoCharacter
                       {
                           Name = c.Name,
                           Level = c.Level
                       }).ToList();
            }
 
            return ret;
        }
    }
}

This class implements the interface (read service contract) presented earlier. Looking at this service contract it returns a list of characters for a specified account.

Service Behaviours

Service behaviours define how certain aspects of the web service should be configured or behave. When creating a new web service library in Visual Studio 2008, the default behaviour defines two things; serviceMetadata and serviceDebug. The former states that the mex endpoint or meta data exchange will be available via a httpGet and the latter can be used to configure debugging information (exceptions).

There is obviously far more to WCF services than this post touched on. This is an ‘at a glance’ overview of what goes into the definition and configuration of a WCF service.