title: ASP.NET Core MvcJsonOptions dependency injection without modified closure?
body: <p>(This question is similar to <a href="https://stackoverflow.com/q/45807507/1541820">ASP.NET Core MvcOptions dependency injection without modified closure?</a> However, I struggle to apply the solution of that question to this one.)</p>
  
  <p>In my ASP.NET Core 1.1.3 project, I currently inject an <code>ITraceWriter</code> dependency into <code>MvcJsonOptions</code> in the <code>ConfigureServices</code> method in the following way:</p>
  
  <pre><code>public override IServiceProvider ConfigureServices(IServiceCollection services)
  {
      services.AddProjectSpecificStuff();
  
      ITraceWriter traceWriter = null;
  
      services.AddMvc().AddJsonOptions(options =&gt;
      {
          options.SerializerSettings.TraceWriter = traceWriter;
      });
  
      var provider = base.ConfigureServices(services);
  
      traceWriter = provider.GetService&lt;ITraceWriter&gt;();
  
      return provider;
  }
  </code></pre>
  
  <p>This works, but causes code analyzers such as ReSharper to complain about access to modified closure.</p>
  
  <p>Is there an alternative to achieve the same dependency injection without using modified closure?</p>