2
votes

I have a single CUSTOMER object that needs to accessed / available to all parts of Blazor application , from the MainLayout to NavMenu to the razor components. How do I implement a Global Singleton Object?

I have attempted to use DI in Startup.cs like this

services.AddSingleton<ICustomer, Customer>();

And then in MainLayout

@inject Customer cust

then set some properties.

And then in CustomerPage

  @inject Customer cust

But values are BLANK in CUSTOMERPAGE

What am I missing? I need to persist this object throughout the app.

1

1 Answers

4
votes

You should inject by the interface:

@inject ICustomer cust

Or register the class by itself:

services.AddSingleton<Customer, Customer>();

@inject Customer cust