Dependency inject a custom file provider for runtime compilation

I am experimenting with loading views from a database, and as suggested in the article one might want to add some caching to prevent hitting the database every time.

ConfigureServices:

services.AddHttpContextAccessor();
services.AddMemoryCache();

services.AddRazorPages()
    .AddRazorRuntimeCompilation(opt =>
    {
        opt.FileProviders.Add(new DatabaseFileProvider(Configuration["AppSettings:SQLConnectionString"]));
    });

DatabaseFileProvider constructor:

private string _connection;

public DatabaseFileProvider(string connection)
{
    _connection = connection;
}

How do I dependency inject an instance of IMemoryCache to the DatabaseFileProvider class?, as one can do with e.g. a singleton:

ConfigureServices:

services.AddSingleton<AppUtils>();

AppUtils constructor:

private static IMemoryCache _cache;

public AppUtils(IMemoryCache cache)
{
    _cache = cache;
}


Read more here: https://stackoverflow.com/questions/67940714/dependency-inject-a-custom-file-provider-for-runtime-compilation

Content Attribution

This content was originally published by Ason at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: