WCF Service Contract Attributes

This article will give you a brief description of WCF service contract attributes like namespace, callbackcontract, protectionlevel, configuration name, session mode. This will help you to design a WCF service contracts in a more efficient way. WCF service contract is included in System.Servicemodel namespace.

Service Contracts are behavioral contracts as defining the behavior of service. You will have to include the reference to System.Servicemodel and use [ServiceContract] with interface or class. It is a good practice to create a service contract with the interface as the interface creates a contract model for the client.

Service contract has below attributes:

  1. Name

    This is a string property of ServiceContract that allows you to set the name of the service contract. The default name of your service is your class or interface name. If you are using [ServiceContract] with the interface the prefix "I" also will include your service name. So It is always better to use some meaningful name for your service. The WSDL will include your service name as PortType.

                
        [ServiceContract(Name="Product")]
        public interface IProductService
        {
            
        }
                
                
  2. CallbackContract

    Callback contracts of ServiceContract are used in the duplex message channel where clients and servers can call each other. (Read more on WCF MEP ) In the duplex model client send a request to the server it might be time consuming operation and then the service needs to provide notification to the client through callback contracts. If you use callbackcontract you will lose the service interoperability.

    Also, you need to consider that not all bindings support callback contracts like the bindings which depend on HTTP protocols. HTTP protocols are stateless whereas the duplex channel requires a live connection to complete the operation.

                
        [ServiceContract]
        interface IOrdersCallback
        {
            [OperationContract(IsOneWay = true)]
            void OnOrderPlaced(string message, DateTime timestamp);
        }
    
        [ServiceContract(CallbackContract = typeof(IOrdersCallback))]
        public interface IOrders
        {
            [OperationContract]
            bool OrderPlace(Order order);
        }  
                
  3. ConfigurationName

    By using ConfigurationName property of ServiceContract you can set the service name in the service config file. It specifies the name of the config element for service.

                
        [ServiceContract(Name="Orders", ConfigurationName="OrderService")]
        public interface IOrders
        {   
            [OperationContract]
            void PlaceOrder(Order order);
        }
                
    In the config file
                
       <service name="OrderService">
       <endpoint
          address="http://localhost:8080/OrderService" binding="basicHttpBinding"
          bindingConfiguration="myBindingConfiguration" contract="IOrders">
       </service>
                        
                
  4. HasProtectionLevel

    It is a read only boolean property of ServiceContract. If the ProtectionLevel property is set to other than "None", HasProtectionLevel property will return true else return false.

  5. ProtectionLevel

    ProtectionLevel of ServiceContract allows you to define the protection level for all messages on the wire. There are three different options for ProtectionLevel enum available.

    • None: No protection will be applied to messages.
    • Sign: Messages will be signed digitally before sending.
    • EncryptAndSign: Encrypt message before digitally signed and send.
                
        [ServiceContract(ProtectionLevel = ProtectionLevel.Sign)]
        public interface IOrderService
        {
            [OperationContract()]
            bool PlaceOrder();
        }
                

    Whatever value you set for ServiceContract.ProtectionLevel same value will be applied to all OperationContract and FaultContract . If you explicitly set different values to operation contract an exception will be thrown.

  6. Namespace

    If you are having multiple services with similar method signatures adding a unique namespace value to the service contract will help you as your methods will become unique by using this feature. This property is very important while serialization and deserialization as you might have similar class names on the client-side and service side.

                
        [ServiceContract(Namespace="http://northwind.com/orderservice") ]
        public interface IOrderService
        {
          [OperationContract(Name="GetOrder")]
          Order GetOrder(int orderID);
        }
                

    You must set the same value to bindingNamespace in app.config for service.

  7. SessionMode

    This allows you to decide how the session should work for your service. Only bindings with WS-*, NetTcpBinding, and NetNamedPipeBinding allows sessions. There are three ways to handle sessions.

    • Allowed: Request with session is allowed but not mandatory.
    • Required: Service can be connected only through valid sessions.
    • NotAllowed: Service can be connected only through sessionless requests.
                
       [ServiceContract(SessionMode = SessionMode.Allowed)]
       public interface IOrders 
       {
          
       }
                

Speak your mind
Please login to post your comment!


  • geeksarray user
    06/29/2015 01:48 AM Sanjay

    good to understand wcf service contract attribute

Blog Search





If you like my content please feel free to buy me coffee. Buy Me A Coffee