WCF Operation Contract Attributes

This article will give you brief description about wcf operation contract attributes. This will help you to design wcf operation contract in more effective way. WCF operation contract is included in System.Servicemodel namespace. You will have to include the reference to System.Servicemodel and use [OperationContract] with methods. Methods marked as [OperationContract] will be exposed to clients for invoking. It is similar to Web methods of asmx web services.


Operation contract has below attributes:


  1. Name

    It is read/write string property. You can get or set the different and meaningful name to your operations using this property. The default value for this property would be your method name. The value of this property will be used in wsdl for <operation> element.

                        
        [ServiceContract()]
        public interface IOrders
        {
            [OperationContract(Name="PlaceOrder")]
            bool OrderPlace(Order order);
        }
                    
  2. Action and ReplyAction

    Action and ReplyAction properties are co related. 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.

    You can specify the value for Action and ReplyAction as "*" (an asterisk) to handle all unhandled URI.

    Create new WCF service library and test using WCFTestClient

                        
        [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);
        }           
                    
  3. AsyncPattern

    AsyncPattern is a Boolean property with default value of false. You can mark it as true if your service operation should execute asynchronously on client or server or both. You should create the asynchronous operations 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)].

                        
        [ServiceContract]
        public interface IOrders
        {
            [OperationContract]
            int TodaysOrderCount(DateTime dt);
    
            [OperationContract(AsyncPattern = true)]
            IAsyncResult BeginPlaceOrder(Order order, AsyncCallback cb, Object state);
    
            bool EndPlaceOrder(IAsyncResult ar);
        }
                    
  4. HasProtectionLevel

    HasProtectionLevel is a read only Boolean property with default value of false. If value of ProtectionLevel is set to "None" it will return false and if ProtectionLevel is set to Sign or EncryptAndSign it will return true.


  5. IsInitiating

    IsInitiating is a Boolean property with default value of false. The service operation marked with IsInitiating = true must be called before any other operation from client side. This operation will create new session for client. For use of this the ServiceContract should support sessions by marking SessionMode=SessionMode.Required or SessionMode=SessionMode.Allowed. This property can be used where Service Contract needs to validate the client details before executing any operation.

    From below example client must call ValidateClient before any other operation call. If any other operation get called before ValidateClient service returns ActionNotSupported FaultException. Operation PlaceOrder can be call many times after calling ValidateClient and before CloseChannel.

                        
        [ServiceContract(SessionMode=SessionMode.Required)]
        public class OrderService
        {
          [OperationContract(
            IsOneWay=true,
            IsInitiating=true,
            IsTerminating=false
          )]
          public void ValidateClient()
          {
            return true;
          }
    
          [OperationContract(
            IsInitiating=false,
            IsTerminating=false
          )]
          public bool PlaceOrder(Order order)
          {
            // Place order code
            return true;
          }
    
          [OperationContract(
            IsOneWay=true,
            IsInitiating=false,
            IsTerminating=true
          )]
          public void CloseChannel()
          {
             //code to  end session for current client
          }
        }
                  
  6. IsTerminating

    This is a Boolean property with default value of false. IsTerminating is used to close client's current session. In previous example once operation CloseChannel called no other calls can be made to service except ValidateClient.

  7. IsOneWay

    IsOneWay is Boolean property with default value of false. For making operation as One way you need to set IsOneWay = true. IsOneWay operations has short term memory loss, it just fired and forget without bothering about response or execution of operation.

    Sometime operation just need to execute without expecting the response like marking the order status. You cannot use FaultException, TransactionContext with OneWay operations. If you are using MSMQ bindings One Way operations are very much useful. If the value of IsOneWay is false and operation's return type is void then empty SOAP body return.

                        
        [ServiceContract]
        public interface IOrders
        {
          [OperationContract(IsOneWay=true)]
          public void ChangeOrderStatus(int orderID, OrderStatus orderStatus);
        }
                    
  8. Name

    Name is string property with default value of Operation Name. It allows you to give operation name with more meaningful value than the default method name. This value will be generated in WSDL for <Operation> element.

                        
        [ServiceContract]
        public interface IOrders
        {
            [OperationContract(Name="GetOrderReport")]
            String OrderReport(DateTime dt);
        }
                
  9. ProtectionLevel

    The default value for ProtectionLevel is None or the value which is set at ServiceContract level. It defines the Protection Level of operation and how service messages will be protected. The value for ProtectionLevel can be set as

    • None : No protection will be applied to messages and message will flow on wire as plain text.
    • Sign : Messages will be digitally signed before sending.
    • EncryptAndSign : Messages will be encrypted before sign and sending.

    Not all wcf Bindings like BasicHttpBindins supports ProtectionLevel. For using ProtectionLevel property you must use the binding which supports protection. Bindings security mode should be set to Message, if it is set to None or Transport the operation level setting of protection level does not have any effect.

        
        [ServiceContract()]
        public interface IOrders
        {
            [OperationContract(ProtectionLevel = ProtectionLevel.Sign)]
            int GetHighValueOrdersCount(DateTime dt);
        }
                    

Speak your mind :
Leave a comment for this article on dotnetbloogers.com