Jon
Jon

Reputation: 1727

Blazor Global Singleton Object - use Dependency Injection?

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.

Upvotes: 3

Views: 1412

Answers (1)

tbdrz
tbdrz

Reputation: 2190

You should inject by the interface:

@inject ICustomer cust

Or register the class by itself:

services.AddSingleton<Customer, Customer>();

@inject Customer cust

Upvotes: 5

Related Questions