Black Friday Deals For Authors Are On Now!

Leanpub Header

Skip to main content

Building AI Agents with C# and .NET 10

A Developer’s First Guide to the Microsoft Agent Framework

Building AI Agents with C# and .NET 10
This book is 100% completeLast updated on 2026-11-29

Your agent works in the demo. This book is about the week after: the payment made twice, the answer citing a replaced policy, the conversation another tenant can read. With the Microsoft Agent Framework 1.28 you’ll build agents in C# that authorize their actions, ground their answers in evidence, survive a restart and ship with a record of what was tested. 24 chapters. 177 companion projects with offline self-tests. Just C# and .NET 10.

Minimum price

$29.99

$39.99

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

Buying multiple copies for your team? See below for a discount!

PDF
Discussion Forum
About

About

About the Book

A working agent takes about twenty lines of C#. The trouble starts once it’s attached to something that matters. A reimbursement is paid twice because a timeout made the orchestration retry an approved call. An answer cites last year’s travel policy. A user in one tenant reads another tenant’s conversation by sending its id. None of those failures lives in the model. They live in the code around it, which is the code you own.

Building AI Agents with C# and .NET 10, Second Edition is about that code. The first edition showed you how to make an agent work. This one shows you how to tell whether it keeps working: when the model is wrong, when the network drops between an approval and a payment, when the process dies while a claim waits for review. Twenty-four chapters in six parts, on Microsoft Agent Framework 1.28, .NET 10 LTS and C# 14.

You will learn how to:

•         Decide whether a task needs an agent at all, and when a plain function does the job better

•         Count tokens for real, pin the SDK and the packages, and keep every secret out of the repository

•         Build your first ChatClientAgent together with its first test, against a fake model that needs no key

•         Get typed C# records back from the model, and check that the record describes something possible

•         Give an agent conversations that survive a restart, belong to one person and can be deleted

•         Add tools with real brakes on the loop, and turn a model’s proposal into an action that application policy authorizes and commits once

•         Serve tools over the Model Context Protocol across trust boundaries, without letting a visible tool become a callable one

•         Ground answers in documents the user is allowed to see, keep a replaced policy out of today’s answer, and read receipts into typed fields

•         Choose a multi-agent topology by measurement, draw a review as a typed workflow graph, and resume a claim in another process after a crash

•         Host agents behind validated identities with tenant isolation, and build streaming interfaces where an approval is a decision about exact arguments

•         Trace a wrong answer to the step that produced it, enforce security policy where the effect happens, and evaluate with the uncertainty printed next to the score

•         Price the work, find the load at which the service stops keeping its promises, qualify a release candidate, and keep it working while models and data change

 

Along the way you build Contoso’s expense assistant through eight versions, from a first agent with a fake model to a release candidate with a qualification dossier that states what was tested and what wasn’t. Payments are simulated: no sample moves real money.

What makes this book different is proof. Every printed output was copied from a captured run. Most samples have a --self-test mode that runs against a scripted chat client, needs no model and no key, prints RESULT n/n and fails when a check fails, so you can check a chapter’s claims on your own machine. The samples that call a model were run against a local Ollama. The Azure OpenAI, OpenAI, Anthropic and Microsoft Foundry paths compile and pass their offline tests, but the book says plainly that they weren’t run against those services. Every chapter ends with three exercises (Basic, Intermediate, Challenge) with acceptance criteria, and the companion repository holds 177 projects: 122 chapter samples, 47 exercise solutions and the 8 Contoso versions.

Written for .NET developers who have shipped an ASP.NET Core service or a console tool and now have to put an agent into a system that already has users. No machine-learning background required. Read the first edition? The Introduction gives you a path through what changed, and Appendix E maps the old project names to the new ones. Coming from Semantic Kernel or AutoGen? Appendix C is your map, with the full migration guides in the repository.

Buying for a team? Volume licenses are available from 3 copies, with team pricing up to 40% off for larger groups: one purchase, one invoice, a copy for everyone.

The companion code lives at https://github.com/RachidD68/building-ai-agents-with-csharp-and-dotnet-10

Team Discounts

Team Discounts

Get a team discount on this book!

  • Up to 3 members

    Minimum price
    $69.00
    Suggested price
    $92.00
  • Up to 5 members

    Minimum price
    $109
    Suggested price
    $149
  • Up to 10 members

    Minimum price
    $209
    Suggested price
    $279
  • Up to 15 members

    Minimum price
    $299
    Suggested price
    $399
  • Up to 25 members

    Minimum price
    $449
    Suggested price
    $599

