Skip to content

If IServiceCollection.AddContentTypeBasedSerializationSupport is called prior to IServiceCollection.AddContent, the generic factory for content handler dispatchers will not be registered in the service collection #282

@jongeorge1

Description

@jongeorge1

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:

  1. Add extra conditional logic to AddContent to ensure the factory is registered even if the ContentFactory has already been added to the collection, or
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions