I want to add more parameters to my JSON WebService without breaking call from old clients. Example:
My WebService in Service.asmx
[WebMethod]
public string Ping(string msg, string additionalInfo)
{
if (string.IsNullOrEmpty(additionalInfo))
{
return "Process msg with old version";
}
return "Process msg with new version"; ;
}
//My old web service does not have additionalInfo arguments
//public string Ping(string msg) {..}
Web.config tells that my WebService is JSON-based
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000" />
</webServices>
</scripting>
if clients call my new Json WebService with all the parameters => everything is fine
CallWs("http://localhost:48918/Service.asmx/Ping", '{"msg":"hello", "additionalInfo":""}')
But all the current clients won't give the additionalInfo
:
CallWs("http://localhost:48918/Service.asmx/Ping", '{"msg":"hello"}')
my new WebService will immediately return error:
string(654) "{"Message":"Invalid web service call, missing value for parameter: \u0027additionalInfo\u0027.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}"
So my customers will have to change theire code in order to use my new WebService, I don't want that. I want to give default values to my new WebService, What is the best way to do?
Possible duplication: Can I have an optional parameter for an ASP.NET SOAP web service But none of the response works for me.
FYI my customers often call my JSON WebService via PHP, they simply make a POST request to the service endpoint:
$ch = curl_init("http://localhost:48918/Service.asmx/Ping");
$wsrq = array(
"msg" => "Hello",
//"additionalInfo" => "World",
);
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_POSTFIELDS => json_encode($wsrq),
CURLOPT_HTTPHEADER => array("Content-type: application/json; charset=utf-8"),
));
$response = curl_exec($ch);