Production-Grade RAG
with C# and .NET
Building Retrieval-Augmented Generation Systems
The complete guide to building RAG systems on the Microsoft stack — from an 80-line Hello World to a fully deployed, audited, EU AI Act-aware Azure pipeline.
PDF. Lifetime updates. Read a free sample on Leanpub before you buy.
About this book
Most RAG material is written for Python. This book is written for you: a .NET developer with a few years of C# experience who wants to ship something real with your company’s data.
Across seven parts, twenty-five chapters and 668 pages, you build Contoso SmartDocs, a running enterprise project that grows chapter by chapter into a production system spanning six document silos — powered by the Microsoft Agent Framework 1.16, Microsoft.Extensions.AI, Azure AI Search, Qdrant, Neo4j, and .NET 10 / C# 14.
Every chapter ships runnable code in the companion repository. Every snippet traces to a specific file. Every sample compiles; all 458 tests pass on a clean clone. The architecture is deliberately clean — small, named interfaces with swappable implementations — so what you learn maps onto the system you need to build, not just the one in the book.
This is the production-concerns RAG book the .NET ecosystem has been missing. If you can write C#, you can ship RAG.
What you’ll learn
Build the core pipeline component by component: embeddings, chunking with Anthropic’s Contextual Retrieval, multimodal content, vector databases, indexing strategies, hybrid retrieval with RRF, reranking, and first-class SSE streaming.
Add query intelligence: metadata filters, query construction, routing, and conversational multi-turn rewriting.
Go beyond vectors with graph databases, hybrid storage, GraphRAG, LazyGraphRAG, and Vectorless RAG.
Serve retrieval as a tool over the Model Context Protocol and orchestrate multi-agent RAG workflows with agentic memory.
Ship it for real: a 100-query golden eval gate on every pull request, latency and cost optimization, drift and model migration, prompt-injection defense, grounded citations, EU AI Act audit logging, and a full Bicep-deployed Azure capstone.
Built on
Table of contents
Seven parts, twenty-five chapters, nine appendices.
Front matter
- Preface
- How to Read This Book
- Read This First — RAG Vocabulary at a Glance
- About the Author
Part I — Foundations
- The AI Landscape
- The .NET Toolkit for RAG Development
Part II — The RAG Pipeline
- Embeddings: Turning Text into Vectors
- Chunking and Contextual Retrieval
- Multimodal RAG
- Vector Databases
- Indexing Strategies
- The Retriever
- Reranking
- The Complete RAG Pipeline
Part III — Query Intelligence
- Metadata Filtering and Query Construction
- Query Routing and Conversational Queries
Part IV — Graph and Hybrid Storage
- Graph Databases for RAG
- Hybrid Databases
Part V — RAG Design Patterns
- Classic RAG Enhancements
- Vectorless RAG
- GraphRAG and LazyGraphRAG
- Model Context Protocol
- Agentic, Multi-Agent, and Memory
Part VI — Production Concerns
- Evaluation and Metrics
- Latency, Cost, and Performance
- Freshness, Drift, and Migration
- Security
- Trust by Design
Part VII — Capstone Project
- Production Capstone
Appendices
- A — Practice Exercise Solutions
- B — Design Pattern Quick Reference
- C — Vector Database Comparison
- D — Embedding Benchmark
- E — Python → .NET Rosetta
- F — Companion Repository Guide
- G — Debugging Checklist
- H — Prompt Engineering Patterns
- I — The Math Behind RAG
- Index
Inside the book
Code in the text is not pseudocode. Every listing comes from the companion repository and traces back to a named file.
// Retrieve, then generate — the shape every chapter builds on.
public sealed class RagPipeline
{
private readonly IRetriever _retriever;
private readonly IChatClient _chat;
public async Task<RagResponse> AskAsync(
string question, CancellationToken ct = default)
{
var hits = await _retriever.RetrieveAsync(question, TopK, ct);
var prompt = _promptTemplate.Build(question, hits);
var reply = await _chat.GetResponseAsync(prompt, ct);
return new RagResponse(
Answer: reply.Text ?? "",
Sources: hits.Select(h => h.Citation).ToArray());
}
}
Read the free sample on Leanpub
Companion repository
MIT-licensed. Builds clean from a fresh clone.
- Projects
- 52
- Passing tests
- 458
- Eval gate
- 100 queries
- Licence
- MIT
Who this is for
- .NET developers with a few years of C# who need to ship RAG against real company data.
- Engineers arriving from LangChain or LlamaIndex who want the .NET equivalents.
- Architects responsible for evaluation, security, cost and compliance of an AI feature.
What it is not
- An introduction to C#. You should be able to read and write it fluently.
- A machine-learning textbook. You will not be training models.
- A Python book with C# examples bolted on. There are no Python detours.
Frequently asked
No. Every sample is C# on .NET 10. Appendix E is a Python-to-.NET Rosetta Stone if you are arriving from LangChain or LlamaIndex.
Not for most of the book. The pipeline chapters run against local Qdrant and Neo4j in Docker. Azure AI Search and the Bicep-deployed capstone are the parts that need a subscription, and they are clearly marked.
None. The book is written for a .NET developer with a few years of C#. Appendix I covers the mathematics behind RAG for readers who want it, but no chapter depends on it.
Yes. The repository holds 52 projects and 458 passing tests, and every snippet in the book traces to a specific file. The suite is verified against a clean clone.
If you have never built an agent in C#, start with Building AI Agents with C# and .NET 10. If you already have an agent and need to ground it in your own documents, start here. The two are sold together as a bundle.
Buy it with the Agents book and save 15%
Both books together for $54.99, against $64.98 apart. 1,268 pages, 44 chapters, 174 projects.
Production-Grade RAG with C# and .NET
668 pages. 25 chapters. One evolving enterprise project, every line runnable on .NET 10.
Get it on Leanpub — from $34.99Not ready to buy? Start with the free book.