Deploying ASP.NET Core to Azure

Comprehensive guide to publishing your ASP.NET Core applications on Microsoft Azure.

This document provides a detailed walkthrough of deploying ASP.NET Core applications to various services within Microsoft Azure. We will cover common deployment strategies, best practices, and considerations for building scalable and robust web applications in the cloud.

Prerequisites

Deployment Options in Azure

Azure offers several services suitable for hosting ASP.NET Core applications. The best choice depends on your application's needs regarding scalability, manageability, cost, and complexity.

1. Azure App Service

Azure App Service is a fully managed platform that enables you to build, deploy, and scale web apps, mobile back ends, and business domains. It's often the go-to choice for most web applications.

Steps for Deployment to App Service:

  1. Create an App Service: In the Azure portal, create a new App Service. Choose your desired OS (Windows or Linux) and runtime stack.
  2. Publish from Visual Studio:
    • In Visual Studio, right-click on your ASP.NET Core project in Solution Explorer.
    • Select "Publish...".
    • Choose "Azure" as the target.
    • Select "Azure App Service (Windows)" or "Azure App Service (Linux)".
    • Choose your subscription and the App Service you created, or create a new one.
    • Configure any necessary settings (e.g., deployment profile, connection strings).
    • Click "Publish".
  3. Publish using Azure CLI:
    az webapp create --resource-group <your-resource-group> --plan <your-app-service-plan> --name <your-app-name> --runtime "DOTNETCORE:6"
    az webapp deployment source config-zip --resource-group <your-resource-group> --name <your-app-name> --src ./publish.zip

2. Azure Kubernetes Service (AKS)

AKS provides a managed Kubernetes experience in Azure, allowing you to deploy, manage, and scale containerized applications. This is ideal for microservices architectures and complex deployments.

General Steps for AKS Deployment:

  1. Containerize your application: Create a Dockerfile for your ASP.NET Core application.
  2. Build and push the Docker image: Use Azure Container Registry (ACR) or Docker Hub to store your image.
  3. Create an AKS cluster: Provision an AKS cluster through the Azure portal or Azure CLI.
  4. Deploy using Kubernetes manifests: Create .yaml files for deployments, services, and ingress to manage your application within the cluster.

Example Kubernetes Deployment manifest:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: aspnetcore-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: aspnetcore
  template:
    metadata:
      labels:
        app: aspnetcore
    spec:
      containers:
      - name: aspnetcore
        image: <your-acr-or-dockerhub-repo>/aspnetcore-app:latest
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: aspnetcore-service
spec:
  selector:
    app: aspnetcore
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer

3. Azure Container Instances (ACI)

ACI allows you to run Docker containers directly in Azure without managing virtual machines or higher-level orchestrators. It's great for simpler container deployments or development/testing.

Steps for ACI Deployment:

  1. Containerize your application: Create a Dockerfile.
  2. Build and push the Docker image: Store your image in ACR or Docker Hub.
  3. Create a container instance: Use the Azure portal, Azure CLI, or ARM templates to define and deploy your container.

Example Azure CLI command:

az container create --resource-group <your-resource-group> --name <your-container-name> --image <your-image-name>:latest --dns-name-label <your-dns-label> --ports 80

Configuration and Best Practices

Connection Strings

Manage application secrets and connection strings securely. For Azure App Service, use application settings. For AKS, consider Azure Key Vault integration or Kubernetes Secrets.

Environment Variables

Configure your application to read settings from environment variables, which is a cloud-native pattern. ASP.NET Core's configuration system supports this out-of-the-box.

HTTPS Enforcement

Always enforce HTTPS for your web applications. Azure App Service provides easy SSL certificate management.

Logging and Monitoring

Integrate Application Insights for comprehensive logging, performance monitoring, and diagnostics. Ensure your application logs are accessible.

CI/CD Integration

Automate your deployment process using Azure DevOps, GitHub Actions, or other CI/CD tools. This ensures consistent and reliable deployments.