Provide ArrayPool object to JsonOutputFormatter co

2019-04-22 03:49发布

问题:

After upgrading from .net RC2 to RTM I find I need to supply a parameter to a constructor of JsonOutputFormatter that derives from ArrayPool. How do I get this object? I am newing JsonOutputFormatter manually because I need to configure ReferenceLoopHandling.

Only other related info I could find is this: https://github.com/aspnet/Mvc/issues/4562

    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddMemoryCache();
        services.AddSession();
        services.AddMvc();
        var formatterSettings = JsonSerializerSettingsProvider.CreateSerializerSettings();
        formatterSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        JsonOutputFormatter formatter = new JsonOutputFormatter(formatterSettings, ???);

        services.Configure<MvcOptions>(options =>
        {
            options.OutputFormatters.RemoveType<JsonOutputFormatter>();
            options.OutputFormatters.Insert(0, formatter);
        });

        //etc...
    }    

回答1:

var formatter = new JsonOutputFormatter(formatterSettings, ArrayPool<Char>.Shared);

Source

In the comments:

The JsonOutputFormatter now needs a ArrayPool when creating it, you can pass in ArrayPool.Shared.

I also noticed there is a .Create() method on ArrayPool.

var formatter = new JsonOutputFormatter(formatterSettings, ArrayPool<Char>.Create());