DanielAvadanei-5617 avatar image
0 Votes"
DanielAvadanei-5617 asked DanielAvadanei-5617 edited

Instantiate one object for all blazor pages in Blazor WebAssembly?

I have a blazor webassmbly app and I want to instantiate an object only once that can be shared between all the blazor pages. What I want to do is to instantiate this object when the application starts, in StartUp.cs and then inject that object in all pages to use it. What I did was to create this class :

public class AccountService
HttpCaller _httpCaller;
AuthenticationStateProvider _provider;
public AccountViewModel _currentAccount;

     public AccountService(HttpCaller caller, AuthenticationStateProvider provider)
         _httpCaller = caller;
         _provider = provider;

     public async Task<OperationResult<AccountViewModel>> GetCurrentAccount()
         if (await UserIsAuthenticated())
             return await _httpCaller.HttpCall<bool, AccountViewModel>(API.GetCurrentAccount, true, true);                            

         return NotAuthenticated<AccountViewModel>();


I call GetCurrentAccount() method and associate the result to _currentAccount property in in StartUp.cs class :

AccountService accountService = serviceProvider.GetRequiredService<AccountService>();
var result = await accountService.GetCurrentAccount();
if (result.IsSucces)
accountService._currentAccount = result.Result;

Then I inject AccountService object in a blazor page and my expectation is that _currentAccount property should contain the AccountViewModel object associated in StartUp.cs. But is null. The AccountService object is created as a scoped object before calling GetCurrentMethod() in StartUp.cs class:

From my knowledge a scoped object should exist until the app is reloaded, there is no reloading but the _currentAccount property is still null.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @DanielAvadanei-5617,

As far as I know, blazor WSML doesn't contain the startup.cs by default. Could you please tell me how you call GetCurrentAccount() method and associate the result to _currentAccount property in StartUp.cs class?

Have you tried to call this method inside the Blazor OnInitialized method and check if other page could get the same object? Like this article shows.
Please notice: the Blazor WSM is running at client-side, it is a different from Blazor server app.

1 Vote 1 ·

Yes, you are correct my mistake, in Program.cs file not Startup.cs. The GetCurrentAccount() method is called in Program.cs in blazor wasm web app (client), not on the server. I use the OnInitialize method in razor file like this, to get the current account:


AccountService object is injected in the razor file where I call the OnInitialized() method like this:


This is the Program.cs class :

This is a part of the AccountService class where I call GetCurrentAccount() method:

The issue is that the _currentAccount is null in the razor file after I instantiate the object in Program.cs class. The data is caming from the server by using the HttpCall method and I confirm that data does come from the server, I debugged the Program.cs file and the object is populated and asociated to the property but when I inject the object in the razor file, the object is null.

0 Votes 0 ·
image.png (20.1 KiB)
image.png (2.7 KiB)
image.png (102.8 KiB)
image.png (46.4 KiB)

Usually, the result of authenticating is persisted not the action of authenticating. Blazor WASM comes with authentication/authorization APIs and guidance. Check out the authentication/authorization docs, you should be able to find an approach that fits your needs.

Secure ASP.NET Core Blazor WebAssembly

Otherwise, you are looking for a singleton pattern or registering a singleton service.

0 Votes 0 ·

0 Answers