Method Overloading in WCF

by aboo bolaky 24. November 2008 07:32

In WSDL-based operations, method overloading is hard to achieve. It's not as easy as it would seem with C++ or even C#, where an interface could be defined as

[ServiceContract]
public interface IHelloWorld
{
	[OperationContract]
	void Add(int num1 ,int num2);
	[OperationContract]
	void Add(string s1, string s2);
} 

When you implement this interface in your class, the code (if it's well written Sealed) will compile just fine. The moment you update your service reference, it all goes crazy. The Error Message is "The Request failed with the error message : Cannot have two operations in the same contract with the same name".

The resolution is to add the Name attribute to the OperationContract on each method defined in the interface.

[ServiceContract]
public interface IHelloWorld
{
	[OperationContract(Name="AddNumber") ]
	void Add(int num1 ,int num2);
	[OperationContract(Name="ConcatenateStrings")]
	void Add(string s1, string s2);
} 

When proxy is generated on the client, the operations now have aliased names.  NICE !!

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="HelloWorldRef.IHelloWorld")]
public interface IHelloWorld 
{
	[System.ServiceModel.OperationContractAttribute(
		Action="http://tempuri.org/IHelloWorld/AddNumber", 
		ReplyAction="http://tempuri.org/IHelloWorld/AddNumberResponse")]
	void AddNumber(int num1, int num2);

	[System.ServiceModel.OperationContractAttribute(
		Action="http://tempuri.org/IHelloWorld/ConcatenateStrings",
		ReplyAction="http://tempuri.org/IHelloWorld/ConcatenateStringsResponse")]
	void ConcatenateStrings(string s1, string s2);

} 

Tags:

.Net | WCF | Tips & Tricks

Tag cloud

Flash Player 9 required.

About Me

I wish I could write something here..
//TODO: ElaborateMe