JsonMediaTypeFormatter has a public property to access the SerializerSettings, you can set ReferenceResolver to your custom class.
When the media type formatter creates the JsonSerializer instances, it simply passes the serializer settings to the json serializer. Json.NET will use the assigned ReferenceResolver and add items to it. This has two side effectes:
1. The resolver has to be thrad-safe and might mix references from different thread which use the media type formatter at the same time.
2. When serialization finishes, the resolver is not notified, so it cannot clear its internal lists. References stay in the dictionary forever and the GC will never free the memory.
This all only happens when you assign your own ReferenceResolver instance. When you leave it as default (null), a new resolver is instantiated for each serialization/deserialization. But in current implementation, ReferenceResolver is nearby useless.
Comments: ** Comment from web user: CarstenSchuette **
When the media type formatter creates the JsonSerializer instances, it simply passes the serializer settings to the json serializer. Json.NET will use the assigned ReferenceResolver and add items to it. This has two side effectes:
1. The resolver has to be thrad-safe and might mix references from different thread which use the media type formatter at the same time.
2. When serialization finishes, the resolver is not notified, so it cannot clear its internal lists. References stay in the dictionary forever and the GC will never free the memory.
This all only happens when you assign your own ReferenceResolver instance. When you leave it as default (null), a new resolver is instantiated for each serialization/deserialization. But in current implementation, ReferenceResolver is nearby useless.
Comments: ** Comment from web user: CarstenSchuette **
I think Json.NET does not use a shared DefaultContractResolver for resolving references, but a DefaultReferenceResolver. And it does not use a shared static instance, but creates a new one for each JsonSerializer instance. That's why there is no problem with references that are hold in the resolvers internal Dictionary when using JsonSerializer directly.
But when using a JsonMediaTypeFormatter, the custom binding resolver is bound to the media type formatter instance, and this is a problem because you only have one media type formatter instance in your global http configuration. How should the custom binding resolver know when to flush outdated references?