call singleton method in program.cs

Fazioli Amboina 61 Reputation points
2022-09-25T09:19:55.187+00:00

How do i call this 'LoadCashe' method from my service?

fkxfcNT.png

My goal is to load a cashe, before the razor pages run. The cashe should never be null. Its on WebAssembly, if that might matter.

[ what i have tried ]

i have heard about factory methods.

i have seen this: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-5.0 (see Call services From Main) But, i do not see similarities between this version of program.cs and mine. Plus i do not understand the example enough to make sure i do it as robustly as intended.

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,616 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 68,081 Reputation points
    2022-09-25T15:47:09.053+00:00

    Just pass preallocated object

    var api = new ApiClient();  
    api.LoadCache();  
    builder.Services.AddSingleton<ApiClient>(api);  
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.