Pragmatic Microservices with C# and Azure - Christian Nagel - E-Book

Pragmatic Microservices with C# and Azure E-Book

Christian Nagel

0,0
35,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

Pragmatic Microservices with C# and Azure introduces .NET Aspire for microservices, focusing on defining an app model, utilizing service discovery, and integrating with Azure's native cloud services. Written by a Microsoft MVP and seasoned software architect with over two decades of experience in .NET, this book will help you get to grips with robust service development using .NET features like minimal APIs, gRPC, and SignalR for real-time communication.
Aside from covering essential aspects of DevOps, including testing methodologies such as unit, integration, and load testing, you’ll also explore logging and monitoring including OpenTelemetry using tools like Azure Log Analytics, Application Insights, Prometheus, and Grafana. You'll learn about asynchronous communication leveraging queues and events through Azure Event Hub and Apache.
Throughout the book, theoretical aspects will be complemented by practical skills gained from building and deploying a fully functional microservices-based application. By the end, you’ll possess a deep understanding of microservices architecture, hands-on experience with various .NET technologies and Azure services, and the ability to design, build, deploy, and manage microservices applications effectively in both on-premises and cloud environments.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 569

Veröffentlichungsjahr: 2024

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Pragmatic Microservices with C# and Azure

Build, deploy, and scale microservices efficiently to meet modern software demands

Christian Nagel

Pragmatic Microservices with C# and Azure

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 author, 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.

Publishing Product Manager: Kunal Sawant

Book Project Manager: Prajakta Naik

Senior Editor: Rounak Kulkarni

Technical Editor: Jubit Pincy

Copy Editor: Safis Editing

Proofreader: Rounak Kulkarni

Indexer: Hemangini Bari

Production Designer: Alishon Mendonca

Senior Developer Relations Marketing Executive: Shrinidhi Monaharan

Business Development Executive: Samriddhi Murarka

First published: May 2024

Production reference: 1240524

Published by Packt Publishing Ltd.

Grosvenor House

11 St Paul’s Square

Birmingham

B3 1RB, UK

ISBN 978-1-83508-829-6

www.packtpub.com

To my mother, Margarete Nagel, and the memory of my father, Ernst, for always supporting me when help was needed. To my wife, Angela, and my children, Stephanie, Matthias, and Katharina – I love you all! Thank you for being here!

– Christian Nagel

Contributors

About the author

Christian Nagel is a Microsoft MVP for Microsoft Azure and Development Technologies, software architect, and veteran developer who has been building solutions with .NET technologies since 2000 and working with UNIX and OpenVMS since before that. He has authored many acclaimed .NET books, and speaks at international conferences such as TechEd, TechDays, Thrive, and BASTA! Christian is a Microsoft Certified Trainer, DevOps Engineer Expert, and Azure Developer Associate. He’s the founder of CN innovation, a company offering training, consulting, and development. Even after many years in software development, Christian still loves learning, using new technologies, and teaching others how to use them. Using his profound knowledge of Microsoft technologies, he has written various books. Contact him via his website at https://www.cninnovation.com, and follow his posts on X (formerly Twitter) at @christiannagel.

This book was a great journey that started before the first public preview of .NET Aspire. I would like to thank the .NET Aspire team for their fast fixes and their support; special thanks to David Fowler with whom I had interesting discussions. I would like to thank my reviewers: István Novák, who has already reviewed several of my books and always helps remind me when I miss something that helps you – the reader – to better follow the content, and Brady Gaster, for his great inspirations and insights on Microsoft technologies, and, if he can’t help, he always has contacts who can. I also thank Sebastian Szvetecz, who is now part of CN innovation and has great inputs to the Codebreaker repository. Last but not least, I thank the team at Packt for making this book a reality.

About the reviewers

Brady Gaster works on the .NET tools team, where he collaborates with product teams such as ASP.NET, Azure Container Apps, Functions, App Service, and the Azure SDK to help .NET developers using ASP.NET, SignalR, Orleans, and .NET Aspire have a great experience building apps with .NET and Azure. When Brady’s not learning with (or from) his two sons or hacking on demos and samples, he’s probably in his home music studio making music with various analog synthesizers and drum machines, burning calories by bouncing up and down as he spins drum and bass or dubstep, or finding ways to combine some or all of the above into a new mashup with a cacophony of cables.

Istvan Novak is a freelance technology consultant and commonsense coach. In the last 30 years, he participated in over 50 enterprise software development projects.

