I would like to use a JsonConverter<>
which has dependency, and that dependency can not be a singleton, instead should be resolve as scoped...
I have to configure my System.Text.JsonConverter
in Startup
, so I must provide the JsonSerializaionOptions
along with my JsonConverter<>
instance in Startup
. I can live together the JsonConverter<>
as singleton, I instantiate myself, but how can I inject its scoped dependencies?
The only horrible thing comes in my mind to have a IServiceProvider
property, and some other logic later (middleware?) checks if that property is initialized and initializes it. So in the actual Read
and Write
methods of the JsonConverter<>
can access to the scoped dependency via the IServiceProvider
.
This sounds sooo errorprone and smells, once because of timing (what is the actual conversion occurs before the property injection), but my main concern is the concurrency, having this JsonConverter<>
a singleton by design, so the very same IServiceProvider instance in the injected property will be used concurrently by all the web app threads...
What am I missing here?
- Is having a scoped dependency for a
JsonConverter<>
smells? - Is there a way to not instantiate the
JsonConverter<>
inStartup
? (yes I know I could instantiate JsonSerializerOptions and do manually the serialization withJsonConverter
), but this way I can not use many built in json support of ASP.NET Core