skip to Main Content

I have created an out of the box ASP.NET Core 6 Web API project.

When I launch the project I can see Swagger loaded, displaying a single WeatherForecast endpoint which works when tested.

enter image description here

However, when I launch the Web API project from another .NET Core app, the controllers are not discovered. Swagger returns the following message for reference. Also manually calling the endpoint Url fails.

enter image description here

After some investigation into this issue, I found that if I register the WeatherForecast controller manually it will become visible in Swagger and accessible when requested.

enter image description here

Why is the WeatherForecast controller not being discovered when running the ASP.NET app from another .NET Core App ?

Source Code: GitHub

2

Answers


  1. Have you tried this:

    builder.Services.AddMvc()
                    .AddApplicationPart(typeof(WeatherForecastController).Assembly)
                    .AddControllersAsServices();
    
    Login or Signup to reply.
  2. Using the answer linked to by @RichardDeeming I was able to get it to work in .Net 6. Find AddControllers in Program.cs and adapt it to look like this (substitute TestServiceController for one of your controllers that is in the target assembly):

    var assembly = typeof(TestServiceController).Assembly;
    
    builder.Services.AddControllers()
                    .PartManager.ApplicationParts.Add(new AssemblyPart(assembly) );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search