In 2002, he co-authored the first Hungarian book on .NET development. In 2007, he was awarded the Microsoft MVP title, holding it for 16 years. In 2011, he became a Microsoft Regional Director for six years.

Istvan holds a master’s degree from the Technical University of Budapest, Hungary, and a doctoral degree in software technology. He is a passionate scuba diver. You may have a good chance of meeting him underwater in the Red Sea in any season of the year.

Table of Contents

Preface

Part 1: Creating Microservices with .NET

1

Introduction to .NET Aspire and Microservices

Technical requirements

Starting with .NET Aspire

Creating a .NET Aspire project

The .NET Aspire dashboard

The .NET Aspire app model

The shared project for common configuration

Using service discovery

.NET Aspire components

Creating the app model manifest

Codebreaker – the solution

Using Microsoft Azure

Azure resources used by Codebreaker

Azure provisioning from the development environment

Summary

Further reading

2

Minimal APIs – Creating REST Services

Technical requirements

The game models

Exploring the game analyzers library

Exploring game analyzers

Creating a .NET library

Implementing classes for the model types

Defining the game repository contract

The minimal APIs project

Creating the Web API project

Exploring WebApplicationBuilder and WebApplication

Implementing the repository

Creating game objects initialized with random values

Creating data transfer objects

Implementing the games service

Converting transfer objects to and from the object model

Creating endpoints for the Games API service

Configuring JSON serialization

Creating endpoint filters

Running the service

OpenAPI information

Adding OpenAPI documentation

Documentation for the endpoints

Adding return type information to OpenAPI

Testing the service

Enabling .NET Aspire

Exploring the Aspire host

Exploring the ServiceDefaults library

Running the .NET Aspire host

Summary

Further reading

3

Writing Data to Relational and NoSQL Databases

Technical requirements

Exploring the models for database mapping

Using EF Core with SQL Server

Creating a data class library using SQL Server

Creating an EF Core context for SQL Server

Customizing the mapping of simple properties

Creating value conversion to map complex properties

Defining relations between games and moves

Implementing the repository contract

Configuring user secrets

Configuring the application model with SQL Server

Configuring the DI container with the minimal APIs project

Creating migrations with EF Core

Adding the .NET EF Core tool

Creating or updating the database programmatically

Updating the database schema

Running the application with SQL Server

Using EF Core with Azure Cosmos DB

Creating a class library project for EF Core with NoSQL

Creating an EF Core context for Azure Cosmos DB

Creating a value converter to convert complex types

Creating embedded entities

Implementing the repository contract

Configuring the application model with Azure Cosmos DB

Configuring the DI container

Summary

Further reading

4

Creating Libraries for Client Applications

Technical requirements

Creating a library to create HTTP requests

Creating a library with multi-targeting support

Injecting the HttpClient class

Sending HTTP requests

Creating a NuGet Package

Creating a client application

Configuring the dependency injection container

Interacting with the user

Using Microsoft Kiota to create a client

Installing Kiota

Generating Code with Kiota

Using the Kiota-generated code

Summary

Further reading

Part 2: Hosting and Deploying

5

Containerization of Microservices

Technical requirements

Working with Docker

Using Docker Desktop

Running a Docker container

Running SQL Server in a Docker container

Using volumes with a Docker container

Creating a database in the Docker container

Building a Docker image

Creating a Dockerfile

Building a Docker image with a Dockerfile

Running the games API using Docker

Building a Docker image using dotnet publish

Running the solution with .NET Aspire

Configuring a Docker container for SQL Server

Configuring the .NET Aspire SQL Server component

Configuring interaction with multiple services

Running the solution with .NET Aspire

Using native AOT with ASP.NET Core

Using the slim builder

Using the JSON serializer source generator

Building for Windows

Creating a Linux Docker image

Running the solution with the native AOT container

Summary

Further reading

6

Microsoft Azure for Hosting Applications

Technical requirements

Experiencing Microsoft Azure

Cost

Naming conventions and more

Creating Azure resources

Creating a resource group

Creating an Azure Cosmos DB account

Creating an Azure container registry

Creating an Azure Container Apps environment

Creating an Azure Cosmos database

Configuring replication with Azure Cosmos DB

Configuring consistency

Pushing images to the ACR instance

Creating Azure container apps

Configuring secrets and environment variables

Configuring scaling with Azure Container Apps

Creating Azure resources with .NET Aspire and azd

Provisioning Azure resources while debugging

Provisioning the complete solution with azd up

Diving into azd up stages

Creating Bicep files using azd

Summary

Further reading

7

Flexible Configurations

Technical requirements

Experiencing .NET configurations

