I’ve loaded the Blazor web app template in Visual Studio, and I’m using .NET 9.0. I was doing some experimentation with dependency injection in order to try to get a good understanding of how that worked within Blazor.
I was able to create a simple DI scenario that worked, but then as I experimented, I introduced a bug that broke the DI. When I tried to retrace my steps, the original code no longer worked (except in Incognito mode). I made of a point of “Cleaning” my solution prior to build assuming there was some sort of rogue artifact causing problems, but the problem persisted.
It has me second-guessing my incremental changes that didn’t seem to work (was I running the code I thought I was, or was there something persisting that I wasn’t aware of?)
I don’t think the specifics of my code are particularly relevant, but I can give a bit of background. Basically, I created a ICounterService to use with the sample Counter page (in the WebClient.App project of the templated solution). I created a “variable” implementation of that which takes an integer in the constructor, and then a SingleStep version that inherits the variable implementation, but with a value of 1.
The CounterPage uses the service, then, to increment that counter value.
Modified Counter.razor:
@page "/counter"
@rendermode InteractiveWebAssembly
@inject ICountService CountService
<PageTitle>Counter</PageTitle>
<h1>Counter</h1>
<p role="status">Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
Console.WriteLine("Incrementing counter with count service...");
currentCount = currentCount + CountService.GetStep();
}
}
My dependency “Counter Service” components:
public interface ICountService
{
int GetStep();
}
public class VariableCountService : ICountService
{
protected int StepSize { get; set; }
public VariableCountService(int stepSize)
{
StepSize = stepSize;
}
public int GetStep()
{
return StepSize;
}
public override string ToString()
{
return $"Variable Counter [{StepSize}]";
}
}
public class SingleStepCountService : VariableCountService
{
public SingleStepCountService()
: base(1)
{ }
public override string ToString()
{
return $"Single Step Counter";
}
}
This is my Program.cs file:
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents()
.AddInteractiveWebAssemblyComponents();
// This WORKS
builder.Services.AddScoped<ICountService, SingleStepCountService>();
// This does NOT
// builder.Services.AddSingleton<ICountService>(new VariableCountService(2));
// This does NOT
//var counter = new VariableCountService(2);
//builder.Services.AddSingleton<ICountService>(counter);
// This does NOT
//var counter = new VariableCountService(2);
//builder.Services.AddSingleton<ICountService, VariableCountService>((x) => { return counter; });
var app = builder.Build();
// SNIP
app.Run();
}
}
}
The code by the green arrow worked. I made incremental steps that didn’t (yellow), but when I rolled back, original no longer worked (except in Incognito)
