Service Contracts

  1. Always apply the ServiceContractAttribute on an interface, not a class:

    //Avoid:
    [ServiceContract]class MyService
    {
       [OperationContract]
       public void MyMethod( )
       {...}
    }
    //Correct:
    [ServiceContract]
    interface IMyContract
    {
       [OperationContract]
       void MyMethod( );
    }
    class MyService : IMyContract
    {
       public void MyMethod( )
       {...}
    }
  2. Prefix the service contract name with I:

    [ServiceContract]
    interfaceIMyContract
    {...}
  3. Avoid property-like operations:

    //Avoid:
    [ServiceContract]
    interface IMyContract
    {
       [OperationContract]
       string GetName( );
    
       [OperationContract]
       void SetName(string name);
    }
  4. Avoid contracts with one member.

  5. Strive to have three to five members per service contract.

  6. Do not have more than 20 members per service contract. Twelve is probably the practical limit.

Get Programming WCF Services 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.