I have a D365FO Service Operation endpoint that I am trying to return an ArrayList of .NET Dictionary<string, object> objects that will be consumed by an external .NET service. If I call the method from within D365FO, it returns the ArrayList of objects correctly but if I call this endpoint from an external service I get the following error.
Here is how the project is set up:
D365FO Project
- X++ Project
- .NET Project
Example call would be process as follows:
- External service hits X++ service operation endpoint
- Gets passed to .NET intermediate class for business logic
- This is where the ArrayList of .NET Dictionary<string, object> objects is created
- ArrayList of .NET Dictionary<string, object> objects gets returned to X++ service operation endpoint to be returned to external service
- This is where the error occurs
External Service .NET code I am using to call the Service Operation endpoint:
using (var client = new HttpClient()) { ... var response = await client.PostAsync($"api/Services/{serviceGroupName}/{serviceName}/{methodName}", new StringContent(JsonConvert.SerializeObject(requestBody))).ConfigureAwait(false); }
This code works for other service operation endpoints I have created and am calling so I believe this code to be correct.
From the error it looks like the Service Operations service is trying to serialize the returned object before handing it off to the external service and hitting a cast exception. Since I'm not doing any explicit casts I'm guessing the service is doing it somewhere outside of my control.
I know that AX/D365FO has Maps which is the X++ equivalent of .NET Dictionary (docs.microsoft.com/.../x-csharp-comparison-collections-of-keys-with-values) the only reason I didn't go this route at first is that at the end of this my external .NET application needs this data in IEnumerable<Dictionary<string, object>> format and wasn't sure if there was a conversion between the two?
Are there any other options or anything else I'm missing?
It turns out a couple of the objects I was adding to the dictionary were X++ utcdatetime objects, apparently the serializer doesn't like that. Once these were converted to be all .NET based objects everything worked fine as described above.