6

I need to know how to create API's dynamically by calling another API then passing [API name] and [API parameters], let's we assume that we have this API, like the one shown here:

Calling API

[HttpPost("GenerateApi")]
[ProducesResponseType(200)]
[ProducesResponseType(500)]
public IActionResult GenerateApi(RootObject ro)
{           
   //Here I need piece of code to create API dynamically
   return Ok(new { ro.apiName, ro.parameters });       
}

Here are the models:

public class RootObject
{
   public string apiName { get; set; }
   public List<parameter> parameters { get; set; }
}

public class parameter
{
   public string parameterName { get; set; }
   public dynamic parameterType { get; set; }
}

Now what needs to be done, is to create dynamic API in controller.

So then we would have the API created they can be called from their URL like this

www.example.com/api/v1/[controller]/[apiName]/{[parameter1_value]}/{[parameter2_value]}

Can you please provide me some insight of this.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ahmed Heasat
  • 256
  • 1
  • 6
  • 22
  • 2
    Sounds like a really bad idea..... what's the point of this? You should follow the **SOLID** principles of system design - and the **S** there stands for **Single Responsibility Principle** which says a piece of code should do **one thing** and **one thing only**. – marc_s Mar 08 '20 at 08:24
  • Thank you for your comment, I want to let user create his API dynamically by calling this API [GenerateApi], if you have another good idea or Suggestion please help @marc_s – Ahmed Heasat Mar 08 '20 at 08:37
  • 3
    This was answered here: https://stackoverflow.com/questions/40522242/how-to-dynamically-create-web-apis-rest-apis-in-selfhosted-owin-server – Richard Mar 08 '20 at 08:45
  • Thank you for your help, I will read the document @Richard – Ahmed Heasat Mar 08 '20 at 09:07
  • 1
    You could use query parameters. There is some code to do this and generate the object using `ExpandoObject` in [this](https://github.com/KevinDockx/ImplementingAdvancedRESTfulConcernsAspNetCore3/tree/master/Finished%20sample/CourseLibrary/CourseLibrary.API) repo. There is a very good course to go along with it, if you can swing the sub cost it would be worth imo. – Tyler Hundley Mar 09 '20 at 03:45
  • Thanks for help .. put this project in .net core 3. @TylerHundley – Ahmed Heasat Mar 09 '20 at 08:01
  • I'm not familiar with .net core 3, Is it very different from .net core 2?@TylerHundley – Ahmed Heasat Mar 09 '20 at 08:09

1 Answers1

0

Found some thing can help in MVC. In case somebody else is looking for this, I need to overwrite the DefaultHttpControllerSelector. Here is a very nice article on the subject: link So basically for my use case mentioned above, I need to create a new AppDomain, start my service in it, and load my assemblies dynamically at runtime. I finally need to overwrite the DefaultHttpControllerSelector to catch the request. When the request arrive, it have then control on which controller I want to use. There I can update the controller dynamically by loading a new assembly, etc. Main thing to be careful on is that this is executed for each request so it could easily impact performance and memory. So I will implement my own caching of controller.

Ahmed Heasat
  • 256
  • 1
  • 6
  • 22