I'm programming using Server-side Blazor. In program.cs I have this to register the service:
builder.Services.AddScoped<MyClass>();
Then, I have a controller page (not a razer component page) with this definition (using the class in the constructor for the object to be injected):
public class DownloadController : Controller
{
private readonly MyClass _myobject;
public DownloadController(MyClass myobject)
{
this._myobject = myobject;
}
}
The problem is that "myobject" is always null, as if dependency injection is not working for the controller. If I inject the object in a razor component page (using @inject Myclass myobject), it works without a problem.
Any ideas about what is going on?
Edit: after doing some experiments, injection works if I change AddScoped to AddSingleton. The problem is that the object shouldn't be a singleton class, it should be created and disposed during the scope of the Blazor circuit. It seems I'm lacking some fundamentals behind the scenes? Could someone explain why it'd work with AddSingleton and not with AddScoped.
MyClasshave any dependencies of its own that need to be scoped?MyClassin separate namespaces. Could be that one of them is used by the controller while the other is the one that was registered with the container