3

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 Answer 1

5

You should inject by the interface:

@inject ICustomer cust

Or register the class by itself:

services.AddSingleton<Customer, Customer>();

@inject Customer cust
Sign up to request clarification or add additional context in comments.

1 Comment

Or Register the class by itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.