Understanding Azure Personalizer with .NET
Imagine building a .NET web application that adapts content or recommendations to each user in real-time — like showing personalized product recommendations, suggesting articles, or tailoring offers.
That’s exactly what Azure Personalizer helps you achieve. It uses reinforcement learning to pick the best choice from a set of options and improves over time based on user feedback.
In this blog, we’ll walk through:
What is Azure Personalizer?
How to integrate it into a .NET API
Sample code to make your app smarter and more user-friendly
What is Azure Personalizer?
Azure Personalizer is a cloud-based AI service that:
Takes a set of choices (called rankings)
Uses context data (user profile, time, device, etc.)
Ranks them based on reinforcement learning
Returns the top choice with the highest predicted reward
Accepts user feedback (reward) to improve future rankings
https://medium.com/@dotnetfullstackdev
It’s perfect for personalized recommendations — from product suggestions to dynamic UI layout.
Imagine you’re building an e-commerce website selling electronics, books, and clothing. You want to recommend products to users on the homepage — ideally showing products that are most relevant to them at that moment.
BEFORE Personalizer
How Recommendations Were Made
Static Recommendations
A simple “Top 10” list based on sales.
Same for every user — no personalization.
Example:
Wireless Earbuds
Bestseller Book
Trendy Jacket
Basic Segmentation
Some sites might segment users into buckets (e.g., by region or device).
Example:
If the user is on a mobile device, show mobile accessories.
If they are a repeat customer, show loyalty deals.
Problems
Recommendations often felt irrelevant (e.g., a book lover seeing kitchenware).
Couldn’t adapt to real-time context (e.g., time of day, season).
Couldn’t learn from user feedback (e.g., no idea if a user actually clicked or bought).
AFTER Personalizer
How Recommendations Work Now
Dynamic Ranking
When a user lands on the homepage, the app sends a list of product options to Azure Personalizer with context:
Device type: mobile/desktop
Time of day: morning/evening
Weather/location: cold/rainy
Past behavior: last purchase category
Real-Time Learning
Personalizer uses reinforcement learning to rank the product options.
Returns the product with the highest predicted relevance.
User Feedback
If the user clicks on the product, the app sends a reward score (e.g., 1.0) to Personalizer.
If the user ignores it, a lower score or even no reward is sent.
Continuous Improvement
Over time, Personalizer learns:
Mobile users in the morning prefer quick reads (e.g., audiobooks).
Desktop users in the evening prefer tech gadgets.
Rainy days push raincoats higher in the ranking.
Step 1 — Install the Personalizer SDK
In your .NET project, install:
dotnet add package Azure.AI.Personalizer
Step 2 — Configure Personalizer in appsettings.json
{
"AzurePersonalizer": {
"Endpoint": "https://<your-personalizer-resource>.cognitiveservices.azure.com/",
"ApiKey": "<your-api-key>"
}
}
Step 3 — Create a Personalizer Client
using Azure;
using Azure.AI.Personalizer;
using Azure.AI.Personalizer.Models;
public class PersonalizerService
{
private readonly PersonalizerClient _client;
public PersonalizerService(IConfiguration configuration)
{
string endpoint = configuration["AzurePersonalizer:Endpoint"];
string apiKey = configuration["AzurePersonalizer:ApiKey"];
var credential = new AzureKeyCredential(apiKey);
_client = new PersonalizerClient(new Uri(endpoint), credential);
}
public async Task<PersonalizerRankResult> RankAsync()
{
var actions = new List<PersonalizerRankableAction>
{
new PersonalizerRankableAction(id: "1", features: new Dictionary<string, object> { { "productCategory", "electronics" } }),
new PersonalizerRankableAction(id: "2", features: new Dictionary<string, object> { { "productCategory", "books" } }),
new PersonalizerRankableAction(id: "3", features: new Dictionary<string, object> { { "productCategory", "clothing" } })
};
var contextFeatures = new List<object>
{
new { timeOfDay = "morning", deviceType = "mobile" }
};
var rankRequest = new PersonalizerRankOptions(actions, contextFeatures);
var result = await _client.RankAsync(rankRequest);
return result.Value;
}
public async Task RewardAsync(string eventId, double value)
{
await _client.RewardAsync(eventId, value);
}
}
Step 4 — Expose the API Endpoint
[ApiController]
[Route("api/personalize")]
public class PersonalizerController : ControllerBase
{
private readonly PersonalizerService _personalizerService;
public PersonalizerController(PersonalizerService personalizerService)
{
_personalizerService = personalizerService;
}
[HttpGet("rank")]
public async Task<IActionResult> GetRecommendation()
{
var result = await _personalizerService.RankAsync();
return Ok(new
{
TopActionId = result.RewardActionId,
EventId = result.EventId
});
}
[HttpPost("reward")]
public async Task<IActionResult> SendReward([FromQuery] string eventId, [FromQuery] double value)
{
await _personalizerService.RewardAsync(eventId, value);
return Ok("Reward submitted.");
}
}
How it works
1️⃣ GET /api/personalize/rank —
Calls Azure Personalizer with a set of actions and context.
Returns the top choice and an
EventId
.
2️⃣ POST /api/personalize/reward?eventId=xyz&value=1.0 —
Sends user feedback on the selected choice.
Personalizer uses this reward to improve future decisions.
Best Practices
✅ Always include rich context — the more data Personalizer has, the better it can learn.
✅ Set up telemetry and logging — so you can track API calls, rewards, and improvements.
✅ Use try/catch to handle API failures gracefully.
✅ Start small — with a few actions — and expand as you gain confidence.
Conclusion
Azure Personalizer brings AI-powered recommendations to your .NET apps with minimal effort. By integrating it into an API layer, you can:
Provide personalized experiences
Improve user engagement
Continuously learn and adapt to your users