
Practical Serverless and Microservices with C#
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
- Understand cost considerations, traffic limits, and time limits surrounding serverless environments
- Take full advantage of the synergy between Azure services (Container Apps, Functions, and Aspire) and.NET code
- Purchase of the print or Kindle book includes a free eBook in PDF format
Book DescriptionFrom the authors of the Software Architecture with C# and.NET series comes this practical and grounded showcase of microservices using the.NET stack. Written for.NET developers entering the world of modern cloud and distributed applications, it shows you when microservices and serverless architectures are the right choice for building scalable enterprise solutions and when they're not. You'll gain a realistic understanding of their use cases and limitations. Rather than promoting microservices as a one-size-fits-all solution, it encourages thoughtful adoption based on real-world needs. Following a brief introduction and important setup, the book helps you prepare for practical application through examples such as a ride-sharing website. You'll work with Docker, Kubernetes, Azure Container Apps, and the new.NET Aspire with considerations for security, observability, and cost management. The book culminates in a complete event-driven application that brings together everything you've covered. By the end of this microservices book, you'll have a well-rounded understanding of cloud and distributed.NET-through the lens of two industry veterans. *Email sign-up and proof of purchase required What you will learn - Set up serverless environments in Azure for developing and debugging
- Design reliable communication and computation across microservices
- Explore Azure Functions in depth and use triggers for IoT and background tasks
- Use Azure Container Apps to simplify the creation and management of containers
- Apply best practices to secure a microservices application
- Accurately assess and calculate costs and usage limits in serverless solutions
Who this book is forThis book is for engineers and senior software developers looking to advance into modern cloud and distributed applications. It helps professionals evolve their knowledge of microservices and serverless architecture to get the best of both architectural models. Prior experience with C#/.NET and the Microsoft Stack (Entity Framework and ASP.NET Core) is required to get the most out of this book. If you've enjoyed the authors' previous Software Architecture with C# and.NET series, this new book offers an in-depth exploration of select topics in those earlier works.
All prices
More details
Content
- Intro
- Title Page
- Credit Page
- Contributors
- Table of Contents
- Preface
- Free Benefits with Your Book
- Chapter 1: Demystifying Serverless Applications
- Technical requirements
- Free Benefits with Your Book
- What is serverless?
- Is serverless a way to deliver microservices?
- How does Microsoft Azure present serverless?
- Creating your first serverless app in Azure
- Understanding the triggers available in Azure Functions
- Coding with Azure Functions
- Coding Azure functions using VS Code
- Coding Azure functions using Visual Studio
- Summary
- Questions
- Further reading
- Chapter 2: Demystifying Microservices Applications
- The rise of Service-Oriented Architectures (SOAs) and microservices
- The rise of SOA
- Toward microservices architectures
- The definition and organization of microservices architectures
- A definition of microservices architectures
- Domain of expertise and microservices
- Replicable microservices
- Splitting microservices development among different teams
- Microservices, interfaces, and communication protocols
- Just the interfaces of the logical microservices are public
- Microservices organization
- Car-sharing example
- When is it worth adopting microservices architectures?
- Microservices common patterns
- Resilient task execution
- Efficacious handling of asynchronous communication
- Event-based communications
- Interfacing the external world
- Summary
- Questions
- Further reading
- Chapter 3: Setup and Theory: Docker and Onion Architecture
- Technical requirements
- The Onion Architecture
- The Domain layer
- Application services
- Queries
- Commands
- Domain events
- A solution template based on the Onion Architecture
- Matching aggregates and ORM entities
- A complete solution based on the Onion Architecture
- Containers and Docker
- Docker Desktop: a simple example
- A few more Docker commands and options
- Visual Studio support for Docker
- Summary
- Questions
- Further reading
- Chapter 4: Azure Functions and Triggers Available
- Technical requirements
- HTTP trigger
- Advantages, disadvantages, and when to use the HTTP trigger
- Car-sharing HTTP trigger example
- Advantages, disadvantages, and when to use the Azure SQL trigger
- Car-sharing SQL trigger example
- Advantages, disadvantages, and when to use the Cosmos DB trigger
- Car-sharing Cosmos DB trigger example
- Azure Service Bus trigger
- Comparison with the Kafka trigger and the RabbitMQ trigger
- Car-sharing example with the Azure Service Bus trigger
- Summary
- Questions
- Further reading
- Chapter 5: Background Functions in Practice
- Technical requirements
- Timer trigger
- Publishing your functions
- Monitoring your functions
- Advantages, disadvantages, and when to use Azure timer triggers
- Car-sharing timer trigger example
- Blob trigger
- Advantages, disadvantages, and when to use Blob storage triggers
- Blob trigger implementation using Event Grid
- Car-sharing Blob storage trigger example
- Queue storage trigger
- Advantages, disadvantages, and when to use queue storage triggers
- Car-sharing queue storage trigger example
- Summary
- Questions
- Further reading
- Join our community on Discord
- Chapter 6: IoT Functions in Practice
- Technical requirements
- Enabling IoT in Azure
- Connecting IoT Hub with Azure Functions
- Car-sharing IoT example
- Summary
- Questions
- Further reading
- Chapter 7: Microservices in Practice
- Technical requirements
- The route-planning microservice of the car-sharing application
- Microservice specifications
- Handling security and authorization
- Creating the Visual Studio solution
- Microservice basic design
- The message broker: RabbitMQ
- Input communication
- Output communication
- Ensuring that messages are processed in the proper order
- Designing Docker image environment parameters
- The microservice main service
- EasyNetQ's RPC facilities
- Other required hosted services
- Ensuring resilient task execution with Polly
- The Polly library
- Adding Polly to our project
- From abstraction to implementation details
- The domain layer
- The route request aggregate
- The route offer aggregate
- The output queue item aggregate
- The database driver
- The IOutputQueueRepository implementation
- The IRouteRequestRepositoryimplementation
- The IRouteOfferRepository implementation
- Creating migrations and databases
- The application services: Defining all command and event handlers
- Coding all event handlers
- Summary
- Questions
- Further reading
- Chapter 8: Practical Microservices Organization with Kubernetes
- Technical requirements
- Introduction to orchestrators and their configuration
- .yaml files
- Kubernetes basics
- Interacting with Kubernetes: Kubectl, Minikube, and AKS
- Creating an Azure Kubernetes cluster
- Configuring your application in Kubernetes
- Dynamic provisioning of permanent disk space
- ReplicaSets, Deployments, and their services
- StatefulSets and Headless Services
- Scaling and autoscaling
- Resource metrics
- Pod metrics
- Object metrics
- Running your microservices on Kubernetes
- Organizing all deployment environments
- Database engine and database installation
- Container registries
- Message broker installation
- Debugging techniques
- Testing the route-matching worker microservice
- Advanced Kubernetes configuration
- Secrets
- Readiness, liveness, and startup probes
- Ingresses
- Testing Ingresses with Minikube
- Using an NGNIX-based Ingress in AKS
- Summary
- Questions
- Further reading
- Chapter 9: Simplifying Containers and Kubernetes: Azure Container Apps, and Othert Tools
- Technical requirements
- Tools for simplifying Kubernetes clusters usage and administration
- Helm and Helm charts
- Kubernetes graphic UIs
- Kubernetes administrative tools
- Development environments based on Kubernetes
- Azure Container Apps basics and plans
- Consumption-only and workload profiles
- Application versioning
- Interacting with Azure Container Apps
- Deploying your microservice application with Azure Container Apps
- Basic commands and operativity
- Application configuration options and the .yaml format
- Container configuration
- The ingress configuration
- Volume definition and allocation
- Associating an Azure identity to your application
- Summary
- Questions
- Further reading
- Chapter 10: Security and Observability for Serverless and Microservices Applications
- Application Security Best Practices
- Network Security
- Data Security
- Authentication and Authorization
- JSON Web Tokens
- OAuth 2.0 and OpenID Connect (OIDC)
- Securing Dependencies
- Kubernetes and Azure Container Apps Security
- Kubernetes network security
- Azure Container Apps Network Security
- Kubernetes User Security
- Azure Container Apps User Security
- Threat Detection and Mitigation
- Threats
- Event Injection
- Privilege Escalation
- Denial of Service (DoS) Attacks
- Man-in-the-Middle (MitM) Attacks
- Code Injection
- Detection and Mitigation with Web Application Firewalls
- Observability for Serverless and Microservices
- Logging
- Metrics
- Tracing
- Centralized Observability with Azure Monitor
- Summary
- Questions
- Further reading
- Chapter 11: The Car Sharing App
- General architecture description
- Microservices involved
- Authorization microservice
- CarSharer microservice
- CarRequests microservice
- RoutesPlanning microservice
- Email microservice
- The demonstration code
- Summary
- Further reading
- Chapter 12: Simplifying Microservices with .NET Aspire
- Technical requirements
- .NET Aspire features and services
- Service discovery and its role in .NET Aspire.
- Resource integration and automatic resource configuration
- Application telemetry
- Configuring microservices and resources
- Azure Functions integration
- Using .NET Aspire in practice
- RabbitMQ integration
- Deploying a .NET Aspire project
- Summary
- Questions
- Further reading
- Chapter 13: Unlock Your Exclusive Benefits
- Unlock this Book's Free Benefits in 3 Easy Steps
- Other Books You May Enjoy
- Index
- Blank Page
1
Demystifying Serverless Applications
When it comes to software development, we are living in incredible times. With the evolution of cloud platforms and the rise of modern technologies, being a developer nowadays is both a wonderful way to live and a challenging profession to follow. There are so many ways to deliver an application and so many innovative technologies to explore that we may fall into a vicious circle where we focus more on the technologies rather than the actual solution.
This chapter aims to present the serverless architecture and explore how you can use this approach to implement a microservices application. To achieve this, it covers the theory behind serverless and provides an understanding of how it can be a viable alternative for microservices implementation.
The chapter also explores how Microsoft implements Function as a Service (FaaS), using Azure Functions as one of the options for building microservices. Two alternative development platforms will be presented: Visual Studio Code and Visual Studio.
By the end of this chapter, you will understand the different triggers available in Azure Functions and be ready to create your first function.
Technical requirements
This chapter requires Visual Studio 2022 free Community edition or Visual Studio Code. During the chapter, the details about how to debug Azure Functions for each development environment will be presented in the topics. You will also need an Azure account to create the sample environment. You can find the sample code for this chapter at https://github.com/PacktPublishing/Practical-Serverless-and-Microservices-with-Csharp.
What is serverless?
When someone asks you to develop a solution, the last thing they usually care about is how the infrastructure will work. The truth is, even for developers, the most important thing about infrastructure is that it simply works well.
Considering this reality, the possibility of having a cloud provider that dynamically manages server allocation and provisioning, leaving the underlying infrastructure to the provider, might be the best scenario.
That is what serverless architecture promises: a model we can use to build and run applications and services without having to manage the underlying infrastructure ourselves! This approach abstracts server management entirely, allowing developers to focus on their code.
The first cloud solution provider that presented this concept was Amazon, with the launch of AWS Lambda in 2014. After that, Microsoft and Google also provided similar solutions with Microsoft Azure Functions and Google Cloud Functions. As we mentioned before, the focus of this book will be Azure Functions.
There are many advantages that we can consider for using serverless computing. The fact that you do not have to worry about scaling can be considered the main one. Additionally, the cloud solution provider maintains the reliability and security of the environment. Besides that, with this approach, you have the option to pay as you go, so you only pay for what you use, enabling a sustainable model of growth.
Serverless can also be considered a good approach for accelerating software development since you only focus on the code needed to deliver that program. On the other hand, you may have difficulty overseeing a considerable number of functions, so this organization needs to be well handed to not cause problems while creating a solution with many functions.
Since the introduction of serverless, various kinds of functions have been created. These functions act as triggers that are used to start processing. As soon as the function is triggered, the execution can be done in different programming languages.
Now, let us check whether functions can be considered microservices or not.
Is serverless a way to deliver microservices?
If you look at the definition of microservices, you will find the concept of delivering an application as loosely coupled components that represent the implementation of a business capability. You can build something like that with a couple of functions, so yes, serverless is a way to deliver microservices.
Some specialists even consider serverless architecture an evolution of microservices, since the focus of serverless architecture is to deliver scalability in a safe environment, enabling the possibility of a set of functions to independently be developed, tested, and deployed, which brings a lot of flexibility to the software architecture. That is exactly the main philosophy of microservices.
Let us imagine, as an example, a microservice responsible for authenticating users. You may create specific functions for registering, logging, and resetting passwords. Considering that this set of functions can be created in a single serverless project, you have both the flexibility of creating separated functions and the possibility of defining the purpose of the microservice.
The serverless project will naturally support integration with databases, messaging queues, OpenAPI specifications, and other APIs, enabling the design patterns typically needed for a robust microservice architecture. It is also important to mention that keeping microservices isolated, small, and preferably reusable is a best practice worth following.
Now that you understand that you can write microservices using serverless approaches, let us understand how Microsoft Azure presents serverless in its platform.
How does Microsoft Azure present serverless?
In 2016, Microsoft introduced Azure Functions as a Platform-as-a-Service (PaaS) offering designed to deliver FaaS capabilities. This option enables innovation at a scale for business transformation. Today, Azure Functions gives us the opportunity to power up applications using multiple programming languages, including C#, JavaScript, F#, Java, and Python.
One of the standout features of Azure Functions is its seamless integration with other Azure services and third-party APIs. For instance, it can easily connect to different Azure databases (from Azure SQL Server to Azure Cosmos DB), Azure Event Grid for event-based architecture, and Azure Logic Apps for workflow automation. This connectivity simplifies the process of building complex, enterprise-grade applications that leverage multiple services.
Over the years, the possibilities with Azure Functions have evolved. Today, we can even manage stateful workflows and long-running operations, using Azure Durable Functions. With this, you can orchestrate complex processes that can be executed in multiple function executions.
But Microsoft has not only created an environment for coding functions. They have also created a complete pipeline for developers, following the DevSecOps process that's now widely discussed and used in enterprise solutions. Developers can use tools such as Azure Pipelines, GitHub Actions, and other CI/CD services to automate the deployment process. You can also monitor and diagnose events in these functions using Azure Monitor and Application Insights, which facilitate troubleshooting and optimization.
The PaaS solution also enables different setups to adjust scalability and security aspects. Depending on the hosting plan you decide to set, you can have different scaling opportunities, as you can check here:
- Consumption plan: The basic and most cost-effective option to get started with Azure Functions. Ideal for event-driven workloads with automatic scaling.
- Flex Consumption plan: Offers rapid, elastic scaling combined with support for private networking (VNet integration).
- Dedicated plan (App Service plan): Suitable for long-running functions and scenarios requiring more predictable performance and resource allocation.
- Azure Container Apps plan: A solid choice for microservices-based architectures that use multiple technology stacks or require greater flexibility.
- Premium plan: Designed for high-performance scenarios with the ability to scale on demand, providing support for advanced features such as VNet, longer execution times, and pre-warmed instances.
In summary, Microsoft Azure delivers serverless FaaS through Azure Functions, offering a powerful, flexible, and scalable platform that enhances the development and deployment of serverless applications. By using Azure Functions, developers can build and maintain responsive, cost-effective solutions. Now, let us explore how to create an Azure function in the Azure portal.
Creating your first serverless app in Azure
There are not many steps for creating your first serverless app in Azure. You can do it in a straightforward process when using the Azure portal. Follow these steps to get started:
- Log in to the Azure portal. To do so, open your web browser and navigate to the Azure portal at https://portal.azure.com/. Sign in with your Azure account credentials.
- In the Azure portal, click on the Create a resource button located in the upper-left corner.
Figure 1.1: Creating a resource in the Azure portal
- In the Search services and marketplace window, search for Function App and select it from the search results. This service will also be presented in the Popular Azure services section.
- Click...
System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.
File format: ePUB
Copy protection: without DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Use a reader that can handle the file format ePUB, such as Adobe Digital Editions or FBReader – both free (see eBook Help).
- Tablet/Smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePUB works well for novels and non-fiction books – i.e., 'flowing' text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook does not use copy protection or Digital Rights Management
For more information, see our eBook Help page.