.NET Core 2 – Create instance of controller class which has a repository

I have the following controller Class;

public class MyController: Controller
{
    private IValueService _service;

    public MyController(IValueService service)
    {
        this._service = service;
    }

    public void MyMethod()
    {
        var values = _service.GetAll();
    }
}

I would like to create an instance of this Class and call the method MyMethod(), e.g.

var MyCopy = new MyController();
MyCopy.MyMethod();

However i get the message i need to give the required service parameter. How would i create an instance of a controller that has a service (repository) so i can call its methods?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

In Startup file add :

 services.AddMvc().AddControllersAsServices();

it will registers all controllers in your application with the DI container.

Then :

   var serviceProvider = HttpContext.RequestServices;
     var controller =(MyController)serviceProvider.GetRequiredService<MyController>();
     controller.Method();

Method 2

Well, on startup I would like to run a method in a class that uses a repository. This method first opens a SocketIO connection and then it should use the repository to save new incoming data to the database.

Then that logic shouldn’t be inside a controller but in some service type instead which you register with the dependency injection container. Controllers are to respond to requests at certain routes, but your thing sounds like it’s a general initialization step in your application that runs outside of a request. So there should be no controller involved.

Instead, you want to make some service first:

public class MyInitializationService
{
    private readonly IValueService _valueService;

    public MyInitializationService(IValueService valueService)
    {
        _valueService = valueService;
    }

    public void Initialize()
    {
        var values = _valueService.GetAll();
        // do something
    }
}

You then register that class in the ConfigureServices method of your Startup class:

services.AddTransient<MyInitializationService>();

And then, you can inject that type into the Configure method and call its Initialize method:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, MyInitializationService initializationService)
{
    initializationService.Initialize();

    // …
    app.UseStaticFiles();
    app.UseMvc();
}

There are various ways of running something at the beginning when an application starts. Calling it inside Configure is just one way, which may or may not be appropriate depending on how early you want your code to run (since Configure runs very early). Another good way would be to register a callback to the ApplicationStarted event of the application lifetime. I have an answer over here which goes into more details on that.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x