A Practical Guide for Enterprise-Grade AI Integration
Machine Learning (ML) is now a core functionality in many enterprise applications. Whether you are building recommendation systems, fraud detection pipelines, forecasting modules, image recognition, or text classification, integrating ML models inside your existing ASP.NET Core applications gives you the advantage of real-time decision-making close to the application layer.
However, the way you integrate ML models into ASP.NET Core depends on multiple factors:
Type of model (ONNX, TensorFlow, PyTorch, ML.NET-trained).
Performance requirements (latency, concurrency).
Deployment strategy (in-process, out-of-process, microservices).
Hardware acceleration (CPU, GPU).
Scalability and maintainability.
This article presents a practical, production-focused guide covering integration methods for ML models in ASP.NET Core applications using ML.NET, ONNX Runtime, Python microservices, and cloud-hosted models.
1. Architecture Approaches for ML in ASP.NET Core
There are four main patterns:
1 In-Process Model Hosting
The ML model executes within the ASP.NET Core process.
Pros
Lowest latency
Easy to deploy
Good for small ML models
Cons
Not suitable for GPU models
If the model crashes, the entire app may crash
Heavy models slow down request processing
Suitable for
Small ONNX models
ML.NET models
Simple classical ML use cases
2. Out-of-Process Hosting (Sidecar or Worker Process)
Your ASP.NET Core app communicates with another process on the same machine.
Pros
Better isolation
Can run GPU-accelerated Python models
Prevents ASP.NET Core from restarting if model process fails
Cons
Higher latency
Extra deployment complexity
Suitable for
Python-based models (TensorFlow, PyTorch)
NVIDIA GPU workloads
3. Microservice-Based Model APIs
ML model runs as an independent service (Docker, Kubernetes, Azure Container Apps).
Pros
Horizontal scaling for inference
Versioned models
CI/CD for model updates
Best for large applications
Cons
Highest infra overhead
Requires service discovery and load balancing
Suitable for
Enterprise AI
Multi-team ownership
Multi-model hosting
4. Cloud-Based External Model APIs
Models hosted in cloud services (Azure ML, AWS Sagemaker, OpenAI, HuggingFace Inference).
Pros
Zero infrastructure
Auto-scaling
High availability
Cons
High latency for large payloads
Recurring cost
Requires network connectivity
Suitable for
NLP, image analysis, embeddings
Prototypes and production AI at scale
2. Integrating ML.NET Models
ML.NET enables training and running .NET-native models.
2.1 Loading the Model
using Microsoft.ML;
public class PredictionEngineService
{
private readonly MLContext _mlContext = new MLContext();
private readonly ITransformer _model;
public PredictionEngineService()
{
_model = _mlContext.Model.Load("models/sentiment.zip", out _);
}
public PredictionEngine<InputData, PredictionResult> CreateEngine()
{
return _mlContext.Model.CreatePredictionEngine<InputData, PredictionResult>(_model);
}
}
2.2 Using Dependency Injection
builder.Services.AddSingleton<PredictionEngineService>();
2.3 Prediction Controller
[ApiController]
[Route("api/predict")]
public class PredictionController : ControllerBase
{
private readonly PredictionEngineService _service;
public PredictionController(PredictionEngineService service)
{
_service = service;
}
[HttpPost]
public ActionResult Predict(InputData input)
{
var engine = _service.CreateEngine();
var result = engine.Predict(input);
return Ok(result);
}
}
Best Practices
Use
PredictionEnginePoolfor concurrent predictions.Avoid reloading model on every request.
Re-train and replace model with hot reload pattern.
3. Integrating ONNX Models with ONNX Runtime
ONNX Runtime is optimized for cross-framework models and supports CPU, GPU, and TensorRT acceleration.
3.1 Installing ONNX Runtime
dotnet add package Microsoft.ML.OnnxRuntime
3.2 Loading ONNX Model
using Microsoft.ML.OnnxRuntime;
public class OnnxModelService
{
private readonly InferenceSession _session;
public OnnxModelService()
{
_session = new InferenceSession("models/model.onnx");
}
public float[] Predict(float[] input)
{
var tensor = new DenseTensor<float>(input, new[] {1, input.Length});
var inputs = new List<NamedOnnxValue>
{
NamedOnnxValue.CreateFromTensor("input", tensor)
};
using var results = _session.Run(inputs);
return results.First().AsEnumerable<float>().ToArray();
}
}
3.3 Exposing ONNX Predictions in ASP.NET Core
[ApiController]
[Route("api/onnx")]
public class OnnxController : ControllerBase
{
private readonly OnnxModelService _service;
public OnnxController(OnnxModelService service)
{
_service = service;
}
[HttpPost]
public ActionResult Predict(InputVector model)
{
var result = _service.Predict(model.Values);
return Ok(result);
}
}
Best Practices
Keep the ONNX session singleton for performance.
Use GPU execution provider if available.
Batch predictions if possible.
4. Integrating Python Models with ASP.NET Core
Most enterprise AI models are still built using Python (TensorFlow, PyTorch, Scikit-learn). Integrating Python-based inference into ASP.NET Core requires careful design.
There are three reliable ways.
Method 1: Python Process Execution (In-process bridge)
Use a Python interpreter hosted next to ASP.NET Core.

Join the conversation! Your thoughts help the community grow.