title: Dependency inject a custom file provider for runtime compilation
body: <p>I am experimenting with <a href="https://www.mikesdotnetting.com/article/301/loading-asp-net-core-mvc-views-from-a-database-or-other-location" rel="nofollow noreferrer">loading views from a database</a>, and as suggested in the article one might want to add some caching to prevent hitting the database every time.</p>
  <p><code>ConfigureServices</code>:</p>
  <pre><code>services.AddHttpContextAccessor();
  services.AddMemoryCache();
  
  services.AddRazorPages()
      .AddRazorRuntimeCompilation(opt =&gt;
      {
          opt.FileProviders.Add(new DatabaseFileProvider(Configuration[&quot;AppSettings:SQLConnectionString&quot;]));
      });
  </code></pre>
  <p><code>DatabaseFileProvider</code> constructor:</p>
  <pre><code>private string _connection;
  
  public DatabaseFileProvider(string connection)
  {
      _connection = connection;
  }
  </code></pre>
  <p>How do I dependency inject an instance of <code>IMemoryCache</code> to the <code>DatabaseFileProvider</code> class?, as one can do with e.g. a singleton:</p>
  <p><code>ConfigureServices</code>:</p>
  <pre><code>services.AddSingleton&lt;AppUtils&gt;();
  </code></pre>
  <p><code>AppUtils</code> constructor:</p>
  <pre><code>private static IMemoryCache _cache;
  
  public AppUtils(IMemoryCache cache)
  {
      _cache = cache;
  }
  </code></pre>