Operation Overloading in WCF


Objective:

This article will explain,

  1. What is Service Contract?
  2. How to achieve Operation overloading in Service.
  3. How to achieve operation overloading at client side.
  4. It explained the entire concept with supporting code as well.
  5. This will explain in detail of all the aspect of method overloading and manually configuration at client side.

    What is Service Contract?

    image1.gif

    ServiceContractAttribute class is defined as

    image2.gif
     
    1. It is defined under namespace System.ServiceModel.
    2. It got 6 properties.
    3. It could be applied either on service contract interface or service contract class.
       
    Could we achieve Operation overloading in Service Contract?

    image3.gif

    How to achieve operation overloading?

    By using Name property of OperationContractAttribute class.

    image4.gif

    image5.gif

    OperationContractAttribute class

    Code Sample
     
    1. Service is having 3 overloaded operations.
    2. Add operation is taking different parameters.
    3. Name properties are being used to achieve overload operation.
    4. We will give aliased name to achieve overload using Name properties of OperationContractAttribute class.

      image6.gif
       
    Service Contract (IAddCalculator.cs)

    image7.gif

    Service Implementation (AddCalculator.cs)

    image8.gif

    When the client imports the contract and generates the proxy, the imported operations will have the aliased name.

    At client side, the below methods will be imported.

    image9.gif

    The client can use the generated proxy at it is. Sample code is given below.

    image10.gif

    Output

    image11.gif

    Few problems in above approach

    So, up to here, we have achieved Operation Overloading. But there is one problem here that client side is importing alias name rather than the same name for all overloaded operation. By below code , we could see that client proxy is importing alias name.


    image12.gif
    image12.1.gif

    Solution of above problem
     
    1. Click on Object browser at client side. Click on ServiceReference1.

      image13.gif
       
    2. Click on AddCalculatorClient at object browser. Double click to open the code browser.

      image14.gif
       
    3. In AddCalculatorClient partial class, modify the imported operation contracts.

      image16.gif

    Just modify the code as,

    image16.gif


    And modify the interface at client side as

    image17.gif

    Save and compile the code.

    Now at the client side, we could see that Add method is overloaded with 3 different parameters.


    image18.gif

    So, now we could modify the client code as,

    image19.gif


    Output

    image20.gif

    Conclusion:

    This article explained,
     

  6. What is Service Contract?
  7. How to achieve Operation overloading in Service.
  8. How to achieve operation overloading at client side.
  9. It explained the entire concept with supporting code as well.

Up Next
    Ebook Download
    View all
    Learn
    View all