I have an Asp.Net Core 3.1 Razor Pages website in which I have a static Repository class holding the most used items. I search these items a lot and it takes around 4 minutes to initialize them.
public static class Repository
{
public static Dictionary<int, RepositoryPerson> People { get; private set; }
public static async Task InitAsync(INoSqlSettings settings)
{
if (People != null || loading)
{
return;
}
loading = true;
var people = await db.People.ToDictionaryAsync(p => p.Id);
People = ConvertToRepository(people);
//..and lots of other stuff
loading = false;
}
}
At first, I tried to load this with a hosted service but it fails because it takes too long. Now I load it in the Index.cshtml.cs file's OnGetAsync(). But the problem is that every once in awhile, it seems like the .exe file closes because the website initializes again. Is this normal? How can I make the program run only once and share the in-memory repository forever?