1

I have a domain model where I've been successfully using a custom IContractResolver that extends CamelCasePropertyNamesContractResolver to remove specific properties at the controller level using a custom Attribute that implements IControllerConfiguration.

I'm now in a situation where I need to expose those properties I was removing based on a specific Action. My plan was to simple change my contract resolver to this:

public MyContractResolver(bool expose = false)

and update how i use the ContractResolver (i.e. create new JsonSerializerSettings per action).

The problem i'm facing is that the Contracts are being cached for each object and CreateProperties is not being called on new instances and therefore ignoring the "expose" logic (in fact it's whoever is called first that gets cached).

I'm able to work around this by simply extending my custom contract resolver but i'd like to be able to have 1 that adapts dynamically. Is that possible? Is there a better way to get it change behavior vs modifying the constructor?

Thanks,

Stringer Bell
  • 265
  • 1
  • 4
  • 13
  • `CamelCasePropertyNamesContractResolver` has a bug in which all instances of derived types share cached contract information. To work around the bug, inherit from `DefaultContractResolver` and set `NamingStrategy = new CamelCaseNamingStrategy()`. See [Json.Net: Html Helper Method not regenerating](https://stackoverflow.com/q/30740734/3744182) for details. In fact, I think this is a duplicate. Agree? – dbc Sep 17 '17 at 03:16
  • 1
    agreed. Maybe the title should be changed to specifically call out the IContractResolver was extending the CamelCasePropertyNamesContractResolver – Stringer Bell Sep 17 '17 at 23:18

0 Answers0