-2

I developed an ASP.Net web api with about 80 different api calls.

I would like to create a dynamic web api client, allowing the application code to remain simple and the "web api client layer" to be reusable (without the need of creating 80 different methods).

I would expect the web api client to look something like this:

public dynamic WebApi(string method, params object[] list)
{
     // Setting the api request by the input...
     // Calling the web api...
     // Handling the results...
}

I'd expect the apps using it to look something like this:

List<Stations> stations = WebApi("GetStationsByCompany", companyId);

Now i feel like this is a very common use case so i shouldn't be developing it myself, however i couldn't find any examples for this scenario online (for .Net Framework).

How should i create a dynamic web api client? What is the best practice for it?

Reginiano
  • 62
  • 10
  • 2
    It is a common problem but what you are describing is an _anti-pattern_ because **1)** clients never know what `operations` are available **2)** clients never know what `parameters are expected` **3)** the server doesn't know what `parameters are legitimate` **4)** the code suffers from a lack of `type safety` **5)** any refactoring performed will break the code - though you won't know until runtime –  Oct 11 '19 at 08:39
  • If you don't want to craft the client proxies by hand, you can code-generate them via _T4 Templates_ –  Oct 11 '19 at 08:42
  • Good points, so the solution is to create 80 different client methods? – Reginiano Oct 11 '19 at 08:47
  • You could, but when faced with this problem I code-generate them via T4 as mentioned. –  Oct 11 '19 at 09:41

1 Answers1

0

if you want to not to do the repetitive task of making client API, I would suggest you try NSwag tools of swagger which has a great facility to accomplish it. Also, I think that there isn't any way to a dynamic proxy that you mentioned.