Cannot have two operations in the same contract with the same name

Posted: January 12, 2011 in Categories, WCF
Tags:

Exception – Cannot have two operations in the same contract with the same name, methods GetDataForTeacher and GetDataForStudent in type WcfSample.IStock violate this rule. You can change the name of one of the operations by changing the method name or by using the Name property of OperationContractAttribute

When I got this exception while using same Name,was wondering what is the role of Name parameter,Methods are implemented with two different names & why is it throwing this exception for the below contract.

[ServiceContract]
public interface IStock
{

[OperationContract(Name ="GetData")]
string GetDataForTeacher(int value);
[OperationContract(Name ="GetData")]
string GetDataForStudent(int value);

}

First lesson learnt,do not use anything by simply copy pasting :)Understand the purpose & if it is required then only use it.

Now let me come to the point what does this Name property do?Name has to be specified when you need to override the element name in WSDL.ie to override the name of methods GetDataForTeacher & GetDataForStudent in wsdl.When you specify Name,now what has happend is it reads as two methods with name GetData.Ideally,Name should be specified only in cases where you have the same implementing method names(overloading scenario) as shown below.

[ServiceContract]
public interface IStock
{

[OperationContract(Name ="GetDataForTeacher")]
string GetData(int value);
[OperationContract(Name ="GetDataForStudent")]
string GetData(string value);

}

Advertisements
Comments
  1. SrinivasP says:

    Thanks

  2. sandippatil1982 says:

    Thanks, it is working fine in my code too.

  3. Naresh says:

    Thank you for the information 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s