提供ArrayPool对象给JsonOutputFormatter构造函数 [英] Provide ArrayPool object to JsonOutputFormatter constructor

查看:54
本文介绍了提供ArrayPool对象给JsonOutputFormatter构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我只能找到的其他相关信息是: https://github.com/aspnet/Mvc/issues/4562

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...
    }    

推荐答案

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

来源

在评论中:

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

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

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

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

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

这篇关于提供ArrayPool对象给JsonOutputFormatter构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