Currently, new instance of JsonSerializerOptions is created for every instance of HaveIBeenPwnedClient. Especially in high-load situations we don't want to keep a single instance of the client around for the lifetime of the application because of the injected (or internal) HttpClient and problems like DNS expiration and running out of ephemeral ports. So we want to recreate clients and because of that instantiation is designed to be as light-weight as possible.
However, since (de)serialization info is cached inside the JsonSerializerOptions (pre .NET 7) it's also a performance hit to recreate client instances. It means that for every client the (de)serialization info is created again.
Solution: Cache the JsonSerializerOptions for the lifetime of the application.
Currently, new instance of
JsonSerializerOptions
is created for every instance ofHaveIBeenPwnedClient
. Especially in high-load situations we don't want to keep a single instance of the client around for the lifetime of the application because of the injected (or internal)HttpClient
and problems like DNS expiration and running out of ephemeral ports. So we want to recreate clients and because of that instantiation is designed to be as light-weight as possible.However, since (de)serialization info is cached inside the
JsonSerializerOptions
(pre .NET 7) it's also a performance hit to recreate client instances. It means that for every client the (de)serialization info is created again.Solution: Cache the
JsonSerializerOptions
for the lifetime of the application.