【问题标题】:Provide ArrayPool object to JsonOutputFormatter constructor向 JsonOutputFormatter 构造函数提供 ArrayPool 对象
【发布时间】:2016-06-28 19:11:28
【问题描述】:

从 .net RC2 升级到 RTM 后,我发现我需要为从 ArrayPool 派生的 JsonOutputFormatter 的构造函数提供一个参数。我如何得到这个对象?我正在手动新建 JsonOutputFormatter,因为我需要配置 ReferenceLoopHandling。

我能找到的唯一其他相关信息是: 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...
    }    

【问题讨论】:

    标签: c# json.net asp.net-core .net-core


    【解决方案1】:
    var formatter = new JsonOutputFormatter(formatterSettings, ArrayPool<Char>.Shared);
    

    Source

    在 cmets 中:

    JsonOutputFormatter 现在在创建它时需要一个 ArrayPool,你 可以传入ArrayPool.Shared。

    我还注意到 ArrayPool 上有一个 .Create() 方法。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-15
      • 2018-10-21
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      相关资源
      最近更新 更多