1

Objective: Have a running hosted service processing queues (from a cloud service) and dynamically route them to the controller. Kind of like in php with the Larval framework calling jobs.

Current Implementation:

Type type = Type.GetType(nameSpace + ".Controllers." + sqsMessage.controller);
Object obj = Activator.CreateInstance(type, null);
MethodInfo methodInfo = type.GetMethod(sqsMessage.method);
result = methodInfo.Invoke(obj, sqsMessage.body);

Why current implementation does not work: The controllers have constructors that arguments vary, and the queue sender should not have to be responsible for passing those arguments:

example:
Controller 1 - Constructor 1 task the ILogger argument
Controller 2 - Constructor takes zero arguments

Question: How do I dynamically call a controller and specific action from a running background service?

using asp.net core 2.1

2 Answers 2

3

What you are trying to do does not sound like a good idea. Controller actions are invoked in a request scope, so there’s always a HTTP request around when they are called.

A background service however does not run within the scope of a HTTP request. They deliberately run outside of it. As such, calling into a controller from a background service does not appear right.

If you see yourself wanting to do that, then that’s a clear sign that your controller is doing too much work. You should try to extract the logic that is within your controller actions into a separate service. So that way both your controller and your background service can depend on that common service to do stuff.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for a little deeper insight! I understand that the controller actions are invoked in a request scope. I was trying to ideally add support for “any” Http endpoint, this way I do not have to add another function for each Http endpoint that is added. However, maybe that is just laziness on my side. I think I will implement a combination of your suggestion with, @gsxrboy73 suggestion
0

If your controller constructors take Interfaces as their argument, you could use Dependency Injection to accomplish this.

In .Net Core 2.1 there is a simple built in Ioc Container. To implement "Controller 1" you would specify the class that implements the ILogger interface.

You can add this in the Startup.cs in the ConfigureServices method like:

public void ConfigureServices(IServiceCollection services)
    {
        // Dependency Injection
        services.AddScoped<ILogger, MyLoggerClass>();
 }

where the MyLoggerClass implements the ILogger interface. Anytime any constructor that calls for that interface as an argument (whether in a Controller or some other class), the Ioc container will initialize the MyLoggerClass and pass it in automatically.

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.