Contract Inheritance

Service contract interfaces can derive from each other, enabling you to define a hierarchy of contracts. However, the ServiceContract attribute is not inheritable:

[AttributeUsage(Inherited = false,...)]
public sealed class ServiceContractAttribute : Attribute

Consequently, every level in the interface hierarchy must explicitly have the ServiceContract attribute, as shown in Example 2-3.

Example 2-3. Service-side contract hierarchy

interface ISimpleCalculator
   int Add(int arg1,int arg2);
interface IScientificCalculator : ISimpleCalculator
   int Multiply(int arg1,int arg2);

When it comes to implementing a contract hierarchy, a single service class can implement the entire hierarchy, just as with classic C# programming:

class MyCalculator : IScientificCalculator
   public int Add(int arg1,int arg2)
      return arg1 + arg2;
   public int Multiply(int arg1,int arg2)
      return arg1 * arg2;

The host can expose a single endpoint for the bottom most interface in the hierarchy:

<service name = "MyCalculator">
      address  = "http://localhost:8001/MyCalculator/"
      binding  = "basicHttpBinding"
      contract = "IScientificCalculator"

Client-Side Contract Hierarchy

When the client imports the metadata of a service endpoint whose contract is part of an interface hierarchy, the resulting contract on the client side does not maintain the original hierarchy. Instead it will include a flattened ...

Get Programming WCF Services now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.