I have a service that I'm adding to my DI container simply using services.AddSingleton<MyService>
.
This service requires a static array of strings, which I'm fetching from S3 using an external method LoadStringArrayAsync
. This method will be called once for the lifetime of the application and the array will be saved in memory so the other methods in the service can utiliz eit. However, I'm not sure where to call this method.
The constructor is not an option since it doesn't make sense and you can't call async methods from it.
I thought about creating a method that will return the string array either IMemoryCache
or fetch it and then cache it, something like so:
private async Task<IEnumerable<string>> GetStringArrayAsync()
{
return _memoryCache.GetOrCreateAsync("stringArray", async factory =>
{
// Code to fetch string array from S3.
}
}
But there must be a simpler way more logical way to just call this method and save it into a private member.
Ideas are welcomed.