1

Let's say I have a controller that does a thing. This thing needs to be logged, however, I'd like to log it into a database that's accessible through the API, so it is also a controller with a service, repository, and validation.

I have a LoggerService that should update its database by LoggerService.UpdateAsync(), but it's not instantiated in the controller that needs to be logged. How would I do that?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
felipebubu
  • 131
  • 7

1 Answers1

2

From your description, my understanding is that you want to use dependency injection to create an instance of the desired API controller.

You can tell the MVC to register all your controllers as services:

builder.Services.AddMvc().AddControllersAsServices();

Then you can simply inject the desired controller in your other controller via the DI mechanism and invoke its action method.

For other solutions, you can refer to this link.

Hope this will help you. Please let me know if my understanding is wrong.

Chen
  • 4,499
  • 1
  • 2
  • 9