WCF Service Contract Attributes

This article will give you brief description about wcf service contract attributes like namespace, callbackcontract, protectionlevel, configuration name, session mode. This will help you to design wcf service contract in more efficient way. WCF service contract is included in System.Servicemodel namespace. Service Contracts are behavioral contracts as defines 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 service contract with interface as interface creates contract model for client.


Service contract has below attributes:


  1. Name

    This is a string property which 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 to 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 are used in duplex message channel where client and server can call each other. (Read more on WCF MEP) In duplex model client send request to server it might be time consuming operation and then service needs to provide notification to client through callback contracts. If you use callbackcontract you will loose the service interoperability. Also you need to consider that not all bindings support callback contracts like the bindings which are depend on HTTP protocols. HTTP protocols are stateless whereas duplex channel require 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);
        }  
                

    See how to implement callback contracts

  3. ConfigurationName

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

                    
        [ServiceContract(Name="Orders", ConfigurationName="OrderService")]
        public interface IOrders
        {   
            [OperationContract]
            void PlaceOrder(Order order);
        }
                
    In 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. If the ProtectionLevel property is set to other than "None", HasProtectionLevel property will return true else return false.

  5. ProtectionLevel

    It allows you to define protection level for all messages on wire. There are three different options of 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 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 at 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

    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 : 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 :
Leave a comment for this article on dotnetbloogers.com
User profile picture on dotnetmentors.com

by Sanjay Verma at 6/29/2015 1:48:00 AM
good to understand wcf service contract attribute
post comment on dotnetmentors.com