Click here to Skip to main content
15,887,267 members
Articles / WCF
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
22 Nov 2011CPOL 21.9K   1  
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”
C#
[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.
C#
[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
C#
[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
C#
[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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
-- There are no messages in this forum --