WCF class implementing two operation contracts in different service contracts with same name
Asked Answered
B

2

8

I have declared two service contracts as follows:

[ServiceContract]
public interface IContract1
{
    [OperationContract]
    double Add(int ip);
}

[ServiceContract]
public interface IContract2
{
    [OperationContract]
    double Add(double ip);
}

I have a class which implements these two contracts. I have created two endpoints for both contracts. But I'm not able to access the service from client code. It displays a big error when I try to update the service reference as:

Metadata contains an error that cannot be resolved.... There was no endpoint listening at ... , etc.

I know that you can't have two OperationContracts with the same name but is it possible to have two operation contracts in different service contracts with same name but different signature?

Blackcock answered 12/5, 2010 at 9:30 Comment(0)
D
14

If one service implements both contracts then you should give unique names to your operation contracts.

[ServiceContract]
public interface IContract1
{
    [OperationContract(Name="AddInt")]
    double Add(int ip);
}

[ServiceContract]
public interface IContract2
{
    [OperationContract(Name="AddDouble")]
    double Add(double ip);
}
Descendible answered 12/5, 2010 at 9:34 Comment(2)
how shall I consume the method then? It just shows only one method in client code.Blackcock
Got that, I will create two clients corresponding to two service contracts. Thanks.Blackcock
S
6

You can use the following.

[ServiceContract]
public interface IContract1
{
    [OperationContract(Name = "Add1")]
    double Add(int ip);
}

[ServiceContract]
public interface IContract2
{
    [OperationContract(Name = "Add2")]
    double Add(double ip);
}
Spangler answered 12/5, 2010 at 9:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.