将 ArrayPool 对象提供给 JsonOutputFormatter 构造函数 [英] Provide ArrayPool object to JsonOutputFormatter constructor
问题描述
从 .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屋!