Windows Communication Foundation (WCF) - Putting it All Together

 

After doing some basic Google searches, I realised that there weren’t enough samples out there that would demonstrate WCF how-does-it-hang-together conceptually. Therefore, I decided to put together one such sample.

Hardly production / best practices compliant, the aim of this article and adjoining sample is to familiarise readers with the key classes and semantics involved in:

  • Creating WCF web services
  • Hosting WCF web services within IIS
  • Hosting WCF web services within a custom server application
  • Creating WCF web service clients

Writing the WCF Service

The two basic concepts involved in writing a WCF service are:

- Defining ServiceContract interfaces

- Defining DataContract classes

As the respective names suggest, the ServiceContract sets the behavior protocol offered by the web service and the DataContract defines the business-objects that traverse process boundaries. Together, these two become part of the WSDL exposed to the outside world. Here’s how these contracts are written:

click to enlarge

The interface is decorated with the ServiceContract attribute and the methods with the OperationContract attribute. This forms the ‘functional’ element of the service code that is eventually exposed via WSDL.

click to enlarge

In above example, the business object class, Customer, is decorated with the DataContract attribute. The data members of this class that must be available on the client-side are decorated with the DataMember attribute.

The web service would need to implement the ServiceContract interface to incorporate concrete web service functionality. All this code could be there within a single .net assembly or be segmented within separate interfaces, business objects, and implementation assemblies to foster reuse. A good reason to do that would be to enable unit testing capability, mock generation, dependency injection and so on. But, that’s a different topic.

Hosting a WCF Service within IIS

This is a rather simple process. You need to create a web project within .Net and add a reference to the assembly containing the web service implementation.

Subsequently, add a file to the web project that will become the web service endpoint. In my example, I have called this CustomerService.svc. This is a content file, which contains a single line of code as follows:

<%@ServiceHost Language=C# Debug=”true” Service=”Business.Services.TestService.CustomerService” CodeBehind =”Business.Services.TestService” %>

The Service property of the ServiceHost directive contains the fully qualified class name of the ServiceContract implementation class and the CodeBehind property contains the fully qualified assembly name.

The web.config file of the application needs to contain the web service specific declarative information. This information is required by the WCF sub-system to setup essential service details, such as, endpoint configuration,  contract specification, binding specification, x509 certificate configuration and other tuning-related parameters. Here’s the sample <system.serviceModel> section:

image

Once the service is hosted, the WSDL can be obtained by using the service URL as follows:

http://localhost/Business.Services.TestService.IISHost/CustomerService.svc?wsdl

image

 

Hosting a WCF Service within A custom server application

Within the realms of WCF, it is possible to host a web service within a custom Windows console application or a custom Windows Service. This mean that WCF services may not necessarily rely on specific web server environment. To accomplish this,  you need to use the ServiceHost class programmatically. The following listing shows how a service is started and stopped:

click to enlarge

Notice the ServiceHost constructor. The first parameter is the type of the class that implements the ServiceContract interface and the second parameter is the Uri where the service will be located. In this example, the service will listen at port 8080 of the local computer.  

The host application will require the app.config with the <system.ServiceModel> section. This is same as the web.confg file discussed in the previous section.

Once the service is hosted, the WSDL can be obtained by using the service URL as follows:

http://localhost:8080/CustomerService?wsdl

Writing the WCF web service client

This is easy like everything else. Start a new windows application / windows forms application project. If you have installed the .Net 3.0 Visual Studio plugin, you will be able to obtain the service reference to the web service by clicking the Add service reference option:

image

This will give you the ServiceContract,  DataContract and the proxy classes for the client application to use.

If would like the source code of this example, then you can download it here.

I hope this is useful to the WCF newbies.

Leave a Reply