ASP.NET Core  

Deploying AI Models to Azure App Services with ASP.NET Core

AI and machine learning models have become a core part of modern applications, enabling recommendations, predictions, and personalization. Once you have trained your models locally, the next step is to deploy them in a scalable, reliable environment. Azure App Services is a fully managed platform that allows you to host web APIs, machine learning endpoints, and applications without managing servers.

In this article, we will explore how to deploy AI models to Azure App Services using ASP.NET Core, including best practices, environment configuration, and scaling considerations.

1. Understanding the Deployment Architecture

When deploying AI models, the architecture typically includes:

Components:

  1. ASP.NET Core API

    • Serves as a REST endpoint for AI predictions

    • Hosts your ML models (ML.NET, ONNX, or external AI API integration)

  2. AI Model Files

    • Could be trained ML.NET .zip models

    • ONNX models (.onnx) for pre-trained frameworks

    • External cloud APIs (optional)

  3. Azure App Services

    • Provides managed hosting

    • Supports scaling, monitoring, and SSL

    • Can integrate with Azure Storage for model files

High-Level Flow:

User/App -> HTTP Request -> ASP.NET Core API -> AI Model -> Response

2. Preparing the ASP.NET Core Application

Step 1: Create a Web API Project

dotnet new webapi -n AIModelWebAPI
cd AIModelWebAPI

Step 2: Add Required Packages

For ML.NET:

dotnet add package Microsoft.ML
dotnet add package Microsoft.ML.Data

For ONNX Runtime:

dotnet add package Microsoft.ML.OnnxRuntime

Step 3: Organize AI Models

Folder structure:

AIModelWebAPI/
  Models/
    sentiment_model.zip
    recommendation_model.onnx

In production, you can store models in Azure Blob Storage and download them on startup.

Step 4: Implement AI Service

Services/AIService.cs

using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;

public class AIService
{
    private readonly MLContext _mlContext;
    private readonly ITransformer? _mlNetModel;
    private readonly InferenceSession? _onnxSession;

    public AIService()
    {
        _mlContext = new MLContext();

        // Load ML.NET model
        if (File.Exists("Models/sentiment_model.zip"))
        {
            _mlNetModel = _mlContext.Model.Load("Models/sentiment_model.zip", out _);
        }

        // Load ONNX model
        if (File.Exists("Models/recommendation_model.onnx"))
        {
            _onnxSession = new InferenceSession("Models/recommendation_model.onnx");
        }
    }

    public string PredictSentiment(string input)
    {
        if (_mlNetModel == null) return "Model not found";

        var engine = _mlContext.Model.CreatePredictionEngine<SentimentData, SentimentPrediction>(_mlNetModel);
        var result = engine.Predict(new SentimentData { Text = input });
        return result.Prediction ? "Positive" : "Negative";
    }

    public float[] PredictRecommendation(float[] inputData)
    {
        if (_onnxSession == null) return Array.Empty<float>();

        var inputTensor = new DenseTensor<float>(inputData, new int[] { 1, inputData.Length });
        var inputs = new List<NamedOnnxValue> { NamedOnnxValue.CreateFromTensor("input", inputTensor) };

        using var results = _onnxSession.Run(inputs);
        return results.First().AsEnumerable<float>().ToArray();
    }
}

Models/SentimentData.cs

using Microsoft.ML.Data;

public class SentimentData
{
    [LoadColumn(0)]
    public string Text { get; set; } = string.Empty;
}

public class SentimentPrediction
{
    [ColumnName("PredictedLabel")]
    public bool Prediction { get; set; }

    public float Score { get; set; }
    public float Probability { get; set; }
}

Step 5: Create API Controller

using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("api/[controller]")]
public class AIController : ControllerBase
{
    private readonly AIService _aiService;

    public AIController(AIService aiService)
    {
        _aiService = aiService;
    }

