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


Error: Cannot have two operations in the same contract with the same name. This issue comes into picture when you try to overload methods in WCF service & also when you define OperationContract NAME attributes are replicated across a service. Find solution for Error: Cannot have two operations in the same contract with the same name

Learn on Error: Cannot have two operations in the same contract with the same name


Scenario 1:
Lets say I have an Interface as IMyService which defines two methods as PrintString()
In the below example, 1st method takes parameter as string & the 2nd method takes no parameter.
So in this it would generate error as "Cannot have two operations …. etc"

[ServiceContract]
public interface IMyService
{
[OperationContract]
[WebGet(UriTemplate = "PrintString/strValue/{s}")]
string PrintString(string s);

[OperationContract]
[WebGet(UriTemplate = "PrintString")]
string PrintString();


}

So to fix this we need to define names to operationcontract i.e.
[OperationContract(Name = "PrintString1?)] as follows

  
[ServiceContract]
public interface IMyService
{
[OperationContract(Name = "PrintString1?)]
[WebGet(UriTemplate = "PrintString/strValue/{s}")]
string PrintString(string s);

[OperationContract(Name = "PrintString2?)]
[WebGet(UriTemplate = "PrintString")]
string PrintString();
}



Scenario 2:
Lets say I have an Interface as IMyService which defines two methods (PrintFirstName(string s), PrintLastName()) one with Single parameter & other with no parameters & here the function names are also different but the OperationContract name is same to both the methods as shown below. Incase also we will get to see the "Cannot have two opetations …. etc"

In this case both the methods are defined with same name (PrintName) for OperationContract as

[ServiceContract]
public interface IMyService
{


[OperationContract(Name = "PrintName")]
[WebGet(UriTemplate = "PrintFirstName/strValue/{s}")]
string PrintFirstName(string s);

[OperationContract(Name = "PrintName")]
[WebGet(UriTemplate = "PrintLastName")]
string PrintLastName();

}

By this time I hope you have figured it out on How to fix this.
Yes you are right by changing OperationContract name as follows


[ServiceContract]
public interface IMyService
{


[OperationContract(Name = "PrintFirstName")]
[WebGet(UriTemplate = "PrintFirstName/strValue/{s}")]
string PrintFirstName(string s);

[OperationContract(Name = "PrintLastName")]
[WebGet(UriTemplate = "PrintLastName")]
string PrintLastName();


}


Happy Kooding… Hope this helps!


Comments

Author: Reazur Rashid10 Nov 2011 Member Level: Bronze   Points : 0

Wow !! That is superb .... really nice. Thanks Mr. Prokash.

Guest Author: Sagar28 May 2013

That's way more than I was expecting. Thanks!

Author: Umesh Bhosale26 Mar 2014 Member Level: Silver   Points : 0

hello
You can Provide
[operationContract("name=Test")]

for overloading operation

Thanks
Umesh Bhosale



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: