Search code examples
c#linuxasp.net-web-api.net-core-2.0kestrel

Why I get a 500 only on kestrel?


I got a ASP.NET Core 2.0 WEB-API and it should work on kestrel because it will be hostet there. When I start with Kestrel (look at my launchSettings below) there is a POST and I always get a 500 returning without going into the code. Means I leave breackpoints everywhere and when I execute the POST in Swagger no breakpoint is hit. When I use IIS instead it works fine. The 500 comes straight away. 500 comes also after deployment on Linux Kestrel.

I implement @Startup.cs:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.Use((context, next) =>
        {
            context.Response.Headers.Remove("Server");
            return next();
        });

@Program.cs:

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseUrls("http://0.0.0.0:5000")
            .UseIISIntegration()
            .UseApplicationInsights()
            .UseKestrel()
            .Build();

@launchSettings.json:

"Kestrel": {
  "commandName": "Project",
  "launchBrowser": true,
  "launchUrl": "",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
}

With Kestrel a POST call should process the Controller method with all it's logic the same like with IIS.


Solution

  • I got it working with

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseIISIntegration()
                .UseApplicationInsights()
                .UseKestrel(options =>
                {
                    options.Listen(IPAddress.Parse("0.0.0.0"), 5000);
                })
                .Build();
    

    and the launchsetting:

        "CZKestrel": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:59883/"
    }
    

    for local debugging.

    To get it working as Service I did dotnet restore and dotnet build, that I copied only the folder with the created dll to a other place and than run / start the service. I guess when I start it I should be inside or just one folder above the dll folder. Now it works.