Friday, November 8, 2019

WCF Web Service Receiving Null Parameter

I was recently working with a client application (WPF) that used WCF Web Services to query and post data to Dynamics Ax (2012). One the requirements needed a new web service to be deployed.  I once again reached one of those situations where the service worked in development and it passed testing with no issues.  However, in 2 of roughly 10 sites, it started failing. One of my associates started debugging and found that the client application was sending the data, however the web service was receiving a null parameter.  I should mention that the parameter was being sent as content data in a post request in json format.

Getting copy of the data being sent was not much of problem.  I then emulated the call using Postman.  Irritatingly, it worked. So, it works in Postman and does not work in the client application and we were almost stumped.  As a last ditch attempt, I rewrote the sending routine to iterate through the records and send 5 records to be processed only.  The routine failed on the second iteration.  Debugging the call and extracting the data, I found that there was a French character in the stream.  Rechecking the settings on Postman, I found that the Postman was using UTF-8.  Honestly, I thought this would be default these days, but I guess I was mistaken.  I modified the call to force it to use UTF-8 and it worked.

For reference:
using (WebClient client = new WebClient())
{
     client.Headers[HttpRequestHeader.ContentType] = "application/json";
     client.Encoding = Encoding.UTF8;

     …<do other stuff>
     var dataString = client.UploadString(apiUrl, jsonObj);
     ResponseClass webresult = JsonConvert.DeserializeObject<ResponseClass>(dataString);
}

No comments:

Post a Comment