2016-06-28 32 views
7

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

只有等相关信息,我能找到的是这样的: 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... 
    }  

回答

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

Source

在评论:

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

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

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