Error: Cannot have two operations in the same contract with the same name
Hi,
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")]
[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 and the other with no parameters and here the function names are also different but the
OperationContract
name is the same to both the methods as shown below. In this case 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!