Pages

Windows Communication Foundation (part one)

Windows Communication Foundation (WCF) is a part of the .NET platform that allows service-oriented applications to communicate across the web. Here's a link to the Microsoft whitepaper. In it, Microsoft describes the problems that WCF addresses and provides implementation details. It's the foundation for many modern applications and I'm going to be working with it today. I'll be creating a calculator service that will perform some simple arithmetic for clients sending in data.

Every WCF application has three primary components:

  • A service class that implements one or more methods
  • A host process in which the service runs
  • One ore more endpoints that allow the client access to the service. All communication flows through the service endpoints.
Step One: Define a WCF Service Contract Contracts are created by defining them in an interface. Each method of this interface corresponds to a service operation. Each interface must have a ServiceContractAttribute applied to it and each operation requires a OperationContractAttribute. Here is the interface I have created for my contract:
Notice how the ServiceContractAttribute is defined for the interface, and how each operation's method signature is preceded by an OperationContractAttribute. If these are not both defined, the method is not exposed. Step Two: Implement the WCF Service Contract Now that we have our interface, we need to implement it in a service class. In my project, I've created a CalculatorService class that will implement ICalculator from step 1. Here is how CalculatorService implements each method:
The next step will be how to properly host the service we have created. This project is continued in part two. Here's a link to the completed application:

No comments:

Post a Comment