Operation overloading in WCF

Source: Internet
Author: User
1. Is operation overloading valid in WCF?
Overloading means that two methods with the same name but with different parameters. e.g., the following is a valid C # interface definiton:
Interface icaculator
{
Int add (int x, int y );
Double add (Double X, Double Y );
}
However, Operation Overloading is not valid if we simplely define such following operationcontract in WCF, it will throw an invalidoperationexception.
Interface icaculator
{
Int Add (Int x, int y );
Double Add (Double X, Double Y );
// This is not allowed!
}
2 How can we overload operation in WCF manually?
However, we can manualy enable operation overloading. The trick is using the name property of operationcontract attribute to alias operation:
[Servicecontract]
Interface icaculator
{
[Operationcontract (name = "addint")]
Int Add (Int x, int y );
[Operationcontract (name = "adddouble")]
Double Add (Double X, Double Y );
}
Certificate -----------------------------------------------------------------------------------------------------------------------------------
When the client imports the contracts and generates the proxy automatically, the imported operations will have aliased names:
[Servicecontract]
Public interface icalculator
{
[Operatoncontract]
Int addint (int x, int y );
[Operatoncontract]
Double adddouble (Double X, Double Y );
}
Public partial class calculatorclient: clientbase <icaculator>, icaculator
{
Public int addint (int x, int y)
{
Return channel. addint (x, y );
}

Public double adddouble (Double X, Double Y)
{
Return channel. adddouble (x, y );
}
}
The client can use the generated proxy and contract as is, however, we can still rework those to realize operation Overloading on the client side. and the trick is the same as we did on the server side using name attribute.
We can re-code like this.
Public interface icalculator
{
[Operationcontract (name = "addint")]
Int Add (Int x, int y );
[Operationcontract (name = "adddouble")]
Double Add (Double X, Double Y );
}
Public partial class calculatorclient: clientbase <icaculator>, icaculator
{
Public int add (int x, int y)
{
Return channel. addint (x, y );
}

Public double add (Double X, Double Y)
{
Return channel. adddouble (x, y );
}
}

And then, in the client side, we can consume the services.
Calculatorclient Cal = new calculator ();
Cal. Add (1, 2); // equals 3
Cal. Add (1.1, 2.3); // equals 3.4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.