1

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.

7
  • Does MyClass have any dependencies of its own that need to be scoped? Commented Dec 9, 2022 at 20:15
  • @JimG. No, MyClass only have a pair of attributes, like public class MyClass { public string A { get; set; } public string B { get; set;} } Commented Dec 9, 2022 at 20:32
  • Check to make sure that there are not two version of MyClass in 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 Commented Dec 9, 2022 at 22:25
  • There is only one version. It's a project created from scratch using Visual Studio, I only added the MyClass class, the controller page, and the lines to register the service. Commented Dec 9, 2022 at 22:42
  • 1
    Created an empty Blazor project. The controller has been added and mapped. Registered MyClass as a scope and injected it into the controller. Everything is fine. It seems to me that you have done something else besides the steps you mentioned. Commented Dec 10, 2022 at 7:33

1 Answer 1

0

I think this answer comes too late, but anyway you should pass an Interface from your "MyClass" to MyClass constructor instead a class. And change your readonly property:

public class DownloadController : Controller, IMyClass
{

    private IMyClass MyClass{ get; } = default!;

    public DownloadController(IMyClass myclass)
    {            
        MyClass = myclass;
    }
}

public interface IMyClass
{
    // your signature methods here
}

It's explained here: Dependency injection into controllers in ASP.NET Core

Using dependency injection in own class (not in component)?

And register your class MyClass like a service at Progam.cs

builder.Services.AddSingleton<IMyClass, MyClass>();
builder.Services.AddControllers();
Sign up to request clarification or add additional context in comments.

Comments

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.