0

I'm creating Type (by reflection) that is extending ApiController. How can I dynamically register it (It can be registered at startup, no need to do this at runtime).

Long story: So in my application I have multiple interfaces, i.e.:

public interface IMyInterface
{
     MyResponse Hello(MyRequest request);
}

The thing that I want to achieve is for each interface create controller that should look like this:

public class IMyInterfaceController : ApiController
{
    private readonly IMyInterface _MyInterface;
    public IMyInterfaceController (IMyInterface MyInterface)
    {
         _MyInterface=MyInterface;
    }
    
    public MyResponse Hello([FromBody] MyRequest request)
    {
        return MyInterface.Hello(request);
    }
}
6
  • You can generate code using t4 templates at development time and avoid taking a hit on the reflection. Commented Jul 21, 2020 at 9:17
  • 2
    Does this answer your question? Dynamic register controller in C# Web Api Commented Jul 21, 2020 at 9:22
  • @Ramesh this not for core Commented Jul 21, 2020 at 9:27
  • 1
    In Core - you can do the similar thing in the Startup Class instead of global.asax Commented Jul 21, 2020 at 9:29
  • 1
    stackoverflow.com/q/46156649/30594 Commented Jul 21, 2020 at 9:30

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.