    [HttpPost("sentiment")]
    public IActionResult Sentiment([FromBody] string text)
    {
        var prediction = _aiService.PredictSentiment(text);
        return Ok(new { prediction });
    }

    [HttpPost("recommendation")]
    public IActionResult Recommendation([FromBody] float[] input)
    {
        var prediction = _aiService.PredictRecommendation(input);
        return Ok(prediction);
    }
}

3. Prepare for Azure Deployment

Step 1: Configure for Production

Program.cs / Startup.cs:

var builder = WebApplication.CreateBuilder(args);

// Add services
builder.Services.AddSingleton<AIService>();
builder.Services.AddControllers();

var app = builder.Build();

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

appsettings.Production.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  }
}

Step 2: Test Locally

dotnet run

Check endpoints:

  • POST /api/ai/sentiment

  • POST /api/ai/recommendation

4. Deploy to Azure App Services

Step 1: Create App Service

  1. Login to Azure CLI:

az login
  1. Create a Resource Group:

az group create --name AIModelRG --location eastus
  1. Create App Service Plan:

az appservice plan create --name AIModelPlan --resource-group AIModelRG --sku B1 --is-linux
  1. Create Web App:

az webapp create --resource-group AIModelRG --plan AIModelPlan --name AIMLWebAPI --runtime "DOTNET|7.0"

Step 2: Publish ASP.NET Core API

  1. Publish locally:

dotnet publish -c Release -o ./publish
  1. Deploy using Azure CLI:

az webapp deploy --resource-group AIModelRG --name AIMLWebAPI --src-path ./publish

Step 3: Configure Model Files

If using large AI models, upload them to Azure Blob Storage:

var blobClient = new BlobContainerClient(connectionString, containerName);
var modelBlob = blobClient.GetBlobClient("sentiment_model.zip");
await modelBlob.DownloadToAsync("Models/sentiment_model.zip");

Download models at startup to avoid bundling large files in your deployment package.

5. Best Practices for Production

  1. Environment Variables

    • Store API keys, Blob Storage connection strings, and other secrets in Azure App Settings

  2. Scaling

    • Use App Service scale-out for high load

    • Consider Azure Kubernetes Service for very high concurrency

  3. Logging and Monitoring

    • Enable Application Insights

    • Track model prediction latency and errors

  4. Security

    • Use HTTPS

    • Restrict API endpoints with authentication (Azure AD, JWT)

    • Protect model files and sensitive data

  5. Model Versioning

    • Maintain different model versions in Blob Storage

    • Load version based on configuration

  6. Async Processing

    • For heavy inference, consider background queues or Azure Functions

    • Prevent blocking HTTP requests

6. Optional Enhancements

  • Auto-scaling AI endpoints using Azure App Service or AKS

  • GPU acceleration for ONNX or TensorFlow models using Azure VM or Azure ML

  • Real-time prediction APIs with WebSocket or SignalR

  • CI/CD pipeline using GitHub Actions or Azure DevOps

7. Folder Structure

AIModelWebAPI/
  Controllers/
    AIController.cs
  Models/
    SentimentData.cs
    SentimentPrediction.cs
  Services/
    AIService.cs
  Models/
    sentiment_model.zip
    recommendation_model.onnx
  Program.cs
  appsettings.json
  appsettings.Production.json

8. Conclusion

Deploying AI models to Azure App Services provides a fully managed, scalable, and secure environment for production-ready AI applications. Key takeaways:

  • Separate AI service logic from controllers

  • Use ML.NET or ONNX for model integration

  • Store large model files in Azure Blob Storage

  • Monitor, scale, and secure endpoints for production

  • Automate deployments using Azure CLI or CI/CD pipelines

With this setup, developers can confidently serve AI predictions from the cloud, enabling features like:

  • Personalized recommendations

  • Sentiment analysis

  • Predictive analytics

  • Intelligent dashboards

Azure App Services makes it simple to deploy, manage, and scale AI-powered APIs in a production environment.