Programming APIs with C# and .NET: Develop high-performance APIs that ensure seamless application communication and enhanced security
By Jesse Liberty and Joseph Dluzen
()
Jesse Liberty
Jesse Liberty is the bestselling author of "Programming ASP.NET", "Programming C#", and a dozen other books on web and object oriented programming. As president of Liberty Associates, Inc., he provides contract programming, consulting and on-site training in ASP.NET, C#, C++ and related topics. Jesse has been a Distinguished Software Engineer at AT & T and Vice President for technology development at CitiBank.
Related to Programming APIs with C# and .NET
Related ebooks
Minimal APIs in ASP.NET 9: Design, implement, and optimize robust APIs in C# with .NET 9 Rating: 0 out of 5 stars0 ratingsAPI Development Made Easy: A Practical Guide with Examples Rating: 0 out of 5 stars0 ratingsBuilding an API Product: Design, implement, release, and maintain API products that meet user needs Rating: 0 out of 5 stars0 ratingsREST API Design Control and Management Rating: 4 out of 5 stars4/5Modern API Design: REST, GraphQL, and Beyond Rating: 0 out of 5 stars0 ratings.NET MAUI Cookbook: Build a full-featured app swiftly with MVVM, CRUD, AI, authentication, real-time updates, and more Rating: 0 out of 5 stars0 ratingsSystems Programming with C# and .NET: Building robust system solutions with C# 12 and .NET 8 Rating: 0 out of 5 stars0 ratingsRESTful API Design - Best Practices in API Design with REST: API-University Series, #3 Rating: 5 out of 5 stars5/5The Beginner’s Guide to APIs Rating: 0 out of 5 stars0 ratingsPragmatic Microservices with C# and Azure: Build, deploy, and scale microservices efficiently to meet modern software demands Rating: 0 out of 5 stars0 ratingsArchitecting ASP.NET Core Applications: An atypical design patterns guide for .NET 8, C# 12, and beyond Rating: 0 out of 5 stars0 ratingsAPI Testing and Development with Postman: API creation, testing, debugging, and management made easy Rating: 0 out of 5 stars0 ratingsPython APIs: From Concept to Implementation Rating: 5 out of 5 stars5/5Hands-On Microservices with JavaScript: Build scalable web applications with JavaScript, Node.js, and Docker Rating: 0 out of 5 stars0 ratingsFastAPI Cookbook: Develop high-performance APIs and web applications with Python Rating: 0 out of 5 stars0 ratingsMastering the Art of C# Programming: Unraveling the Secrets of Expert-Level Programming Rating: 0 out of 5 stars0 ratingsBuilding Scalable Web Apps with Node.js and Express Rating: 0 out of 5 stars0 ratingsComprehensive Guide to Swagger and OpenAPI: Definitive Reference for Developers and Engineers Rating: 0 out of 5 stars0 ratingsFastAPI Essentials: Definitive Reference for Developers and Engineers Rating: 0 out of 5 stars0 ratingsJSON: API in Practice Rating: 0 out of 5 stars0 ratingsSoftware Architecture with Kotlin: Combine various architectural styles to create sustainable and scalable software solutions Rating: 0 out of 5 stars0 ratingsBeginning C# 6 Programming with Visual Studio 2015 Rating: 0 out of 5 stars0 ratingsMastering RESTful API Development with Go Rating: 0 out of 5 stars0 ratingsSpring Boot 3.0 Cookbook: Proven recipes for building modern and robust Java web applications with Spring Boot Rating: 0 out of 5 stars0 ratingsHallo .NET 8.0: Practical ASP.NET Core Minimal API Rating: 0 out of 5 stars0 ratings
Internet & Web For You
The Digital Marketing Handbook: A Step-By-Step Guide to Creating Websites That Sell Rating: 5 out of 5 stars5/5The $1,000,000 Web Designer Guide: A Practical Guide for Wealth and Freedom as an Online Freelancer Rating: 4 out of 5 stars4/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5More Porn - Faster!: 50 Tips & Tools for Faster and More Efficient Porn Browsing Rating: 3 out of 5 stars3/5How to Be Invisible: Protect Your Home, Your Children, Your Assets, and Your Life Rating: 4 out of 5 stars4/5Coding For Dummies Rating: 4 out of 5 stars4/5The Beginner's Affiliate Marketing Blueprint Rating: 4 out of 5 stars4/5SEO For Dummies Rating: 4 out of 5 stars4/5Ultimate guide for being anonymous: Avoiding prison time for fun and profit Rating: 5 out of 5 stars5/5Notion for Beginners: Notion for Work, Play, and Productivity Rating: 4 out of 5 stars4/548 Really Useful Web Sites Rating: 5 out of 5 stars5/5An Ultimate Guide to Kali Linux for Beginners Rating: 3 out of 5 stars3/5Cybersecurity For Dummies Rating: 5 out of 5 stars5/5Beginner's Guide To Starting An Etsy Print-On-Demand Shop Rating: 0 out of 5 stars0 ratingsKill All Normies: Online Culture Wars From 4Chan And Tumblr To Trump And The Alt-Right Rating: 3 out of 5 stars3/5From Nothing Rating: 5 out of 5 stars5/52022 Adobe® Premiere Pro Guide For Filmmakers and YouTubers Rating: 5 out of 5 stars5/5Everybody Lies: Big Data, New Data, and What the Internet Can Tell Us About Who We Really Are Rating: 4 out of 5 stars4/5Surveillance and Surveillance Detection: A CIA Insider's Guide Rating: 3 out of 5 stars3/5No Place to Hide: Edward Snowden, the NSA, and the U.S. Surveillance State Rating: 4 out of 5 stars4/5UX/UI Design Playbook Rating: 4 out of 5 stars4/5Social Engineering: The Science of Human Hacking Rating: 3 out of 5 stars3/5
Reviews for Programming APIs with C# and .NET
0 ratings0 reviews
Book preview
Programming APIs with C# and .NET - Jesse Liberty
Programming APIs with C# and .NET
Copyright © 2024 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Associate Group Product Manager: Kunal Sawant
Book Project Manager: Prajakta Naik
Publishing Product Manager: Srishti Seth
Book Coordinator: Manisha Singh
Senior Editor: Rounak Kulkarni
Technical Editor: Vidhisha Patidar
Copy Editor: Safis Editing
Indexer: Rekha Nair
Production Designer: Gokul Raj S.T
Business Development Executive: Sonia Chauhan
First published: November 2024
Production reference:1251024
Published by Packt Publishing Ltd.
Grosvenor House
11 St Paul’s Square
Birmingham
B3 1RB, UK.
ISBN 978-1-83546-885-2
www.packtpub.com
To my 106-year-old mother, my wife, and especially my children. I am a very lucky man.
– Jesse Liberty
To my wife, children, and parents.
– Joseph Dluzen
Contributors
About the authors
Jesse Liberty specializes in C#, Git, and building world-class APIs. He hosts the popular Yet Another Podcast (https://jesseliberty.com/podcast/
) and is the author of more than two dozen best-selling programming books (https://packt.link/FVtEr
).
Jesse was a Technical Evangelist for Microsoft, Distinguished Software Engineer at AT&T; Software Architect for PBS, and Vice President of Information Technology at Citibank. He was also on the teaching staff at Brandeis University. Jesse has been a Microsoft MVP for 13 years.
Jesse is a recognized expert and has spoken at conferences worldwide. His website is https://jesseliberty.com
.
I have so many people to thank, starting with my co-author, Joe Dluzen, without whom this book would literally have been impossible. I’d also like to thank all the people at Packt, especially Rounak, Prajakta, Kunal, and Vidhisha who stuck in there with us, as well as the technical reviewer. The quality of this book is due to their diligence, though any mistakes are ours. I’d also like to thank my boss for his encouragement and for teaching me so much about creating world-class enterprise APIs. Finally, as ever, I wish to thank my wife, without whose patience and encouragement I could never have written any of my books.
Joseph Dluzen has been working in C# and associated frameworks for almost 20 years. He is currently in a software architecture role for one of the world’s largest agricultural machine manufacturers. Formerly a senior consultant at Accenture, he has experience in a wide range of industries, including government identity, autonomous vehicle tooling, and enterprise auction management systems.
I would like to thank Jesse, without whom this book would never have happened.
About the reviewer
Matheus de Campos , based out of Brazil, is a software engineer with a Bachelor's degree in Computer Science. He has extensive international experience and specializes in Microsoft technologies. Currently, he works as a consultant for tech organizations, focusing on delivering impactful solutions that optimize technology strategies.
Table of Contents
Preface
1
Getting Started
Technical requirements
Where does the API fit in?
The players
Getting set up
Downloading the free software you need
Summary
You try it
2
What We’ll Build
Technical requirements
What is an API and what is it for?
Creating the database
The application
Creating the Car Table
Database structure
Car object
The ASP.NET application
Program.cs
Connecting to the database
Folders
Summary
You try it
3
Implementing with REST
Technical requirements
REST
Client/server
Using web protocols
Statelessness and caching
Implementing REST in ASP.NET Core
DTOs
Dapper
Examining the SQL
Putting it all together (inserting a car)
Creating the body in Postman
Get all
Update
Soft delete
Summary
You try it
4
Documentation with Swagger
Technical requirements
Setting up the Swagger documentation
Swagger for the controller
Swagger out of the box
Running your API in Swagger
param tag
Response codes
Summary
You try it
5
Data Validation
Technical requirements
Tuning your API
Paging
Validation
Capturing errors
Built-in validators
Summary
You try it
6
Azure Functions
Technical requirements
Understanding Functions
Hosting
Application packaging
Billing
Project walkthrough
Starting up
Options
Routing
Deploying
Azure resource creation
Publishing from Visual Studio
Post-deployment reconfiguration
Summary
You try it
7
Azure Durable Functions
Technical requirements
Overview of Durable Functions
Orchestrators
Activities
Programming and debugging walk-through
Additional options
Exceptions
Summary
You try it
8
Advanced Topics
Technical requirements
Implementing an advanced API
Updating the database
Creating the classes
Supporting implementation
Azure Storage Tables
Exploring the essentials
TableModel
StorageTableService
Populating the table in Azure
Introducing Microsoft Aspire
Sample program
Exploring the Aspire dashboard
What have you learned, Dorothy?
Adding Aspire to an existing app
Getting oriented with our new project
Adding Aspire
Summary
You try it
9
Authentication and Authorization
Technical requirements
Introduction to authentication and authorization
Overview of A&A
Enabling authentication on existing functions
Programmatically accessing your API
Authorizing additional Azure Resources
Summary
You try it
10
Deploying to Azure
Technical requirements
Getting started
Using tests to ensure code quality
Using the same binaries
Deploying from DevOps
Importing sample code
Creating the build pipeline
Publishing an artifact
Deploying your artifact to Azure
Continuous deployment
Testing the release pipeline
End-to-end testing
Summary
You try it
Congratulations!
11
What’s Next?
C#
SQL
Database
API architecture
Dapper
AutoMapper
Visual Studio
Git
Housing your version control
Summary
Index
Other Books You May Enjoy
Preface
Building a good Application Program Interface (API) is essential for creating real-world applications that display data from a data source (most often a database). The job of the API, as you’ll see in this book, is the separation of concerns on a large scale; specifically separating the concerns of the frontend from the backend. This allows you to change one (e.g., swap out a new database) without breaking the other (e.g., a website) or vice versa.
A typical enterprise might have multiple teams working on the same overall product. For example, you might have a team working on a web presentation of your data, another working on the iOS version, and a third working on Android.
On the backend, you might have multiple data sources. In the simple example that we use in this book, we examine a car dealership. It might have input from sales, but also from inventory, from a service that provides information on average prices, and so forth. Some of this is static data easily stored in a database, some must be in a frequently updated cache, and some must be obtained on demand.
Coordinating the frontend with the backend is difficult and subject to catastrophic breakage should, for example, the format or calculations in the backend change. Furthermore, the needs of the presentation level are almost guaranteed to change over time. Finally, the frontend is most often not the ideal place to put your business logic.
APIs solve these problems. The frontend talks to well-defined endpoints, and the backend responds with well-defined data. What the backend does to get and manipulate that data is invisible to the frontend. For that matter, the uses that the frontend puts that data to are invisible to the backend. Most importantly, the API itself doesn’t need to know about either; it just knows what is being asked for and how to get it.
Who this book is for
This book is targeted at programmers with at least a working knowledge of C# who want to create world-class APIs, often for enterprise applications. It assumes no prior experience with APIs, though a working acquaintance with SQL will be helpful.
What this book covers
Chapter 1
, Getting Started, provides a quick start to get everything set up locally for development.
Chapter 2
, What We’ll Build, provides an overview of an API in general, and how to use it to decouple a frontend and backend system.
Chapter 3
, Implementing with REST, provides an overview of best practices and an opinionated design for general API development that is used throughout the industry.
Chapter 4
, Documentation with Swagger, shows you how to enable and surface documentation based on Swagger.
Chapter 5
, Data Validation, provides an overview of how to validate API calls, including custom validation with a widely used library.
Chapter 6
, Azure Functions, provides an alternative cloud-first hosting framework that not only can be used for APIs but also as a starting point for much more. It also illustrates best practices and then builds upon them to allow runtime configuration without redeployment.
Chapter 7
, Azure Durable Functions, provides an overview of the simplification that results when following a few small design rules in a stateful, scalable system.
Chapter 8
, Advanced Topics, provides a straightforward, cost-effective, cloud logging implementation. Additionally, it goes over advanced scenarios such as complex object mapping, an opinionated cloud-first design tool, and the creation and use of storage tables.
Chapter 9
, Authentication and Authorization, provides an out-of-the-box solution for cloud-first authentication scenarios, including authorizing Azure and non-Azure clients.
Chapter 10
, Deploying to Azure, gets you set up quickly to iterate with Continuous Delivery and Continuous Integration (CI/CD) pipelines.
Chapter 11
, What’s Next?, gives you practical advice on the classic question: Now what?
To get the most out of this book
You will need a working knowledge of at least the basics of C#. An acquaintance with SQL is helpful but not required. The use of Git and repositories in general will make your life easier. We