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:
ASP.NET Core API
Serves as a REST endpoint for AI predictions
Hosts your ML models (ML.NET, ONNX, or external AI API integration)
AI Model Files
Could be trained ML.NET
.zipmodelsONNX models (
.onnx) for pre-trained frameworksExternal cloud APIs (optional)
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

Join the conversation! Your thoughts help the community grow.