Pragmatic Microservices with C# and Azure

Book description

Unlock efficiency in software development by learning microservices with C#, .NET and Azure

Key Features

  • Benefit from step-by-step project-based guidance on how to develop a full-fledged microservices application
  • Gain extensive knowledge of microservices by covering essential concepts, tools, and methodologies
  • Use essential Microsoft Azure cloud-native services and as alternatives for on-premises environments
  • Purchase of the print or Kindle book includes a free PDF eBook

Book Description

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.

What you will learn

  • Understand the advantages and practical applications of microservices
  • Utilize the cloud-ready .NET Aspire stack and tools for efficient development
  • Create robust RESTful APIs using ASP.NET Core minimal APIs
  • Implement seamless database integration with both relational and NoSQL databases
  • Containerize microservices with Docker for better scalability and management
  • Deploy microservices to the Azure Container Apps environment and Kubernetes for robust cloud solutions
  • Implement logging, metrics, and distributed tracing to ensure reliability and performance

Who this book is for

This book is for software developers, architects, and engineers who are interested in adopting microservices architecture using the .NET framework and Microsoft Azure. Both intermediate and advanced developers who have a foundational understanding of C#, ASP.NET Core, and cloud computing concepts will find this book helpful.

Table of contents

  1. Pragmatic Microservices with C# and Azure
  2. Contributors
  3. About the author
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Conventions used
    6. Get in touch
    7. Share Your Thoughts
    8. Download a free PDF copy of this book
  6. Part 1: Creating Microservices with .NET
  7. Chapter 1: Introduction to .NET Aspire and Microservices
    1. Technical requirements
    2. Starting with .NET Aspire
      1. Creating a .NET Aspire project
      2. The .NET Aspire dashboard
      3. The .NET Aspire app model
      4. The shared project for common configuration
      5. Using service discovery
      6. .NET Aspire components
      7. Creating the app model manifest
    3. Codebreaker – the solution
    4. Using Microsoft Azure
      1. Azure resources used by Codebreaker
      2. Azure provisioning from the development environment
    5. Summary
    6. Further reading
  8. Chapter 2: Minimal APIs – Creating REST Services
    1. Technical requirements
    2. The game models
      1. Exploring the game analyzers library
      2. Exploring game analyzers
      3. Creating a .NET library
      4. Implementing classes for the model types
      5. Defining the game repository contract
    3. The minimal APIs project
      1. Creating the Web API project
      2. Exploring WebApplicationBuilder and WebApplication
      3. Implementing the repository
      4. Creating game objects initialized with random values
      5. Creating data transfer objects
      6. Implementing the games service
      7. Converting transfer objects to and from the object model
      8. Creating endpoints for the Games API service
      9. Configuring JSON serialization
      10. Creating endpoint filters
      11. Running the service
    4. OpenAPI information
      1. Adding OpenAPI documentation
      2. Documentation for the endpoints
      3. Adding return type information to OpenAPI
    5. Testing the service
    6. Enabling .NET Aspire
      1. Exploring the Aspire host
      2. Exploring the ServiceDefaults library
      3. Running the .NET Aspire host
    7. Summary
    8. Further reading
  9. Chapter 3: Writing Data to Relational and NoSQL Databases
    1. Technical requirements
    2. Exploring the models for database mapping
    3. Using EF Core with SQL Server
      1. Creating a data class library using SQL Server
      2. Creating an EF Core context for SQL Server
      3. Customizing the mapping of simple properties
      4. Creating value conversion to map complex properties
      5. Defining relations between games and moves
      6. Implementing the repository contract
      7. Configuring user secrets
      8. Configuring the application model with SQL Server
      9. Configuring the DI container with the minimal APIs project
    4. Creating migrations with EF Core
      1. Adding the .NET EF Core tool
      2. Creating or updating the database programmatically
      3. Updating the database schema
      4. Running the application with SQL Server
    5. Using EF Core with Azure Cosmos DB
      1. Creating a class library project for EF Core with NoSQL
      2. Creating an EF Core context for Azure Cosmos DB
      3. Creating a value converter to convert complex types
      4. Creating embedded entities
      5. Implementing the repository contract
      6. Configuring the application model with Azure Cosmos DB
      7. Configuring the DI container
    6. Summary
    7. Further reading
  10. Chapter 4: Creating Libraries for Client Applications
    1. Technical requirements
    2. Creating a library to create HTTP requests
      1. Creating a library with multi-targeting support
      2. Injecting the HttpClient class
      3. Sending HTTP requests
      4. Creating a NuGet Package
    3. Creating a client application
      1. Configuring the dependency injection container
      2. Interacting with the user
    4. Using Microsoft Kiota to create a client
      1. Installing Kiota
      2. Generating Code with Kiota
      3. Using the Kiota-generated code
    5. Summary
    6. Further reading
  11. Part 2: Hosting and Deploying
  12. Chapter 5: Containerization of Microservices
    1. Technical requirements
    2. Working with Docker
      1. Using Docker Desktop
      2. Running a Docker container
      3. Running SQL Server in a Docker container
      4. Using volumes with a Docker container
      5. Creating a database in the Docker container
    3. Building a Docker image
      1. Creating a Dockerfile
      2. Building a Docker image with a Dockerfile
      3. Running the games API using Docker
      4. Building a Docker image using dotnet publish
    4. Running the solution with .NET Aspire
      1. Configuring a Docker container for SQL Server
      2. Configuring the .NET Aspire SQL Server component
      3. Configuring interaction with multiple services
      4. Running the solution with .NET Aspire
    5. Using native AOT with ASP.NET Core
      1. Using the slim builder
      2. Using the JSON serializer source generator
      3. Building for Windows
      4. Creating a Linux Docker image
      5. Running the solution with the native AOT container
    6. Summary
    7. Further reading
  13. Chapter 6: Microsoft Azure for Hosting Applications
    1. Technical requirements
    2. Experiencing Microsoft Azure
      1. Cost
      2. Naming conventions and more
    3. Creating Azure resources
      1. Creating a resource group
      2. Creating an Azure Cosmos DB account
      3. Creating an Azure container registry
      4. Creating an Azure Container Apps environment
    4. Creating an Azure Cosmos database
      1. Configuring replication with Azure Cosmos DB
      2. Configuring consistency
    5. Pushing images to the ACR instance
    6. Creating Azure container apps
      1. Configuring secrets and environment variables
      2. Configuring scaling with Azure Container Apps
    7. Creating Azure resources with .NET Aspire and azd
      1. Provisioning Azure resources while debugging
      2. Provisioning the complete solution with azd up
      3. Diving into azd up stages
      4. Creating Bicep files using azd
    8. Summary
    9. Further reading
  14. Chapter 7: Flexible Configurations
    1. Technical requirements
    2. Experiencing .NET configurations
      1. Retrieving configuration values
      2. Using options
      3. Using environments
    3. Using configurations with Azure Container Apps
    4. Using configurations with Azure App Configuration
      1. Creating an Azure App Configuration service
      2. Configuring values with Azure App Configuration
      3. Initializing app configuration values
      4. Using Azure App Configuration from the application
    5. Storing secrets with Azure Key Vault
    6. Reducing the need for secrets with managed identities
      1. Creating a managed identity and assigning roles
      2. Configuring the Azure App Configuration provider with managed identities
    7. Using environments with Azure App Configuration
      1. Using App Configuration labels to map .NET environments
    8. Summary
    9. Further reading
  15. Chapter 8: CI/CD – Publishing with GitHub Actions
    1. Technical requirements
    2. Preparing the solution using the Azure Developer CLI
    3. Exploring GitHub Actions
      1. Creating a GitHub Actions workflow
      2. Workflow file with YAML syntax
      3. Triggers
      4. Permissions for secretless Azure federated credentials
      5. Jobs and runners
      6. Steps and actions
      7. GitHub variables and secrets
      8. Getting more with GitHub Actions
    4. Enhancing GitHub Actions workflows
      1. Configuring variables and secrets
      2. Running unit tests
      3. Running multiple jobs
    5. Using deployment environments
      1. Create environments with the Azure Developer CLI
      2. Creating GitHub environments
      3. Defining deployment protection rules
      4. Setting environment secrets and variables
      5. Using environments with workflows
    6. Publishing NuGet packages
      1. Preparing the library project
      2. Creating access tokens
      3. Creating a GitHub action to publish a GitHub package
    7. Using modern deployment patterns
      1. Configuring feature flags
      2. DI and middleware configuration for feature flags
      3. Using feature flags
    8. Summary
    9. Further reading
  16. Chapter 9: Authentication and Authorization with Services and Clients
    1. Technical requirements
    2. Choosing an identity solution
    3. Creating an Azure AD B2C tenant
      1. Specifying identity providers
      2. Configuring user attributes
      3. Defining user flows
      4. Creating app registrations
    4. Securing an API
      1. Creating a new project with authentication
      2. Creating an application gateway with YARP
      3. Mapping routes with YARP
      4. Adding authentication to the gateway
    5. Authentication using Microsoft Identity with ASP.NET Core web applications
    6. Specifying authentication with Azure Container Apps
    7. Using ASP.NET Core Identity to store user information in a local database
      1. Customizing the EF Core configuration
      2. Configuring ASP.NET Core Identity
      3. Creating identity API endpoints
    8. Summary
    9. Further reading
  17. Part 3: Troubleshooting and Scaling
  18. Chapter 10: All About Testing the Solution
    1. Technical requirements
    2. Creating unit tests
      1. Exploring the games analyzer library
      2. Creating a unit test project
      3. Mocking the IGame interface
      4. Creating test helpers
      5. Creating a simple unit test
      6. Passing test data to unit tests
      7. Expecting exceptions with a unit test
      8. Using a mocking library
      9. Running unit tests
    3. Creating .NET Aspire integration tests
      1. Creating asynchronous initialization
      2. Creating a test to verify an HTTP bad request status
      3. Creating a test to play a complete game
    4. Creating end-to-end .NET Playwright tests
      1. Creating a test project with Playwright
      2. Creating a context
      3. Playing a game with Playwright
    5. Creating test loads
    6. Summary
    7. Further reading
  19. Chapter 11: Logging and Monitoring
    1. Technical requirements
    2. Adding log messages
      1. Creating strongly typed log messages
      2. Writing log messages
      3. Viewing logs with the .NET Aspire dashboard
    3. Using metrics data
      1. Monitoring built-in .NET metrics
      2. Creating custom metrics data
      3. Creating tags
      4. Creating strongly typed methods for metrics data
      5. Injecting and using metrics
      6. Updating unit tests to inject metrics types
      7. Creating unit tests to verify metrics
      8. Viewing metrics data with the .NET Aspire dashboard
    4. Using distributed tracing
      1. Creating an ActivitySource class with the DIC
      2. Writing trace messages
      3. Viewing distributed traces with the .NET Aspire dashboard
    5. Monitoring with Azure Application Insights
      1. Configuring the .NET Aspire host for Application Insights
      2. Configuring the services to use Application Insights
      3. Monitoring the solution with Application Insights
    6. Monitoring with Prometheus and Grafana
      1. Adding Docker containers for Prometheus and Grafana
      2. Configuring Prometheus
      3. Configuring Grafana
      4. Monitoring the solution with Prometheus and Grafana
    7. Summary
    8. Further reading
  20. Chapter 12: Scaling Services
    1. Technical requirements
    2. Increasing performance with caches
      1. Reading and writing from the cache
      2. Configuring the Aspire Redis component
    3. Simulating users with Azure Load Testing
      1. Scaling to one replica
      2. Creating an Azure URL-based load test
      3. Running a load with virtual users
      4. Reaching limits with a higher load
    4. Scaling up or scaling out services
      1. Configuring scaling up
      2. Configuring scaling out
    5. Scaling dynamically with scale rules
    6. Implementing health checks
      1. Adding health checks to the DI container
      2. Adding health checks with .NET Aspire components
      3. Mapping health checks
      4. Using the health checks with Azure Container Apps
    7. Summary
    8. Further reading
  21. Part 4: More communication options
  22. Chapter 13: Real-Time Messaging with SignalR
    1. Technical requirements
    2. Creating a SignalR service
      1. Creating a SignalR hub
      2. Returning live information to the clients
      3. Registering SignalR services and the hub
      4. Forwarding requests from the game-apis service
    3. Creating a SignalR client
    4. Changing the serialization protocol
    5. Using Azure SignalR Service
    6. Summary
    7. Further reading
  23. Chapter 14: gRPC for Binary Communication
    1. Technical requirements
    2. Comparing REST with gRPC
      1. Communication style
      2. Performance
      3. Flexibility
      4. Language support
      5. Security
      6. Use cases
    3. Updating a service project so that it uses gRPC
    4. Creating service contracts
      1. Creating a gRPC service contract for live-service
      2. Creating a gRPC service contract for the game-apis service
      3. Creating conversion methods
    5. Creating gRPC services
    6. Creating gRPC clients
    7. Summary
    8. Further reading
  24. Chapter 15: Asynchronous Communication with Messages and Events
    1. Technical requirements
    2. Comparing messages and events
      1. Message queues
      2. Events
    3. Creating a service that reads from Azure Queue Storage
      1. Defining app-model for Azure Storage
      2. Using the storage queue component
      3. Running the application
    4. Publishing messages to Azure Event Hubs
      1. Defining app-model for Event Hubs
      2. Using the .NET Aspire Event Hubs component to produce events
    5. Subscribing to Azure Event Hubs events
      1. Defining app-model for Event Hubs subscribers
      2. Using the Event Hubs component with async streaming
      3. Using the .NET Aspire Event Hubs component to process messages
    6. Deploying the solution to Microsoft Azure
    7. Using Apache Kafka for event processing
      1. Configuring Apache Kafka with app-model
      2. Publishing Apache Kafka events
      3. Subscribing to Apache Kafka events
    8. Summary
    9. Further reading
  25. Chapter 16: Running Applications On-Premises and in the Cloud
    1. Technical requirements
    2. Thinking about deployment in production
      1. Customizing deployments with C# and .NET Aspire
    3. Creating a Kubernetes cluster with Microsoft Azure
    4. Using Aspir8 to deploy to Kubernetes
      1. Creating Kubernetes manifests
      2. Creating and pushing Docker images
      3. Deploying to Kubernetes
    5. Summary
    6. Further reading
  26. Index
    1. Why subscribe?
  27. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Pragmatic Microservices with C# and Azure
  • Author(s): Christian Nagel
  • Release date: May 2024
  • Publisher(s): Packt Publishing
  • ISBN: 9781835088296