Operation Overloading

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

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

However, operation overloading is invalid in the world of WSDL-based operations, since all operations must have unique names (they are identified by name in the messages). 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]
   int Add(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 each overloaded operation, 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 the aliased names:

[ServiceContract]
interface ICalculator
{
   [OperationContract]
   int AddInt(int arg1,int arg2);

   [OperationContract]
   double AddDouble(double arg1,double arg2);
}
class CalculatorClient : ClientBase<ICalculator>,ICalculator
{
   public int AddInt(int arg1,int arg2)
   {
      return Channel.AddInt(arg1,arg2);
   }
   public double AddDouble(double arg1,double arg2)
   {
      return Channel.AddDouble(arg1,arg2);
   }
   //Rest of the proxy
}

The client can use the generated proxy and contract as they are, but you can also rework them to provide overloading on the client side. Rename the methods on the imported contract and the proxy to the overloaded names, and make sure the proxy class makes calls on the internal proxy using the overloaded methods, as in:

public int Add(int arg1,int arg2)
{
   return Channel.Add(arg1,arg2);
}

Finally, use the Name property on the imported contract on the client side to alias and overload the methods, matching the imported operation names, as shown in Example 2-2.

Example 2-2. Client-side operation overloading

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

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

class CalculatorClient : ClientBase<ICalculator>,ICalculator
{
   public int Add(int arg1,int arg2)
   {
      return Channel.Add(arg1,arg2);
   }
   public double Add(double arg1,double arg2)
   {
      return Channel.Add(arg1,arg2);
   }
   //Rest of the proxy
}

Now the client can benefit from the readable and smooth programming model offered by overloaded operations:

CalculatorClient proxy = new CalculatorClient(  );

int result1 = proxy.Add(1,2);
double result2 = proxy.Add(1.0,2.0);

proxy.Close(  );
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.99