There are two means by which the instance of ContentFactory can be added to the IServiceCollection:
ContentHandlingJsonServiceCollectionExtensions.AddContentTypeBasedSerializationSupport
ContentFactoryServiceCollectionExtensions.AddContent
The first adds our implementation IJsonSerializationSettingsProvider, an instance of ContentFactory and our custom JsonConverter implementations, ContentTypeConverter and ContentEnvelopeConverter.
The second is used to add content types to the ContentFactory. It also registers the generic factory for content handler dispatchers (IContentHandlerDispatcher<> implemented by ContentHandlerDispatcher<>).
If during service container setup, AddContentTypeBasedSerializationSupport is called prior to AddContent, the generic factory for content handler dispatchers will not be registered due to the conditional logic in that method.
We should either:
- Add extra conditional logic to
AddContent to ensure the factory is registered even if the ContentFactory has already been added to the collection, or
- Move the factory registration into its own method so that it can be explicitly added to the container if needed.
(My preference is for the second option, for two reasons:
- usage of our content type-based serialization is much more common than usage of our content handler dispatcher approach, so it makes sense to make it opt-in rather than automatically registering it with the serialization support.
- it is not at all obvious that you need to call
AddContent in order to register the factory)
Note that whilst this is clearly a breaking change, I have not yet found anywhere we're actually using the IContentHandlerDispatcher<> so this isn't a huge issue.