When deserializing a request I get a non-null but empty dictionary.
Object I want to post:
public class Data
{
public IDictionary<string, object> Dictionary { get; set; }
}
Here is the body I send with my request:
{"Dictionary":{"key1":"value1","foo":"bar"}}
The built-in serializer creates a Data
object with an empty Dictionary
. The Newtonsoft serializer behaves how I want. So after some googling I changed the service contract to accept a Stream
rather than a Data
.
using Newtonsoft.Json;
[ServiceContract]
interface IMyServiceContract
{
[OperationContract]
[WebInvoke(
Method = "POST",
UriTemplate = "data",
RequestFormat = WebMessageFormat.Json)]
void PostData(Stream body);
}
class MyServiceContract : IMyServiceContract
{
void PostData(Stream body)
{
using (var reader = new StreamReader(body))
json = reader.ReadToEnd();
data = JsonConvert.DeserializeObject<Data>(json);
// ...
}
}
The main problem here is that the request is no longer accepted if I specify the header Content-Type: application/json
, which I obviously want, but it would also be nice if the method signature mentioned Data
.
How can I specify a custom deserializer for my service? Or, if not possible, make the current solution work even if Content-Type is specified?