WCF Operation Contract Attributes

This article will give you a brief description of WCF operation contract attributes. This will help you to design a WCF operation contract in a 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 a read/write string property of OperationContract. You can get or set a different and meaningful name for 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 the <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 to transport neutral communication between client and service and it is very much useful in situations where SOAP response is not enough and cannot be provided immediately. So you will have to introduce an intermediate router service between client and target service which will receive messages 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 of OperationContract with a default value of false. You can mark it as true if your service operation should execute asynchronously on a client or server or both. You should create 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 of OperationContract with the default value of false. If the 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 of OperationContract with the 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 a new session for clients. For use of this, the ServiceContract should support sessions by marking SessionMode=SessionMode.Required or SessionMode=SessionMode.Allowed. This property can be used where the Service Contract needs to validate the client details before executing any operation.

    From the below example client must call ValidateClient before any other operation call. If any other operation gets called before the ValidateClient service returns ActionNotSupported FaultException. Operation PlaceOrder can be called 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 of OperationContract with the default value of false. IsTerminating is used to close the client's current session. In the previous example, once the operation CloseChannel called no other calls can be made to service except ValidateClient.

  7. IsOneWay

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

    Sometimes operations 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. For more information on bindings visit various types of WCF bindings. If the value of IsOneWay is false and the 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 of OperationContract 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 messages will flow on the wire as plain text.
    • Sign: Messages will be digitally signed before sending.
    • EncryptAndSign: Messages will be encrypted before the sign and send.

    Not all WCF Bindings like BasicHttpBinding 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
Please login to post your comment!


Blog Search





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