Possible Duplicate:
why you cant overload a method in WCF?
I am working on one project in which I use the WCF services. My problem is that in WCF service I am having one method named Display()
which is used by me client1.
Now I want to add another method with the same name but with one parameter, ie. Display(string name)
, so that new clinet2 can use the new method and old client1 can use the old method. how can I achieve this? Here is the code which I have written.
namespace ContractVersioningService
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string Display();
[OperationContract]
string GoodNight();
}
}
namespace ContractVersioningService
{
public class Service1 : IService1
{
public string Display()
{
return "Good Morning";
}
public string GoodNight()
{
return "Good Night";
}
}
}
namespace ContractVersioningService
{
[ServiceContract(Namespace = "ContractVersioningService/01", Name = "ServiceVersioning")]
public interface IService2 : IService1
{
[OperationContract]
string Disp(string greet);
}
}
namespace ContractVersioningService
{
public class Service2 : Service1, IService2
{
public string Display(string name)
{
return name;
}
public string Disp(string s)
{
return s;
}
}
}
Why WCF doesnot support method overloading directly ?
Because WSDL doesnot support method overloading(not OOPs). WCF generates WSDL which specifies the location of the service and the operation or methods the service exposes.
WCF use Document/Literal WSDL Style : Microsoft proposed this standard where the soap body element will contain the web method name.
By default all the WCF services conform to the document literal standard where the soap body should include the method name.
The only way is using Name attribute. For eg,
[OperationContract(Name="Integers")]
int Display(int a,int b)
[OperationContract(Name="Doubles")]
double Display(double a,double b)
The the compiler will generate the following, which makes sense for wsdl to locate
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName=
"ServiceRef.IService1")]
public interface IService1
{
[System.ServiceModel.OperationContractAttribute(
Action="http://tempuri.org/Service1/AddNumber",
ReplyAction="http://tempuri.org/IHelloWorld/IntegersResponse")]
int Display(int a,int b)
[System.ServiceModel.OperationContractAttribute(
Action="http://tempuri.org/IHelloWorld/ConcatenateStrings",
ReplyAction="http://tempuri.org/Service1/DoublesResponse")]
double Display(double a,double b)
}
OK, I'm going to make this an answer, since the comments get overly excessive by now.
You basically have two options:
Use a single interface (note that interface inheritance, like you suggest in your question, technically counts as one interface here), but then you have to give each service operation a distinct name. You can either do that by naming the C# methods distinct, or by applying the [OperationContract(Name = "distinctname")]
attribute.
Use two separate interfaces, without any inheritance relationship in between them, publishing each on a different endpoint. You can have then have a service operation in each, having the same name, but with different parameters. You can still implement both interfaces with one implementation class, if you like/need to, of course.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With