Home > Enterprise >  Start ASP.NET Core Web API from a Console App
Start ASP.NET Core Web API from a Console App

Time:12-02

I want to integrate a ASP.NET Core WebApi into a solution which consists of many other projects. (I use dotnet 6.0) Within the solution the WebAPI is started as Thread by the Main project. The problem is that if I run the WebApi from the other project only an empty WebApi is started. (Default Ports are used, not the configured one and there are no controllers...)

Can anybody help me?

This is a minimal soltution with the same problem I discribed above:

Solution structure

The WebApi is the default VisualStudio 2022 project. I only made small changes in the Program.cs file. This is the Code of the WebApi Project's Program.cs file:

namespace WebApi;

public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        // Add services to the container.

        builder.Services.AddControllers();
        // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
        builder.Services.AddEndpointsApiExplorer();
        builder.Services.AddSwaggerGen();

        var app = builder.Build();

        // Configure the HTTP request pipeline.
        if (app.Environment.IsDevelopment())
        {
            app.UseSwagger();
            app.UseSwaggerUI();
        }

        app.UseHttpsRedirection();

        app.UseAuthorization();

        app.MapControllers();

        app.Run();
    }
}

Code of the Programm.cs of the Main Project:

using WebApi;

internal class Program
{
    private static void Main(string[] args)
    {
        Console.WriteLine("Hello, World!");
        WebApi.Program.Main(Array.Empty<string>());
    }
}

launchSettings.json

{
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:56423",
      "sslPort": 44385
    }
  },
  "profiles": {
    "WebApi": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "https://localhost:7257;http://localhost:5257",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

This is how it looks if I run the WebApi from the Main project:

Hello, World!
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\data\Experimental\CallWebApiFromOtherProject\Main\bin\Debug\net6.0\

(Controllers and Swagger is not available. Also the ports are not the ones defined above. -> Only a Webserver without anything)

What I did wrong?

CodePudding user response:

What you are trying to achieve is well documented here (and it is called as selfhosting) -

enter image description here

  • Related