Cannot debug in Visual Studio after changing the port number?
Asked Answered
J

2

9

I added the line .UseUrls("http://*:5000") to enable clients from other hosts accessing the web api.

    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .UseUrls("http://*:5000") // Added
            .Build();

        host.Run();
    }

However, using browser to access localhost:5000/api/Test got the error of HTTP/1.1 400 Bad Request? Should the .UseUrls() be only compiled for production?

HTTP/1.1 400 Bad Request
Date: Mon, 08 Aug 2016 21:42:30 GMT
Content-Length: 0
Server: Kestrel

The following messages are copied from Visual studio Output window when testing.

Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http://localhost:5000/api/Test

Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware:Error: 'MS-ASPNETCORE-TOKEN' does not match the expected pairing token '9bca37f2-7eda-4517-9f8f-60b6cc05cf01', request rejected.

Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 8.5976ms 400

Juliettejulina answered 8/8, 2016 at 21:49 Comment(4)
Can you call UseUrls() before UseIISIntegration(). see #37862975Felicific
Please confirm if you were running this inside IIS, IISExpress or stand-alone.Felicific
I moved the UseUrls() in front of UseIISIntegration() and it works when debug run the application name. It still doesn't work if run with IIS express.Juliettejulina
and even after that still same exception or another one?Felicific
F
12

You should call first .UseUrls() and/or .UseConfig() and then .UseIISIntegration().

When running ok under IIS/IISExpress, you end up with 2 processes. IIS listening on the desired port and Kestrel on another one. Your requests should go to IIS and then it is forwarded to Kestrel (with the MS-ASPNETCORE-TOKEN).

The call to .UseIISIntegration() hides this mapping. It actually changes the port in your app and sets IIS on the desired port. But it breaks if you call both methods in incorrect order.

You are getting this error message because Kestrel expected to run behind IIS, and received a direct request. And it noticed that because IIS was not there to inject the MS-ASPNETCORE-TOKEN header.

This issue documents the issue and may solve it in future releases.

Felicific answered 9/8, 2016 at 1:43 Comment(1)
This solved it for me. I had the same issue within an hour of op.Driskell
W
0

Another way to solve it.
Since the error because UseKestrel() and UseIISIntegration(), you can try running debug not using IIS/IIS Express, but choose Kestrel server, it will avoid the error happen.
You can check Properties\launchSettings.json to find out another debug option.

Worser answered 4/11, 2020 at 3:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.