Explore 1.5M+ audiobooks & ebooks free for days

From $11.99/month after trial. Cancel anytime.

Programming APIs with C# and .NET: Develop high-performance APIs that ensure seamless application communication and enhanced security
Programming APIs with C# and .NET: Develop high-performance APIs that ensure seamless application communication and enhanced security
Programming APIs with C# and .NET: Develop high-performance APIs that ensure seamless application communication and enhanced security
Ebook393 pages1 hour

Programming APIs with C# and .NET: Develop high-performance APIs that ensure seamless application communication and enhanced security

Rating: 0 out of 5 stars

()

Read preview
LanguageEnglish
PublisherPackt Publishing
Release dateNov 22, 2024
ISBN9781803231099
Programming APIs with C# and .NET: Develop high-performance APIs that ensure seamless application communication and enhanced security
Author

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

Internet & Web For You

View More

Reviews for Programming APIs with C# and .NET

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Programming APIs with C# and .NET - Jesse Liberty

    Cover.jpgPackt Logo

    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

    Enjoying the preview?
    Page 1 of 1