I want to make some microservices with asp.net core (Web Api) For service discovery I want use Consul where I must register the services via the HTTP API including IP and port.
My question is: How can I know which port is used within in a service? Is there a way to determine the used port via code?
Change Port To change the port the application is using, Open the file lunchSetting. json. You will find it under the properties folder in your project and as shown below. Inside the file, change applicationUrl port (below is set to 5000) to a different port number and save the file.
We know that ASP.NET Core Application runs under Kestrel Server with default port number http://localhost:5000.
Click on the Dropdown on the run button. Now click on debug properties. By clicking on that launch profile window will open. now you can change the port from the app URL from here.
In Solution Explorer, click the name of the application. In the Properties pane, click the down-arrow beside Use dynamic ports and select False from the dropdown list. This will enable editing of the Port number property. In the Properties pane, click the text box beside Port number and type in a port number.
Yeah, Use HttpContext.Connection.LocalPort
to get the local port that the server is listening on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With