Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Software Architecture with C# 12 and .NET 8 - Fourth Edition

You're reading from  Software Architecture with C# 12 and .NET 8 - Fourth Edition

Product type Book
Published in Feb 2024
Publisher Packt
ISBN-13 9781805127659
Pages 756 pages
Edition 4th Edition
Languages
Authors (2):
Gabriel Baptista Gabriel Baptista
Profile icon Gabriel Baptista
Francesco Abbruzzese Francesco Abbruzzese
Profile icon Francesco Abbruzzese
View More author details

Table of Contents (26) Chapters

Preface 1. Understanding the Importance of Software Architecture 2. Non-Functional Requirements 3. Managing Requirements 4. Best Practices in Coding C# 12 5. Implementing Code Reusability in C# 12 6. Design Patterns and .NET 8 Implementation 7. Understanding the Different Domains in Software Solutions 8. Understanding DevOps Principles and CI/CD 9. Testing Your Enterprise Application 10. Deciding on the Best Cloud-Based Solution 11. Applying a Microservice Architecture to Your Enterprise Application 12. Choosing Your Data Storage in the Cloud 13. Interacting with Data in C# – Entity Framework Core 14. Implementing Microservices with .NET 15. Applying Service-Oriented Architectures with .NET 16. Working with Serverless – Azure Functions 17. Presenting ASP.NET Core 18. Implementing Frontend Microservices with ASP.NET Core 19. Client Frameworks: Blazor 20. Kubernetes 21. Case Study 22. Case Study Extension: Developing .NET Microservices for Kubernetes 23. Answers
24. Other Books You May Enjoy
25. Index

Implementing Microservices with .NET

In Chapter 11, Applying a Microservice Architecture to Your Enterprise Application, you learned the theory and basic concepts of microservices. In this chapter, you will learn how to put into practice those general concepts and tools to implement microservices in .NET. This way, you will have a practical understanding of how high-level architectural decisions translate into concrete .NET code.

The focus of this chapter is on worker microservices; that is, microservices that are not part of the public interface of your application. Other kinds of microservices will be focused on in other chapters. Worker microservices process jobs that are not connected to a specific user. They somehow prepare the data that will be used by frontend microservices to satisfy all user requests. They are the assembly line of each application, so their design priorities are efficiency in both communication and local processing, together with protocols that ensure...

Technical requirements

This chapter requires the free Visual Studio 2022 Community edition or better with all database tools installed. The code for this chapter is available at https://github.com/PacktPublishing/Software-Architecture-with-C-Sharp-12-and-.NET-8-4E.

Experimenting with message brokers also requires the installation of the RabbitMQ message broker (version 3.9 or higher), which in turn requires the previous installation of a 64-bit version of Erlang. An adequate Erlang version for RabbitMQ 3.9 can be downloaded from https://github.com/erlang/otp/releases/download/OTP-24.0.6/otp_win64_24.0.6.exe. The RabbitMQ Windows installer can be downloaded from https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.9.5/rabbitmq-server-3.9.5.exe. We recommend you launch both installations from an administrative account.

Three complete examples of worker microservices are in the A worker microservice with ASP.NET core and A worker microservice based on RabbitMQ sections...

Communication and data serialization

As explained in the Microservice design principles subsection of Chapter 11, Applying a Microservice Architecture to Your Enterprise Application, requests to a microservices-based application can’t cause long chains of recursive microservices calls.

In fact, each call adds both a wait time and a communication time to the actual processing time, thus leading to unacceptable levels of overall response time, as shown in the following figure.

Shape, circle  Description automatically generated

Figure 14.1: Tree of blocking RPC calls

Messages 1-6 are triggered by a request to the A microservice and are sent in sequence, so their processing times sum up to the response time. Moreover, once sent, message 1 from microservice A remains blocked until it receives the last message (6); that is, it remains blocked for the whole lifetime of the overall recursive communication process.

Microservice B remains blocked twice, waiting for an answer to a request it issued. The first time...

Implementing worker microservices with ASP.NET Core

In order to avoid blocking the caller’s synchronous request for too much time, an ASP.NET Core-based solution requires the implementation of an internal queue where it can store all received messages. This way, when a message is received, it is immediately enqueued without processing it, so that a “received” response can be immediately returned.

Therefore, the application level needs a repository interface that handles the queue. Here is a possible definition of this interface:

public interface IMessageQueue
{
    public Task<IList<QueueItem>> Top(int n);
    public Task Dequeue(IEnumerable<QueueItem> items);
    public Task Enqueue(QueueItem item);
}

Where:

  • QueueItem is a class that contains all request information
  • Enqueue adds a new message to the queue
  • Top returns the first n queue items without removing them from the queue
  • Dequeue removes the first...

Implementing microservices with .NET worker services and message brokers

This section explains the modifications needed to use a message broker instead of gRPC communication with an internal queue. This kind of solution is usually more difficult to test and design but allows for better horizontal scaling.

The message broker used in the code is RabbitMQ. However, we could also replace it with Azure Service Bus using the code available in the GitHub repository associated with the book. The next subsection explains how to install RabbitMQ on your development machine. We used RabbitMQ to give the reader the opportunity to install and study it, since Azure Service Bus needs less configuration and is immediately ready to use. In an actual production system, one might choose RabbitMQ, just so you are not tied to a specific cloud provider, because while Azure Service Bus is available just on Azure, RabbitMQ can be installed in any cloud or on-premises environment.

Installing RabbitMQ...

Summary

In this chapter, we analyzed various options for efficient internal microservices communication. We explained the importance of a binary serialization that is interoperable and that ensures compatibility with previous message versions, and we described ProtoBuf in detail.

We analyzed the limits of RPC communication and why data-driven communication must be preferred. Then, we focused on how to achieve reliable asynchronous communication and efficient distributed transactions.

After having described the conceptual problems and techniques of reliable asynchronous communication, we looked at two architectures. The first one was based on gRPC, ASP.NET Core, and internal queues, and the second one was based on message brokers like RabbitMQ and .NET worker services.

The chapter explained, using practical examples, how to implement all the communication protocols that have been discussed and the architectural options for implementing worker microservices that are available...

Questions

  1. Why are queues so important in microservices communication?
  2. How do we recall another .proto file?
  3. How can we represent a TimeSpan in the ProtoBuf language?
  4. What are the advantages of ProtoBuf and gRPC over other binary options?
  5. What are the advantages of using message brokers instead of internal queues?
  6. Why is it acceptable to use a blocking gRPC call to enqueue a message in a recipient queue?
  7. How do we enable .proto file code generation in a .NET project file?
  8. How do I send a message on a RabbitMQ channel with the official .NET client?
  9. How do you ensure that a message sent on a RabbitMQ channel is safely saved on disk using the official .NET client?

Further reading

Learn more on Discord

To join the Discord community for this book – where you can share feedback, ask questions to the authors, and learn about new releases – follow the QR code below:

https://packt.link/SoftwareArchitectureCSharp12Dotnet8

lock icon The rest of the chapter is locked
You have been reading a chapter from
Software Architecture with C# 12 and .NET 8 - Fourth Edition
Published in: Feb 2024 Publisher: Packt ISBN-13: 9781805127659
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}