I'm building a custom file provider that implements IFileProvider in AspNetCore 2. The file provider will reach out to a webservice( consumerBrandingRepository ) to get the views. The webservice is configured in Startup and will cache data( via IMemoryCache ) that it receives. The webservice works flawlessly.
The problem is that because I'm calling BuildServiceProvider here to get access to the service config, I'm ending up with a MemoryCache that runs for the app and another one that runs for the File Providers. For full disclosure, the ConsumerBrandingRepository is an abstraction over Azure Storage and the purpose is to make it possible to change some views specific to a customer without redeploying the entire app.
What's the right way to get access to the services from within the services.Configure block shown below?
services.AddSingleton<IMemoryCache, MemoryCache>();
services.AddTransient<IConsumerBrandingRepository>(x => new CachedAzureConsumerBrandingRepository(new AzureConsumerRepository(azureConfig), x.GetRequiredService<IMemoryCache>()));
services.Configure<RazorViewEngineOptions>(options =>
{
var serviceProvider = services.BuildServiceProvider();
var logger = serviceProvider.GetRequiredService<ILogger<SettingsFileProvider>>();
var consumerBrandingRepository = serviceProvider.GetRequiredService<IConsumerBrandingRepository>();
var clientKeyFactory = serviceProvider.GetRequiredService<IClientKeyFactory>();
options.FileProviders.Clear();
options.FileProviders.Add(new CompositeFileProvider(
new SettingsFileProvider(consumerBrandingRepository, logger, clientKeyFactory),
new PhysicalFileProvider(_hostingEnvironment.ContentRootPath)));
});
Edit: Incoherent sentence Edit Added more details.