Provide ArrayPool object to JsonOutputFormatter constructor

你离开我真会死。 提交于 2019-12-10 02:44:43

问题


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());


来源:https://stackoverflow.com/questions/38084437/provide-arraypool-object-to-jsonoutputformatter-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!