WCF – type of contracts.

Contracts are the way of describing WCF service behaviour.

  • 1. Service Contract

    Service contract describes what operations can be called by the outside world. It is basically the attribute for the interface.

    [ServiceContract]
    public interface IGameService
    {
    }
    
  • In addition, let’s go through the most important properties:
  • CallbackContract - Callback contracts are used in duplex message channel where client and server can call each other.
  • ConfigurationName - by using configuration name, you can set the service name in service config file.
    [ServiceContract(ConfigurationName = "GameService")]
    public interface IGameService
    {
        [OperationContract]
        void StartGame(int value);
    
        [OperationContract]
        void EndGame();
    
    }
    

    In config file

    <service name="GameService">
        <endpoint
          address="http://localhost:1212/GameService" binding="basicHttpBinding"
          bindingConfiguration="myBindingConfiguration" contract="IGameService">
    </service>
    
  • ProtectionLevel - allows you set protection for all messages. Options are:
    • “None” – nothing will be protected.
    • “Sign” – messages will be signed digitally before sending.
    • “EncryptAndSign” – encrypt message before digitally signed and send
  • SessionMode – It allows you to decide how session should work for your service. Only bindings with WS-*, NetTcpBinding and NetNamedPipeBinding allows sessions. There are three ways to handle sessions:
    • “Allowed” – session is allowed, but not mandatory.
    • “Required” – service can be connected only through valid sessions.
    • “NotAllowed” – service can be connected only through session less requests.
  • 2. Operation Contract

    Operation contract define within the service contract. It is attribute that marks method of an interface. This method can be invoked by the client.

    [ServiceContract]
    public interface IGameService
    {
        [OperationContract]
        void StartGame(int value);
    
        [OperationContract]
        void EndGame();
    
    }
    

    Operation contract can have different properties, let’s talk about some of them:

    • Action and ReplyAction – the default value for Action is concatenation of ServiceNameSpace + ServiceContractName + OperationName and for ReplyAction ServiceNameSpace + ServiceContractName + OperationName + Response. Action header indicates the URI of Operation to be invoked and defines WS-Addressing action. WS-Addressing allows you transport neutral communication between client and service and it is very much useful in situation where SOAP response is not enough and cannot be provided immediately. So you will have to introduce intermediate router service between client and target service which will receive message and route to different services for additional activities.
      [ServiceContract(Namespace = "http://orders.northwind.com")]
      public interface IOrders
      {
          [OperationContract(
            Action = "http://orders.northwind.com/placeorders",
            Name = "PlaceOrders",
            ReplyAction = "http://orders.northwind.com/reorderqty"
          )]
          Message PlaceOrders(Message msg);
      }
      
    • AsyncPattern - AsyncPattern is a Boolean property which is diabled by default(set to false).It can be set to TRUE in order to handle long running processes like I/O calls. For creating wcf asynchronous operations use the method name conventions as BeginMethodName and EndMethodName. The BeginMethodName must be marked as [OperationContract(AsyncPattern=true)].
    • IsInitiating - property that signalize that the service operation marked with TRUE must be called before any other operation from client side. This operation will create new session for client. In order to use this ServiceContract should be marking with SessionMode=SessionMode.Required or SessionMode=SessionMode.Allowed properties. This properties can be used where Service Contract needs to validate the client details before executing any operation. Only bindings with WS-*, NetTcpBinding and NetNamedPipeBinding allows sessions.
    • IsOneWay - by defualt set to FALSE. IsOneWay operations has short term memory loss, it just fired and forget without bothering about response or execution of operation. You cannot use FaultException, TransactionContext with OneWay operations.
  • 3. Data Contract

    Data contract describes the data type of the information that will be send between client and service. Data contract can be used by operation contract as a parameter or return type or it also can be used by a message contract.

    [DataContract]
    public class Player
    {
        [DataMember]
        public int Id { get; set; }
    
        [DataMember]
        public string Name { get; set; }
    }
    
  • 4. Message Contract

    It used in specific cases when you need to control the layout of your SOAP messages. In 90% cases you will use DataContract.

  • A message contract allows you to specifically say which elements (scalar types or compound types asĀ DataContracts) will be in the SOAP header, and which will be in the SOAP body.
    • When using Message contract type as parameter, Only one parameter can be used in Operation.
    • Service operation either should return MessageContract type or it should not return any value.
    • Operation will accept and return only message contract type. Other data types are not allowed.
    [MessageContract]
    public class GameMessage
    {
        [MessageHeader]
        public GameHeader Header { get; set; }
        [MessageBodyMember]
        public Player Player { get; set; }
    }
    [DataContract]
    public class GameHeader
    {
        [DataMember]
        public string UserId { get; set; }
    }
    
  • 5. Fault Contract

A fault contract defines errors raised by service. This helps us to easy identify what error has occurred and where.

Share this post:Tweet about this on TwitterShare on Facebook0Share on LinkedIn0Share on Google+0Share on Reddit0Email this to someoneDigg this