Retrieving configuration values

Using options

Using environments

Using configurations with Azure Container Apps

Using configurations with Azure App Configuration

Creating an Azure App Configuration service

Configuring values with Azure App Configuration

Initializing app configuration values

Using Azure App Configuration from the application

Storing secrets with Azure Key Vault

Reducing the need for secrets with managed identities

Creating a managed identity and assigning roles

Configuring the Azure App Configuration provider with managed identities

Using environments with Azure App Configuration

Using App Configuration labels to map .NET environments

Summary

Further reading

8

CI/CD – Publishing with GitHub Actions

Technical requirements

Preparing the solution using the Azure Developer CLI

Exploring GitHub Actions

Creating a GitHub Actions workflow

Workflow file with YAML syntax

Triggers

Permissions for secretless Azure federated credentials

Jobs and runners

Steps and actions

GitHub variables and secrets

Getting more with GitHub Actions

Enhancing GitHub Actions workflows

Configuring variables and secrets

Running unit tests

Running multiple jobs

Using deployment environments

Create environments with the Azure Developer CLI

Creating GitHub environments

Defining deployment protection rules

Setting environment secrets and variables

Using environments with workflows

Publishing NuGet packages

Preparing the library project

Creating access tokens

Creating a GitHub action to publish a GitHub package

Using modern deployment patterns

Configuring feature flags

DI and middleware configuration for feature flags

Using feature flags

Summary

Further reading

9

Authentication and Authorization with Services and Clients

Technical requirements

Choosing an identity solution

Creating an Azure AD B2C tenant

Specifying identity providers

Configuring user attributes

Defining user flows

Creating app registrations

Securing an API

Creating a new project with authentication

Creating an application gateway with YARP

Mapping routes with YARP

Adding authentication to the gateway

Authentication using Microsoft Identity with ASP.NET Core web applications

Specifying authentication with Azure Container Apps

Using ASP.NET Core Identity to store user information in a local database

Customizing the EF Core configuration

Configuring ASP.NET Core Identity

Creating identity API endpoints

Summary

Further reading

Part 3: Troubleshooting and Scaling

10

All About Testing the Solution

Technical requirements

Creating unit tests

Exploring the games analyzer library

Creating a unit test project

Mocking the IGame interface

Creating test helpers

Creating a simple unit test

Passing test data to unit tests

Expecting exceptions with a unit test

Using a mocking library

Running unit tests

Creating .NET Aspire integration tests

Creating asynchronous initialization

Creating a test to verify an HTTP bad request status

Creating a test to play a complete game

Creating end-to-end .NET Playwright tests

Creating a test project with Playwright

Creating a context

Playing a game with Playwright

Creating test loads

Summary

Further reading

11

Logging and Monitoring

Technical requirements

Adding log messages

Creating strongly typed log messages

Writing log messages

Viewing logs with the .NET Aspire dashboard

Using metrics data

Monitoring built-in .NET metrics

Creating custom metrics data

Creating tags

Creating strongly typed methods for metrics data

Injecting and using metrics

Updating unit tests to inject metrics types

Creating unit tests to verify metrics

Viewing metrics data with the .NET Aspire dashboard

Using distributed tracing

Creating an ActivitySource class with the DIC

Writing trace messages

Viewing distributed traces with the .NET Aspire dashboard

Monitoring with Azure Application Insights

Configuring the .NET Aspire host for Application Insights

Configuring the services to use Application Insights

Monitoring the solution with Application Insights

Monitoring with Prometheus and Grafana

Adding Docker containers for Prometheus and Grafana

Configuring Prometheus

Configuring Grafana

Monitoring the solution with Prometheus and Grafana

Summary

Further reading

12

Scaling Services

Technical requirements

Increasing performance with caches

Reading and writing from the cache

Configuring the Aspire Redis component

Simulating users with Azure Load Testing

Scaling to one replica

Creating an Azure URL-based load test

Running a load with virtual users

Reaching limits with a higher load

Scaling up or scaling out services

Configuring scaling up

Configuring scaling out

Scaling dynamically with scale rules

Implementing health checks

Adding health checks to the DI container

Adding health checks with .NET Aspire components

Mapping health checks

Using the health checks with Azure Container Apps

Summary

Further reading

Part 4: More communication options

13

Real-Time Messaging with SignalR

Technical requirements

Creating a SignalR service

Creating a SignalR hub

Returning live information to the clients

Registering SignalR services and the hub

Forwarding requests from the game-apis service

Creating a SignalR client

Changing the serialization protocol

Using Azure SignalR Service