Bundle

Bundles that include this book

Author

About the Author

Rachid DAHIR

I design systems for the long haul.

As a software architect, technical author, and educator based in Morocco, I've spent more than a decade building enterprise-grade .NET solutions in financial services, healthcare, and large-scale enterprise environments — where reliability, performance, and clean architecture are not optional.

I write for developers who are ready to move past tutorials. My books share a deliberate standard: production-grade code only — no toy examples, no shortcuts that would never survive a code review; every concept explained, demonstrated, then practiced; every advanced topic taught with the same patience as an introductory one — because complexity is never an excuse for a poor explanation.

Whether you're a mid-career developer leveling up, a solution architect consolidating best practices, or an enterprise team standardizing modern .NET, my goal stays the same: clarity without compromise.

When I'm not writing code or prose, I explore mathematics, AI research, and natural health.

Contents

Table of Contents

Front Matter

  • Preface
  • Introduction
  • About the Author

Part I — Understand and Start

  1. Choosing the Right Amount of Agency
    • The Contoso Scenario
    • How It Works
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next
  2. Models, Context and Capabilities
    • The Contoso Scenario
    • How Models Read and Write
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next
  3. A Reproducible Development Environment
    • The Contoso Scenario
    • The Reference Environment
    • Choosing a Provider
    • How Configuration Is Resolved
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next
  4. Your First Agent and Its First Test
    • The Contoso Scenario
    • How It Works
    • The Minimal Solution
    • Streaming
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next

Part II — Contracts and Authorized Actions

  1. Typed Responses and Domain Validation
    • The Contoso Scenario
    • How Typed Responses Work
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next
  2. Conversations, Memory and Data Ownership
    • The Contoso Scenario
    • How Conversations Work
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next
  3. Tools and Bounded Execution
    • The Contoso Scenario
    • How Tools Work
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next
  4. Authorizing and Committing Agent Actions
    • The Contoso Scenario
    • How It Works
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next

Part III — Knowledge and Documents

  1. MCP Across Trust Boundaries
    • The Contoso Scenario
    • How It Works
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next
  2. Building a Grounded RAG Baseline
    • The Contoso Scenario
    • How It Works
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next
  3. RAG With Permissions, Freshness and Evidence
    • The Contoso Scenario
    • How It Works
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next
  4. Reliable Multimodal Document Processing
    • The Contoso Scenario
    • How It Works
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next

Part IV — Composition and Durable Execution

  1. Choosing a Multi-Agent Topology
    • The Contoso Scenario
    • How It Works
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next
  2. Workflows, Branches and Human Review
    • The Contoso Scenario
    • How It Works
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next
  3. Durable Execution and External Effects
    • The Contoso Scenario
    • How It Works
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next
  4. Skills, Harnesses and Capability Lifecycle
    • The Contoso Scenario
    • How It Works
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next

Part V — Interfaces, Security and Observation

  1. Hosting, Identity and Tenant Isolation
    • The Contoso Scenario
    • How It Works
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next
  2. Streaming Interfaces and Approval Experiences
    • The Contoso Scenario
    • How It Works
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next
  3. Observability, Quality and Incident Diagnosis
    • The Contoso Scenario
    • How It Works
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next
  4. Security Policies and Governance
    • The Contoso Scenario
    • How It Works
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next

Part VI — Qualification and Operations

  1. Evaluation With Evidence and Uncertainty
    • The Contoso Scenario
    • How It Works
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next
  2. Cost, Latency and Capacity Engineering
    • The Contoso Scenario
    • How It Works
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next
  3. Qualifying the Contoso Agent System
    • The Contoso Scenario
    • How It Works
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • What’s Next
  4. Operating and Evolving Agents
    • The Contoso Scenario
    • How It Works
    • The Minimal Solution
    • Where It Fails
    • Proving It
    • The Production Variant
    • Summary
    • Key Terms
    • Practice Exercises
    • Your Next Steps

Appendices

  • Appendix A — C# Essentials for AI Development
  • Appendix B — Package Manifest and Qualification Scope
  • Appendix C — Migrating from Semantic Kernel and AutoGen
  • Appendix D — Glossary
  • Appendix E — Exercise Solutions and Project Mapping
  • Appendix F — Troubleshooting by Symptom
  • Appendix G — References and Evidence
  • Keyword Index

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub