Showing posts with label Where to Configure Ports in .NET Core. Show all posts
Showing posts with label Where to Configure Ports in .NET Core. Show all posts

Tuesday, October 7, 2025

🔑 Where to Configure Ports in .NET Core

 There are 3 main ways:


1️⃣ launchSettings.json (Local Development Only)

  • Located in:
    Properties/launchSettings.json

Example:

{ "profiles": { "ProductService": { "commandName": "Project", "dotnetRunMessages": true, "applicationUrl": "http://localhost:5002;https://localhost:7002", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } }

👉 This means:

  • ProductService will listen on http://localhost:5002 and https://localhost:7002.

  • Useful for local debugging.


2️⃣ appsettings.json or appsettings.{Environment}.json

  • You can configure Kestrel endpoints directly.

Example:

{ "Kestrel": { "Endpoints": { "Http": { "Url": "http://localhost:5002" }, "Https": { "Url": "https://localhost:7002" } } } }

👉 This works in development, staging, and production.
👉 More flexible, especially when deploying in Docker/Kubernetes.


3️⃣ Hardcode in Program.cs (Not Recommended for Production)

var builder = WebApplication.CreateBuilder(args); builder.WebHost.UseUrls("http://localhost:5002", "https://localhost:7002"); var app = builder.Build(); app.MapControllers(); app.Run();

👉 Quick way to bind to specific ports, but harder to manage in large deployments.


⚙️ Best Practices

  1. Local Development → use launchSettings.json.

  2. Production (IIS, Azure, Docker, Kubernetes)

    • Use appsettings.json (Kestrel:Endpoints).

    • Or environment variables → DOTNET_URLS=http://+:5002.

  3. Dockerized Microservices → expose ports in Dockerfile / docker-compose.yml. Example:

    EXPOSE 5002
    ports: - "5002:5002"

✅ Summary

  • launchSettings.json → local dev only.

  • appsettings.json (Kestrel) → preferred for production.

  • Program.cs → UseUrls() → quick overrides.

  • Environment Variables / Docker → best in containerized/cloud setups.

Blog Archive

Don't Copy

Protected by Copyscape Online Plagiarism Checker

Pages