I'm having an issue with the JsonMediaTypeFormatter
that as far as I can tell just started happening very recently.
At some point, the object I have (a plain old poco) started serializing complete as backing fields. I am using auto properties (again just a poco) but for whatever reason the output from the JsonMediaTypeFormatter
is k__backingfield<propname>:"value"
In the last weeks we have upgraded to visual studio 2015 (though I doubt that is the root cause of the problem, as this has been working for a few weeks prior).
I have cleared all forms of nuget caches, deleted all bin/obj directories.... uninstalled and reinstalled visual studio 2015 professional... repaired visual studio 2015 professional.... This code hasn't changed in some time.
We are using version 5.2.0
of System.Net.Http.Formatting
.
Thank you for any help.
Just a quick update.... this is not happening on anybody else's machine. I am not experiencing any errors (that I am noticing). I'm going to add some visual studio 2015 related tags to this as well
Ok another quick update. I have sent a LinqPad script as well as the assemblies required to run that script to a co-worker. The assemblies I am sending are the exact ones that I have pulled from nuget and compiled on my machine. When he runs the code, it serializes as desired. My machine (and now another co-worker's machine) are experiencing the issue still.
I'm continuing to isolate this as much as possible but any further guidance would be greatly appreciated
I can work around this issue by turning the JsonMediaTypeFormatter.UseDataContractJsonSerializer
property to true
. I'm doing more research as to why this works and why I would want or not want to do this... I'll have to run some more tests in the morning with co-workers who are not having this problem. I don't like the idea of a code change fixing something that is behaving unpredictably.
OK! One more update. So far the machines affected have recently installed Active Reports 10. We are performing more tests and are going to install active reports 10 on a machine that is currently not affected to see if that indeed is the culprit. Adding a tag for Active Reports. I will open a bug with them if this ends up being true.
Are your POCOs marked with a [Serializable]
attribute? If so, this may be the cause of the issue.
Web API apparently ships with a Json.Net DefaultContractResolver
that is configured with IgnoreSerializableAttribute = false
. One of the effects of this setting is that the names of auto properties will be serialized in the format <PropertyName>k__BackingField
where PropertyName
is replaced with the actual name of the property.
To fix this, you can either remove the [Serializable]
attribute from your classes, or you can replace the out-of-box resolver with a new one that has IgnoreSerializableAttribute = true
. To do the latter, add the following to the Application_Start
method in your Global.asax
:
var resolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
resolver.IgnoreSerializableAttribute = true;
var config = GlobalConfiguration.Configuration;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = resolver;
Here is a fiddle that demonstrates the concept (although it is a console app, not Web API): https://dotnetfiddle.net/Bu6lgy
Ok. Installing Active Reports 10 adds Newtonsoft.Json
to the GAC. Removing that assembly from the GAC has solved this problem. I will be posting a bug with active reports.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With