Summary

Further reading

14

gRPC for Binary Communication

Technical requirements

Comparing REST with gRPC

Communication style

Performance

Flexibility

Language support

Security

Use cases

Updating a service project so that it uses gRPC

Creating service contracts

Creating a gRPC service contract for live-service

Creating a gRPC service contract for the game-apis service

Creating conversion methods

Creating gRPC services

Creating gRPC clients

Summary

Further reading

15

Asynchronous Communication with Messages and Events

Technical requirements

Comparing messages and events

Message queues

Events

Creating a service that reads from Azure Queue Storage

Defining app-model for Azure Storage

Using the storage queue component

Running the application

Publishing messages to Azure Event Hubs

Defining app-model for Event Hubs

Using the .NET Aspire Event Hubs component to produce events

Subscribing to Azure Event Hubs events

Defining app-model for Event Hubs subscribers

Using the Event Hubs component with async streaming

Using the .NET Aspire Event Hubs component to process messages

Deploying the solution to Microsoft Azure

Using Apache Kafka for event processing

Configuring Apache Kafka with app-model

Publishing Apache Kafka events

Subscribing to Apache Kafka events

Summary

Further reading

16

Running Applications On-Premises and in the Cloud

Technical requirements

Thinking about deployment in production

Customizing deployments with C# and .NET Aspire

Creating a Kubernetes cluster with Microsoft Azure

Using Aspir8 to deploy to Kubernetes

Creating Kubernetes manifests

Creating and pushing Docker images

Deploying to Kubernetes

Summary

Further reading

Index

Other Books You May Enjoy

Part 1: Creating Microservices with .NET

Part 1 introduces the fundamental functionality of a microservices application. Before delving into the development of the Codebreaker application, you will explore .NET Aspire - a new cloud-ready stack for service construction. This section covers the technology’s offerings, essential features, an introduction to Microsoft Azure, and an overview of the components comprising the Codebreaker application. Subsequently, you will engage in coding using ASP.NET Core minimal APIs, crafting code for data interaction with both relational and NoSQL databases through Entity Framework (EF) Core, utilizing Azure Cosmos DB and SQL Server, and generating client libraries to access the REST service. One approach involves leveraging the HTTP client factory, while the other employs Microsoft Kioata.

Each chapter in this section provides a functional application that evolves with each subsequent chapter, enhancing the learning experience.

This part has the following chapters:

Chapter 1, Introduction to .NET Aspire and MicroservicesChapter 2, Minimal APIs – Creating REST ServicesChapter 3, Writing Data to Relational and NoSQL DatabasesChapter 4, Creating Libraries for Client Applications

2

Minimal APIs – Creating REST Services

Since .NET 6, minimal APIs are the new way to create REST APIs. With later .NET versions, more and more enhancements have been made available, which makes them the preferred way to create REST services with .NET.

In this chapter, you’ll learn how to create a data representation of the game with model types, use these types in a service to implement the game functionality, create a minimal API project to create games, update games by setting game moves, and return information about games.

You’ll implement functionality to offer an OpenAPI description for developers accessing the service to get information about the service, and an easy way to create a client application.

In this chapter, you’ll be exploring these topics:

Creating models for the gameImplementing an in-memory game repositoryImplementing the REST service of the game using minimal APIsUsing OpenAPI to describe the serviceTesting the service using HTTP filesEnabling .NET Aspire

By the end of this chapter, you’ll have a running service implementing the Codebreaker Games API with an in-memory games store, accessible using HTTP requests.

Technical requirements

The code for this chapter can be found in the following GitHub repository: https://github.com/PacktPublishing/Pragmatic-Microservices-with-CSharp-and-Azure. The ch02 source code folder contains the code samples for this chapter. You’ll find the code for the following:

Codebreaker.GamesAPIs – The Web API projectCodebreaker.GamesAPIs.Models – A library for the data modelsCodebreaker.GameAPIs.Analyzers – A library containing game move analyzers for the gameCodebreaker.GamesAPIs.Analyzers.Tests – Unit tests for the game move analyzersCodebreaker.AppHost – The host project for .NET AspireCodebreaker.ServiceDefaults – A library used by the .NET Aspire configuration

Note

You don’t implement the game move analyzers of the game in this chapter. The Analyzers project is just for reference purposes, but you can simply use a NuGet package for the analyzers (CNinnovation.Codebreaker.Analyzers) that has been made available for you to build the service.

For the installation of Visual Studio, Visual Studio Code, and .NET Aspire, check the README file for this chapter in the repository.

The game models

Before