My Index
page has the @code
block calling a service method:
@using BlazingPizza.Services
@inject PizzaService PizzaService
...
@code {
private List<Pizza> specials = new List<Pizza>();
protected override async Task OnInitializedAsync()
{
specials = await PizzaService.GetPizzasAsync();
}
}
The service is already registered in Program.cs
:
builder.Services.AddSingleton<PizzaService>();
I also tried injecting the interface and declaring the following:
builder.Services.AddSingleton<IPizzaService, PizzaService>();
But whenever I call GetPizzasAsync
, the exception happens.
NullReferenceException: Object reference not set to an instance of an object.
BlazingPizza.Pages.Index.OnInitializedAsync() in Index.razor
+
specials = await PizzaService.GetPizzasAsync();
What am I missing? I am following official's tutorials.