Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET 5 Multiple projects in the same assembly

I have a solution with multiple projects inside. One of them are for a dashboard web application and one of them is for an API project. Is it possible to run these two application in the same assembly?

For example the web app would run on the "localhost:5001" and the API project would run on the "localhost:5001/api/". If that is possible would they share the same memory cache manager?

like image 603
Alper Alpdoğan Avatar asked Apr 17 '26 16:04

Alper Alpdoğan


1 Answers

Yes, you are absolutely able to run both an API and Web App on the same Assembly even if they are from different projects. Not only can you do it but it is very quick and easy to do and can do it for many projects.

Since we are using .NET5 in this example we are going to assume you have two console applications. One for your WebApp and one for your API.

Step 1: Choose your Entry point

Choose the project you want to Luanch from. In this case lets choose the WebApp. For all the other projects convert them from console application to class library and delete all program.cs files.

Step 2: Add References to these class files

Self explanitory.

Step 3: Call all the Startup.cs files in the class library from both the ConfigureServices and Configure functions.

See code below:

Add a variable to your WebApp's Startup.cs folder for the other Startup.cs files and Initialize:

    private MyWebApi.Startup ApiStartUp { get; } 
    public IConfiguration Configuration { get; }

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
        ApiStartUp = new MyWebApi.Startup(Configuration);
    }
    

Now call the registration functions from their counterpart functions.

    public void ConfigureServices(IServiceCollection services)
    {
        ApiStartUp.ConfigureServices(services);

        // ... Rest of code
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        ApiStartUp.Configure(app, env);
        
        // ... Rest of code
    }
like image 125
CorrieJanse Avatar answered Apr 21 '26 17:04

CorrieJanse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!