C
C#โ€ข15mo ago
James213

โœ… Frontend unable to connect to webapi when starting server manually (dotnet run)

When I run my C# Webapi project through Visual Studio Code debugger (or VS) then my reactapp can make a GET request to the webapi, however if I start up the webapi via cmd dotnet run and make a GET request I get net::ERR_CONNECTION_REFUSED This is the output from VS Code when I start the webapi through the debugger.
* Executing task: C:\Program Files\dotnet\dotnet.exe build C:\Users\Username\source\repos\Username\InsuranceCalculator/InsuranceCalculator.sln /property:GenerateFullPaths=true /consoleloggerparameters:NoSummary

MSBuild version 17.6.1+8ffc3fe3d for .NET
Determining projects to restore...
All projects are up-to-date for restore.
webapi -> C:\Users\Username\source\repos\Username\InsuranceCalculator\webapi\bin\Debug\net7.0\webapi.dll
* Terminal will be reused by tasks, press any key to close it.
* Executing task: C:\Program Files\dotnet\dotnet.exe build C:\Users\Username\source\repos\Username\InsuranceCalculator/InsuranceCalculator.sln /property:GenerateFullPaths=true /consoleloggerparameters:NoSummary

MSBuild version 17.6.1+8ffc3fe3d for .NET
Determining projects to restore...
All projects are up-to-date for restore.
webapi -> C:\Users\Username\source\repos\Username\InsuranceCalculator\webapi\bin\Debug\net7.0\webapi.dll
* Terminal will be reused by tasks, press any key to close it.
This is the output from cmd when I start the webapi through the cmd.
C:\Users\Username\source\repos\Username\InsuranceCalculator\webapi>dotnet run
Building...
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5206
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\Users\Username\source\repos\Username\InsuranceCalculator\webapi
C:\Users\Username\source\repos\Username\InsuranceCalculator\webapi>dotnet run
Building...
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5206
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\Users\Username\source\repos\Username\InsuranceCalculator\webapi
22 Replies
cap5lut
cap5lutโ€ข15mo ago
can u show details about the request itself? url and port most i can think of 4 issues here: - wrong port/scheme - wrong hostname - firewall is blocking the connection (tho this would be a bit weird cuz it should block the server itself then which would be shown in the logs of the dotnet run command)
James213
James213OPโ€ข15mo ago
GET https://localhost:7045/api/insurancecalculator/getCalculation/%7B%22bill%22:2000,%22billOccurrence%22:5,%22premiums%22:1000,%22cover%22:80,%22returnPA%22:10,%22duration%22:10%7D net::ERR_CONNECTION_REFUSED
GET https://localhost:7045/api/insurancecalculator/getCalculation/%7B%22bill%22:2000,%22billOccurrence%22:5,%22premiums%22:1000,%22cover%22:80,%22returnPA%22:10,%22duration%22:10%7D net::ERR_CONNECTION_REFUSED
const testResponse = await fetch('https://localhost:7045/api/insurancecalculator/getCalculation/' + JSON.stringify(extractedData)); It says 7045 but both methods of connecting to server listen on 5206 and when I try to start the server via the debugger (which works) when I have the cmd method running it complains that 5206 is being used.
phaseshift
phaseshiftโ€ข15mo ago
Well obviously the request needs to be sent to a port the server is listening on. Also sounds like you have another server running - check task manager etc
James213
James213OPโ€ข15mo ago
It complains that 5206 is being used (when I do debug method) if I run dotnet run via cmd first. Otherwise, if the debug method is just running alone then it is using 5026 but is able to make the GET request which has port 7045, hence I don't see the issue being here.
cap5lut
cap5lutโ€ข15mo ago
when i do debug method
what do u mean by this? starting via dotnet run and then pressing the "Start Debug" button in visual studio? also its most likely occupying both ports, one for http and one for https
phaseshift
phaseshiftโ€ข15mo ago
Default Configs would normally pick consecutive ports for http/https More likely they're running different profiles without realising
cap5lut
cap5lutโ€ข15mo ago
i cant test right now, wouldnt asp.net core also log both ports when running refering to this log
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5206
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5206
James213
James213OPโ€ข15mo ago
That is the debug method of starting the server (which works), and dotnet run in cmd is the other method (which doesn't) Oh, I ran netstat -an | find "LISTENING" in cmd and the ports 5206 and 7045 show up via the debug method but only 5206 via the cmd method. 7045 isn't starting.
cap5lut
cap5lutโ€ข15mo ago
well, it certainly sounds like u first start via dotnet run and then (while its still running) try to start the debug process and thus it complains the ports are already taken so 7045 is definitively the https port. can u show ur application settings json files? dont forget to censor/remove any tokens or other sensible data first
James213
James213OPโ€ข15mo ago
Yes, that was me just testing to see if the same ports 5206 via both methods were both being used. The problem is 7045 is not also running via cmd.
{
"profiles": {
"http": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5206"
},
"https": {
"commandName": "Project",
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7045;http://localhost:5206"
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
},
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:12041",
"sslPort": 44329
}
}
}
{
"profiles": {
"http": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5206"
},
"https": {
"commandName": "Project",
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7045;http://localhost:5206"
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
},
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:12041",
"sslPort": 44329
}
}
}
I'm not sure if anything is sensitive info.
cap5lut
cap5lutโ€ข15mo ago
nah its not what happens if ya run dotnet run -c Debug (might have to be Development)? maybe its just not using http due to release configuration and thats the whole issue
James213
James213OPโ€ข15mo ago
No difference to dotnet run
cap5lut
cap5lutโ€ข15mo ago
hmm then im out of ideas right now, would have to have access to a computer w/ vs and dotnet installation, and i probably wont have within the next 24h ๐Ÿ˜’
James213
James213OPโ€ข15mo ago
Ok, no worries. Thanks for your help anyways. When I run with Visual Studio (not Code) I get this output. Contains 7045.
info: Microsoft.Hosting.Lifetime[14]
Now listening on: https://localhost:7045
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5206
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\Users\Username\source\repos\Username\InsuranceCalculator\webapi
info: Microsoft.Hosting.Lifetime[14]
Now listening on: https://localhost:7045
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5206
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\Users\Username\source\repos\Username\InsuranceCalculator\webapi
cap5lut
cap5lutโ€ข15mo ago
yeah for any reason the dotnet run doesnt use the https configuration as well, my guess was because of "ASPNETCORE_ENVIRONMENT": "Development" and it may be running in release configuration (hence the tries with Debug/Development)
cap5lut
cap5lutโ€ข15mo ago
@James213 https://github.com/dotnet/sdk/issues/29569 this seems to be ur issue
GitHub
dotnet run command not listening on https ยท Issue #29569 ยท dotnet/s...
Describe the bug I am running a simple webapi program on Windows and when I try to run the 'dotnet run' cli command, I can see it's listening only on http but not on https. I have also ...
cap5lut
cap5lutโ€ข15mo ago
so if u would switch the order of the http and the https profile it would already be enough
James213
James213OPโ€ข15mo ago
It works! dotnet run --launch-profile https
C:\Users\Username\source\repos\Username\InsuranceCalculator\webapi>dotnet run --launch-profile https
Building...
info: Microsoft.Hosting.Lifetime[14]
Now listening on: https://localhost:7045
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5206
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\Users\Username\source\repos\Username\InsuranceCalculator\webapi
C:\Users\Username\source\repos\Username\InsuranceCalculator\webapi>dotnet run --launch-profile https
Building...
info: Microsoft.Hosting.Lifetime[14]
Now listening on: https://localhost:7045
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5206
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\Users\Username\source\repos\Username\InsuranceCalculator\webapi
Thank you
cap5lut
cap5lutโ€ข15mo ago
glad i could help, a bit of using a search engine sometimes works wonders ๐Ÿ˜‚
James213
James213OPโ€ข15mo ago
lol been using chatgpt, though to be fair it did talk about profiles which I was going to try to do later, that I just found out about now.
cap5lut
cap5lutโ€ข15mo ago
tbh, chatgpt isnt that useful in most cases. its relatively good for beginner stuff. i used it alot for my getting-started-with-avalonia stuff because there werent many resources around, the answers were still quite broken, but i was able to adapt. for such cases it is relatively good, but for details or anything beyond their time of fetching internet resources (iirc that was 2021) its doing more damage than anything and dotnet 7 was released last year, so it doesnt have any valuable information regarding that if ur question is now fully answered please dont forget to mark this thread as answered by $close ing it
MODiX
MODiXโ€ข15mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server