Reputation: 417
I have to use a custom JsonConverter
with ASP.NET Core for a reason, and I need to use it with JsonInputFormatter
. The only way I've found is to use AddJsonOption
extension method like this:
services
.AddMvc()
.AddJsonOptions(jso => jso.SerializerSettings.Converters.Add(new CustomConverter()))
But it has a flaw: CustomConverter
requires a dependency from a DI container which cannot be easily solved at configuration time.
So the question: is there any programmer friendly way to supply a JsonConverter
with dependency to ASP.NET Core JsonInputFormatter
?
Upvotes: 1
Views: 784
Reputation: 64180
One quick workaround would be to postpone it to the Configure
method.
public Confiugre(IAppBuilder app, IOptions<MvcOptions> mvcOptions, IOptions<MvcJsonOptions> jsonOptions)
{
var formatter = mvcOptions.InputFormatters.OfType<JsonInputFormatter>().Single();
jsonOptions.SerializerSettings.Converters.Add(
new CustomConverter(formatter));
...
}
Still feels a bit dirty though ;)
Upvotes: 2