3

I have a .NET Core worker project with TCP pipelines and want to add one HTTP controller endpoint to it. I basically

  • created a new worker project (for reproduction purposes)
  • in its csproj file I added <FrameworkReference Include="Microsoft.AspNetCore.App" /> to the item group to gain access to the webbuilder stuff
  • installed the package Microsoft.AspNetCore.Mvc.Core
  • created a Startup class

.

internal class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc(); // not sure if I even need this
        services.AddControllers();
    }

    public void Configure(IApplicationBuilder applicationBuilder)
    {
        applicationBuilder.UseRouting();
        applicationBuilder.UseEndpoints(endpoints => { endpoints.MapControllers(); });
    }
}
  • configured Kestrel in the Program class

.

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webHostBuilder =>
            {
                webHostBuilder.UseKestrel(kestrelServerOptions =>
                {
                    kestrelServerOptions.ListenLocalhost(5000); // this will be a config value
                }).UseStartup<Startup>();
            })
            .ConfigureServices((hostContext, services) =>
            {
                services.AddHostedService<Worker>();
            });
}
  • created a controller for testing purposes

.

[ApiController]
[Route("[controller]")]
internal class UsersController : ControllerBase
{
    [HttpGet]
    public async Task<ActionResult> Test()
    {
        return Ok();
    }
}
  • I run the project and call GET http://localhost:5000/users
  • I would expect a 200 but get a 404 and the debugger does not hit the controller endpoint

Does someone know what I'm missing?

  • Try both HTTP and HTTPS. The S is secure mode and uses TLS for authentication. Usually you would get a 500 error if TLS is wrong. Error 404 is not found but if server ignores the HTTP and need HTTPS than that could explain the 404 error. HTTP defaults to either port 80 or 8080. – jdweng Oct 01 '20 at 12:03
  • 2
    Could you change controller access modifier to public and check? Will it solve your problem? – Sergey Nazarov Oct 01 '20 at 12:13
  • @SergeyNazarov oh damn, changing it to public fixed it :) –  Oct 01 '20 at 12:33
  • @OlafSvenson Is this the right way to add HTTP endpoint to a worker project? – Hary Mar 27 '21 at 16:23

2 Answers2

3

You need to specify the default route on method if you want this to be work

[ApiController]
[Route("[controller]")]
   public class UsersController : ControllerBase
    {
     [Route("")]
     [HttpGet]
     public async Task<ActionResult> Test()
       {
          return Ok();
       } 
    }

And Second thing is make the controller public

Muhammad Aqib
  • 709
  • 7
  • 14
  • 2
    You don't need the `[Route("")]`. It's the access modifier being `internal` that's the problem. The second part of your answer is correct. – John H Oct 01 '20 at 12:25
0

Try ConfigureKestrel instead of UseKestrel configuration method

webBuilder.ConfigureKestrel(serverOptions =>
{
    serverOptions.Listen(IPAddress.Loopback, 5000);
    serverOptions.Listen(IPAddress.Loopback, 5001, 
        listenOptions =>
        {
            listenOptions.UseHttps("cert.pfx", "password");
        });
})
svyat1s
  • 868
  • 9
  • 12
  • 21