20

What is a WCF OperationContract? I dont really understand what it does

burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
Obsivus
  • 8,231
  • 13
  • 52
  • 97

2 Answers2

43

WCF uses an opt-in model to define what belongs to one of its contracts. In a service contract interface, only methods decorated with [OperationContract] are exposed to the client. That means that, in the interface below, if used within a WCF service, a client could call both Add and Subtract operations, but not Multiply.

[ServiceContract]
public interface ICalculator
{
    [OperationContract]
    int Add(int x, int y);

    [OperationContract]
    int Subtract(int x, int y);

    // Not decorated
    int Multiply(int x, int y);
}
carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
  • 2
    Well, Like I need all methods to be accessed by client, and I don't want to write that on every method, what do I do there? – Waqas Sep 15 '14 at 08:57
  • See https://stackoverflow.com/questions/18721554/how-to-apply-operationcontract-to-all-methods-in-interface – hmiedema9 Nov 02 '18 at 17:20
6

Every method you want to be able to the user calling from his client side must be declared like that.

Bruno Willian
  • 160
  • 2
  • 5
  • 18