WCF Programming Methods

Windows Communication Foundation supports a number of different methods, or approaches, of programming, each with its pros and cons. The great thing about WCF is that there is always more than one way to accomplish something. For example, you can specify an endpoint in code or in a configuration file.
You also do not need to stick to a specific method or approach, and in fact the best approach is to combine the methods to get the most flexibility out of your service.
This section discusses the three common methods of developing WCF services:

  • Declarative
  • Explicit
  • Configuration

Declarative

Declarative programming is accomplished via attributes. These attributes are used to define the contracts and specify the behavior of the service. They are used to specify additional parameters that change the details of the contracts and service behavior.
For example, the following code shows the beginnings of a simple service contract:

[ServiceContract]
public interface coolservice
{
    [OperationContract]
    decimal CalculateShipping(string state, int shiptype)
    {
        //do something
    }
    [OperationContract]
    decimal CalculateTax(string state, decimal bookcost)
    {
        //do something
    }
}

For declarative programming, the attributes are added as shown in the following modified code:

[ServiceContract(Session = true)]
public interface coolservice
{
    [OperationContract(IsOneWay = true)]
    decimal CalculateShipping(string state, int shiptype)
    {
        //do something
    }
    [OperationContract(IsOneWay = true)]
    decimal CalculateTax(string state, decimal bookcost)
    {
        //do something
    }
}

Explicit

Explicit programming lets you work directly with all of the many classes and interfaces provided by the WCF object model. Working directly with the object model gives you as a developer much more flexibility and control over your code, and is much more extensive than that of attributes (declarative programming) or using configuration files. This is because you as a developer can use your object-oriented programming knowledge in a language you are already familiar with to take advantage of the rich WCF object model.
The following code, used earlier in this chapter, illustrates a simple example of how to create and start a service using the object model:

class TestWCFApp
{
    static void Main()
    {
        Uri baseAddress = new Uri(ConfigurationManager.AppSettings["address"]);
        AddressHeader ah = AddressHeader.CreateAddressHeader("service1", "http: //localhost:8080/service");
        EndpointAddress ea = new EndpointAddress(new Uri("http: //localhost:8081/testservice/service"), ah);
        ServiceEndpoint se = new ServiceEndpoint(ContractDescription.GetContract(typeof (BookOrderService)), new WSHttpBinding), ea);
        ServiceHost sh new ServiceHost(typeof (BookOrderService), baseAddress);
        sh.Description.Endpoints.Add(se);
        sh.Open(); sh.close();
    }
}

Configuration

Just like declarative programming, there are many things that you can specify regarding the behavior of a service via the configuration file of the service. The nice thing about using “configuration-based” programming is that any changes do not necessitate a recompile of the service.
The following example uses a configuration file to define a service endpoint with an address, binding, and contract:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service type="MyTestService">
        <endpoint address="http://localhost:8080/MyTestService/" bindingConfiguration=”usingDefaults” binding=”MyTestBinding” contract=”MyTestService”>
        </endpoint>
      </service>
    </services>
  </system.serviceModel>
</configuration>
Tagged . Bookmark the permalink.

Leave a Reply