Cover of Production-Grade RAG with C# and .NET by Rachid Dahir
Retrieval-Augmented Generation

Production-Grade RAG with C# and .NET

Building Retrieval-Augmented Generation Systems

668 pages 25 chapters 9 appendices 458 passing tests

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.

Get it on Leanpub
From $34.99 · suggested $46.99

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

01

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.

02

Add query intelligence: metadata filters, query construction, routing, and conversational multi-turn rewriting.

03

Go beyond vectors with graph databases, hybrid storage, GraphRAG, LazyGraphRAG, and Vectorless RAG.

04

Serve retrieval as a tool over the Model Context Protocol and orchestrate multi-agent RAG workflows with agentic memory.

05

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

.NET 10 LTS C# 14 Microsoft Agent Framework 1.16 Microsoft.Extensions.AI Azure AI Search Qdrant Neo4j Model Context Protocol GraphRAG Bicep

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

  1. The AI Landscape
  2. The .NET Toolkit for RAG Development

Part II — The RAG Pipeline

  1. Embeddings: Turning Text into Vectors
  2. Chunking and Contextual Retrieval
  3. Multimodal RAG
  4. Vector Databases
  5. Indexing Strategies
  6. The Retriever
  7. Reranking
  8. The Complete RAG Pipeline

Part III — Query Intelligence

  1. Metadata Filtering and Query Construction
  2. Query Routing and Conversational Queries

Part IV — Graph and Hybrid Storage

  1. Graph Databases for RAG
  2. Hybrid Databases

Part V — RAG Design Patterns

  1. Classic RAG Enhancements
  2. Vectorless RAG
  3. GraphRAG and LazyGraphRAG
  4. Model Context Protocol
  5. Agentic, Multi-Agent, and Memory

Part VI — Production Concerns

  1. Evaluation and Metrics
  2. Latency, Cost, and Performance
  3. Freshness, Drift, and Migration
  4. Security
  5. Trust by Design

Part VII — Capstone Project

  1. 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
View on GitHub

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.

See the bundle

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.99

Not ready to buy? Start with the free book.