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

To fix this issue lets understand the below two scenario

Scenario 1:

Let's 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:

Let's 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 coding... Hope this helps!

Ebook Download
View all
Learn
View all