1

I had a function in WCF that takes one parameter and is being called from another application. Now, I want to add a optional parameter in the WCF so that it does not affect the method being called in the previous code.

getData(int number, int type=0)

I tried using default value in the second parameter, setting [WebGet(UriTemplate but no success. If I use IsRequired property of DataMember then it will change the method call of previous code as well.

I don't want to create another WCF method. Is there any other workaround for it ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
AshishJindal
  • 176
  • 2
  • 10

2 Answers2

3

You can't use optional parameters when implementing an interface for a WCF.Can't use optional parameters when implementing an interface for a WCF

Use function overloading

getData(int number)
getData(int number, int type)
Community
  • 1
  • 1
Narek Arzumanyan
  • 616
  • 3
  • 11
0

You can't pass optional parameters in a Wcf service method, also you can't overload method in Wcf service with same Name. If you want to overload method you can use Name property to overload methods this way:

[OperationContract (Name = "MethodName")]

I resolved this issue by setting default null value in the URI so that made this parameter optional if user provided e.g: "type" value, i received it but if user didn't provide any value, in that case default value was null so that solved my problem.

Hope this answers your question.

Jahanzeb Khan
  • 81
  • 1
  • 1
  • 5