Microsoft Learn

Deploying ASP.NET Core Applications

Deploying your ASP.NET Core application is the process of making it available to users. This involves packaging your application and its dependencies, configuring the server environment, and managing the application's lifecycle.

ASP.NET Core offers flexibility in deployment, allowing you to choose the most suitable approach based on your project's requirements and your target environment. This tutorial covers various deployment options, common target platforms, and recommended strategies.

Understanding the difference between self-contained and framework-dependent deployments is crucial for efficient resource management and update strategies.

Deployment Options

ASP.NET Core applications can be deployed in two primary ways:

Self-Contained Deployments

In a self-contained deployment, the application includes a specific version of the .NET runtime along with its dependencies. This means the target machine does not need to have a pre-installed .NET runtime. This approach provides greater control over the runtime version but results in larger deployment packages.

dotnet publish -c Release --self-contained true -r win-x64

Framework-Dependent Deployments

A framework-dependent deployment requires a compatible version of the .NET runtime to be installed on the target machine. The application package only includes the application code and its dependencies. This results in smaller deployment packages and simplifies runtime updates, as the runtime can be updated independently of the application.

dotnet publish -c Release --self-contained false

Target Environments

ASP.NET Core applications can be deployed to a variety of environments:

Windows

Windows offers several hosting options, including IIS (Internet Information Services), Kestrel (the built-in cross-platform web server), and hosting within a Windows Service.

Linux

Linux is a popular choice for hosting ASP.NET Core applications. Common web servers like Nginx and Apache are often used as reverse proxies in front of Kestrel.

macOS

While less common for production server environments, macOS can be used for development and testing. Kestrel is the primary web server for hosting ASP.NET Core apps on macOS.

Containers

Containerization using Docker is a highly recommended approach for modern ASP.NET Core deployments. Containers provide an isolated, consistent, and portable environment for your application.

Docker deployment diagram

Deployment Strategies

Various web servers and platforms can be used to serve your ASP.NET Core application:

Internet Information Services (IIS)

IIS is a robust web server for Windows that can host ASP.NET Core applications. It typically involves installing the ASP.NET Core Hosting Bundle and configuring the IIS site.

Nginx

Nginx is a high-performance web server often used as a reverse proxy on Linux systems. It forwards requests to Kestrel, which runs as a background process.

Apache

Similar to Nginx, Apache can be configured as a reverse proxy for ASP.NET Core applications running on Kestrel.

Azure App Service

Azure App Service provides a fully managed platform for deploying and scaling web applications. It simplifies many aspects of deployment and infrastructure management.

Docker

Deploying your ASP.NET Core application in Docker containers offers portability, consistency, and efficient resource utilization. This involves creating a Dockerfile to build your container image.

# Example Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish -c Release -o /app/publish

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS final
WORKDIR /app
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "YourAppName.dll"]

Configuration

Application configuration (e.g., connection strings, API keys) should be managed securely and independently of the application code. This can be achieved using:

  • Environment variables
  • Configuration files (e.g., appsettings.json, appsettings.Production.json)
  • Azure Key Vault or other secret management services

Best Practices

  • Use Release Builds: Always deploy release builds for optimal performance and smaller deployment sizes.
  • Environment-Specific Configuration: Leverage environment-specific configuration files (e.g., appsettings.Production.json).
  • Secure Secrets: Never commit sensitive information directly into your code repository. Use environment variables or secret management services.
  • Health Checks: Implement health check endpoints to monitor the application's status.
  • Logging: Configure robust logging to help diagnose issues in production.
  • Reverse Proxy: Use a web server like Nginx or IIS as a reverse proxy for Kestrel to handle SSL termination, load balancing, and static file serving.
  • CI/CD: Automate your build, test, and deployment processes using Continuous Integration and Continuous Deployment (CI/CD) pipelines.

Next Steps

Explore the detailed guides for deploying to specific platforms:

Deploy to Azure App Service

Learn how to deploy your ASP.NET Core application to Azure's managed platform.

Learn More

Deploy to Docker

Master containerizing your ASP.NET Core application for consistent deployments.

Learn More

Deploy to IIS

Understand the steps for hosting ASP.NET Core applications on Windows with IIS.

Learn More