Chapter 2. Service Contracts

The ServiceContract attribute presented in the previous chapter exposes a programming construct (the interface) as a service-oriented contract, allowing you to program in languages such as C#, while exposing the construct as WCF contracts and services. This chapter starts by discussing how to better bridge the gap between the two programming models by enabling operation overloading and contract inheritance. Next, it presents a few simple yet powerful service contract design and factoring guidelines and techniques. The chapter ends by showing you how to interact programmatically at runtime with the metadata of the exposed contracts.

Operation Overloading

Programming languages such as C++ and C# support method overloading; that is, defining two methods with the same name but with different parameters. For example, this is a valid C# interface definition:

interface ICalculator
{
   int Add(int arg1,int arg2);
   double Add(double arg1,double arg2);
}

However, operation overloading is invalid in the world of WSDL-based operations, since all operations must have unique names (they are identified by name in the messages). Consequently, while the following contract definition compiles, it will throw an InvalidOperationException at the service host load time:

//Invalid contract definition:
[ServiceContract]
interface ICalculator
{
   [OperationContract]
   int Add(int arg1,int arg2);
   [OperationContract]
   double Add(double arg1,double arg2);
}

However, you can manually ...

Get Programming WCF Services, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.