My application stopped working after migrating to beta 6/7, after investigating, I have found that my json deserializer is no more used ( Jil ), it's called for writing but not for reading.
It has been now 3 days that I'm searching forums and reading aspnet code but I haven't found the issue.
I noticed that JSON.net is used everywhere in beta 6, a little bit less in beta 7.
Here is my code :
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore(options =>
{
var jilFormatter = new JilMediaTypeFormatter();
options.OutputFormatters.Clear();
options.OutputFormatters.Add(jilFormatter);
options.InputFormatters.Clear();
options.InputFormatters.Add(jilFormatter);
options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json"));
options.ModelBinders.Add(new DocumentModelBinder());
options.ModelBinders.Add(new DataTablesBinder());
});
services.AddDataProtection();
services.AddWebEncoders();
}
Even if I do just InputFormatters.Clear() without adding an object, it keeps deserializing the requests, I have no idea how it does it.
And my JIL InputFormatter/OutputFormatter ( the ouputformatter works, I can break in the CanWrite, but nothing happens for CanRead )
internal class JilMediaTypeFormatter : IOutputFormatter, IInputFormatter
{
private static readonly string [] _readableTypes = new[] { "application/json", "text/json", "text/javascript" };
private static readonly Task<bool> _done = Task.FromResult(true);
private readonly Options _options = Options.RFC1123ExcludeNullsIncludeInherited;
public bool CanWriteResult(OutputFormatterContext context, Microsoft.Net.Http.Headers.MediaTypeHeaderValue contentType)
{
return contentType ==null || _readableTypes.Contains(contentType.MediaType.ToLowerInvariant());
}
public Task WriteAsync(OutputFormatterContext context)
{
context.HttpContext.Response.ContentType = "application/json";
using (var writer = new StreamWriter(context.HttpContext.Response.Body))
{
JSON.Serialize(context.Object, writer, _options);
writer.Flush();
return _done;
}
}
public bool CanRead(InputFormatterContext context)
{
return _readableTypes.Contains(context.HttpContext.Request.ContentType);
}
public Task<object> ReadAsync(InputFormatterContext context)
{
var reader = new StreamReader(context.HttpContext.Request.Body);
var result = JSON.Deserialize(reader, context.ModelType, _options);
return Task.FromResult(result);
}
}
InputFormatters are used only when [FromBody] is used in the parameter's list of the action.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With