Search code examples
c#.net-coredependency-injectionasp.net-core-webapi.net-6.0

ASP.NET Core 6 Web API base project with multiple API projects


I have a project called backend-sandbox. I want this project to be the startup project of my solution. Once I run this project, it should run all the other projects dependent on it.

Other projects can be of any type.

  • Class Library
  • Web API project
  • etc

If the other project type is Web API, it will have its own configuration, controllers and services registered in it.

For example, I have sub project named securityadmin of type Web API. This project will have its own appsettings.json configuration file. And I want to register the services of this app within its own Program.cs file and not in the root.

But I want this project to run under backend-sandbox.

The whole idea is to have one root server which can support multiple distributed modules.

Is it possible to achieve this? Or I need to follow another approach.

This is the current project structure.

enter image description here


Solution

  • The whole idea is to have one root server which can support multiple distributed modules.

    Is it possible to achieve this? Or I need to follow another approach.

    Well, based on your scenario and description it can be acheived following different ways.

    Not sure what exact reason you have for this kind of implementation. However, you could follow micro-service architecture or even any modular architecture

    The way you have explained your scenario, in that case you could follow modular approach.

    You can have each Web API project (such as securityadmin) with its own configuration, controllers, and services, and still run them under the umbrella of the backend-sandbox.

    you'll need to ensure that each Web API project is configured to run independently and register its services within its own Program.cs file. Then, you can start each Web API project from the backend-sandbox project.

    For instance, you should ensure each Web API project (securityadmin, etc.) is configured properly with its own appsettings.json, controllers, and services registered within its Program.cs file. In the Main method of Program.cs, start each Web API project using Process.Start.

    Alternatively, you could consider micro-service architecture using Ocelot. Which would allow you to implement following architecture: enter image description here

    So basically, every project would have it's own configuration but there would be one entry point. In your case that would be backend-sandbox

    Within your backend-sandbox program.cs file you should have following configuration:

    You need to read the configuration like below:

    builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);
    

    And use the Ocelot middleware:

    await app.UseOcelot();
    

    Within the ocelot.json you should define your endpoint, URL and API.

    enter image description here

    Note: Keep in mind, this is not exactly the solution for your project, I just share the idea how should you proceed. Please refer to this official document for more details implementation.