For years, AI felt like something only Python developers could build.
Every tutorial started with Jupyter notebooks, TensorFlow, and dozens of packages that backend developers had never heard of.
Today, that's no longer true.
If you're already building applications with ASP.NET Core, AI is becoming another capability you can add to your application rather than an entirely new field to learn.
The good news is that Microsoft has built an ecosystem that allows developers to stay inside C# while adding intelligent features to their applications.
In this article, we'll look at three approaches every .NET developer should know:
- ML.NET for training custom models
- ONNX for consuming existing models
- Azure AI for plug-and-play intelligence
Option 1: Build Your Own Models with ML.NET
ML.NET allows developers to train and consume machine learning models entirely inside .NET.
Install it first.
dotnet add package Microsoft.ML
Define your data model
public class SentimentData
{
public string Text { get; set; } = string.Empty;
public bool Label { get; set; }
}
public class SentimentPrediction
{
public bool PredictedLabel { get; set; }
public float Probability { get; set; }
public float Score { get; set; }
}
Train a model
using Microsoft.ML;
var context = new MLContext();
var data = context.Data.LoadFromTextFile<SentimentData>(
"data.csv",
hasHeader: true,
separatorChar: ',');
var pipeline = context
.Transforms
.Text
.FeaturizeText(
"Features",
nameof(SentimentData.Text))
.Append(
context.BinaryClassification
.Trainers
.SdcaLogisticRegression(
labelColumnName: "Label",
featureColumnName: "Features"));
var model = pipeline.Fit(data);
context.Model.Save(
model,
data.Schema,
"sentiment_model.zip");
Make a prediction
var engine = context.Model.CreatePredictionEngine<
SentimentData,
SentimentPrediction>(model);
var result = engine.Predict(
new SentimentData
{
Text = "I love this product"
});
Console.WriteLine(
$"Prediction: {result.PredictedLabel}");
Option 2: Use ONNX Models
Sometimes training a model is unnecessary.
You simply want to use one that already exists.
Install the runtime.
dotnet add package Microsoft.ML.OnnxRuntime
Load an ONNX model.
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
var session =
new InferenceSession(
"model.onnx");
var tensor =
new DenseTensor<float>(
new[] { 1f, 2f, 3f },
new[] { 1, 3 });
var inputs =
[
NamedOnnxValue.CreateFromTensor(
"input",
tensor)
];
using var results =
session.Run(inputs);
This approach is great when performance matters or when data scientists already provide trained models.
Option 3: Add AI Without Building Models
Sometimes developers overcomplicate AI.
You don't always need machine learning.
Sometimes you simply need intelligent features.
Azure AI provides services for:
- OCR
- Translation
- Speech recognition
- Sentiment analysis
- Document extraction
Example:
using Azure;
using Azure.AI.TextAnalytics;
var client =
new TextAnalyticsClient(
endpoint,
new AzureKeyCredential(key));
var response =
client.AnalyzeSentiment(
"The service was excellent.");
Console.WriteLine(
response.Value.Sentiment);
No training required.
Just consume the API.
How I Choose Between Them
Over time, I started using a simple rule.
ML.NET
When my application needs to learn from my own data.
ONNX
When a trained model already exists.
Azure AI
When I simply need intelligent features quickly.
You don't have to pick only one.
Many applications combine all three.
Final Thoughts
The biggest mindset shift is realizing that AI is no longer a separate world.
It's becoming another layer of software development.
Just like databases.
Just like caching.
Just like Docker.
As .NET developers, we no longer have to leave our ecosystem to build intelligent applications.
And honestly, that's probably the most exciting part.
The barrier to entry has never been lower.
Top comments (0)