Deploying ASP.NET Core Applications

This module covers the essential steps and considerations for deploying your ASP.NET Core applications to various environments. Learn how to build, configure, and publish your app for production.

Deployment Targets

ASP.NET Core applications are highly flexible and can be deployed to a wide range of hosting environments. Here are some common targets:

  • Azure App Service: Microsoft's fully managed platform-as-a-service (PaaS) offering for hosting web applications.
  • IIS (Internet Information Services): A popular web server for Windows environments.
  • Docker Containers: Package your application for consistent deployment across different environments.
  • Linux Servers: Deploy your cross-platform ASP.NET Core apps on Nginx or Apache.
  • Self-Contained Deployments: Bundle the .NET runtime with your application.
  • Framework-Dependent Deployments: Rely on a pre-installed .NET runtime on the target machine.

Key Deployment Concepts

1. Publishing

Publishing is the process of creating a deployable artifact from your project. This typically involves building your application and copying the necessary files to a specified output directory.

You can publish using the .NET CLI:

dotnet publish -c Release -o ./publish

This command builds the project in the Release configuration and outputs the published files to the ./publish folder.

2. Configuration

Configuration management is crucial for adapting your application to different environments (development, staging, production). ASP.NET Core provides a flexible configuration system that supports various sources.

Common configuration sources include:

  • JSON configuration files (e.g., appsettings.json, appsettings.Production.json)
  • Environment variables
  • Command-line arguments
  • Azure Key Vault

3. Environment Variables

Environment variables are a standard way to configure applications in cloud and containerized environments. ASP.NET Core automatically reads configuration values from environment variables.

For example, to set the database connection string:

--ConnectionStrings:DefaultConnection="Server=myServer;Database=myDb;User Id=myUser;Password=myPassword;"

This can be set as an environment variable named ConnectionStrings:DefaultConnection.

4. Docker Deployment

Docker allows you to package your application and its dependencies into a container, ensuring consistency across environments.

Create a Dockerfile in your project's root directory:

# Use an official .NET SDK image as a build base image
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /app

# Copy the project file and restore dependencies
COPY *.csproj ./
RUN dotnet restore

# Copy the rest of the application code
COPY . ./

# Publish the application for the Release configuration
RUN dotnet publish -c Release -o out

# Use a smaller runtime image for the final image
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS runtime
WORKDIR /app
COPY --from=build /app/out .

ENTRYPOINT ["dotnet", "YourAppName.dll"]

Build the Docker image:

docker build -t my-aspnet-app .

Run the Docker container:

docker run -p 8080:80 my-aspnet-app

Next Steps

Explore these resources to deepen your understanding of ASP.NET Core deployment: