Skip to Main Content
Programming WCF Services
book

Programming WCF Services

by Juval Lowy
February 2007
Intermediate to advanced content levelIntermediate to advanced
634 pages
16h 1m
English
O'Reilly Media, Inc.
Content preview from Programming WCF Services

Operation Overloading

Programming languages such as C++ and C# support method overloading: defining two methods with the same name but with different parameters. For example, this is a valid C# interface definition:

interface ICalculator
{
   intAdd(int arg1,int arg2);
   double Add(double arg1,double arg2);
}

However, operation overloading is invalid in the world of WSDL-based operations. Consequently, while the following contract definition compiles, it will throw an InvalidOperationException at the service host load time:

//Invalid contract definition:
[ServiceContract]
interface ICalculator
{
   [OperationContract]
   intAdd(int arg1,int arg2);

   [OperationContract]
   double Add(double arg1,double arg2);
}

However, you can manually enable operation overloading. The trick is using the Name property of the OperationContract attribute to alias the operation:

[AttributeUsage(AttributeTargets.Method)]
public sealed class OperationContractAttribute : Attribute
{
   public string Name
   {get;set;}
   //More members
}

You need to alias the operation both on the service and on the client side. On the service side, provide a unique name for the overloaded operations, as shown in Example 2-1.

Example 2-1. Service-side operation overloading

[ServiceContract]
interface ICalculator
{
   [OperationContract(Name = "AddInt")]
   int Add(int arg1,int arg2);

   [OperationContract(Name = "AddDouble")]
   double Add(double arg1,double arg2);
}

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming WCF Services, 3rd Edition

Programming WCF Services, 3rd Edition

Juval Lowy
Mastering ASP.NET Web API

Mastering ASP.NET Web API

Mithun Pattankar, Malendra Hurbuns

Publisher Resources

ISBN: 0596526997Supplemental ContentErrata Page