Microservices Communication in .NET Using gRPC

Book description

Learn how to implement gRPC on the .NET platform step by step and cover how to use gRPC on .NET, including fundamentals, use cases, and best practices

Key Features

  • Explore all aspects of gRPC implementation on .NET, from the most basic features to advanced ones
  • Discover best practices for using gRPC to make sure that your applications are as efficient and as scalable as possible
  • Identify when gRPC is the best tool for the job and when it isn't

Book Description

Explore gRPC's capabilities for faster communication between your microservices using the HTTP/2 protocol in this practical guide that shows you how to implement gRPC on the .NET platform.

gRPC is one of the most efficient protocols for communication between microservices that is also relatively easy to implement. However, its official documentation is often fragmented and.NET developers might find it difficult to recognize the best way to map between C# data types and fields in gRPC messages. This book will address these concerns and much more. Starting with the fundamentals of gRPC, you'll discover how to use it inside .NET apps. You'll explore best practices for performance and focus on scaling a gRPC app. Once you're familiar with the inner workings of the different call types that gRPC supports, you'll advance to learning how to secure your gRPC endpoints by applying authentication and authorization.

With detailed explanations, this gRPC .NET book will show you how the Protobuf protocol allows you to send messages efficiently by including only the necessary data. You'll never get confused again while translating between C# data types and the ones available in Protobuf.

By the end of the book, you'll have gained practical gRPC knowledge and be able to use it in .NET apps to enable direct communication between microservices.

What you will learn

  • Get to grips with the fundamentals of gRPC and Protobuf
  • Debug gRPC components inside a .NET application to locate and fix errors
  • Understand gRPC best practices, such as performance enhancement
  • Effectively translate between gRPC and native C# code by applying well-known types
  • Secure gRPC communication inside a .NET application
  • Discover how to monitor gRPC on .NET by applying logging and metrics

Who this book is for

This book is for NET developers who are working with microservices and are looking for efficient solutions to facilitate communication between services using gRPC. Anyone who is familiar with microservices architecture and has knowledge of the fundamentals of .NET Core, but not necessarily of gRPC, will also find this book useful.

Table of contents

  1. Microservices Communication in .NET Using gRPC
  2. Contributors
  3. About the author
  4. About the reviewer
  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. Code in Action
    6. Download the color images
    7. Conventions used
    8. Get in touch
    9. Share Your Thoughts
  6. Section 1: Basics of gRPC on .NET
  7. Chapter 1: Creating a Basic gRPC Application on ASP.NET Core
    1. Technical requirements
    2. Introduction to gRPC
      1. gRPC on ASP.NET Core
      2. Using gRPC in your own distributed ASP.NET Core application
    3. Preparing your system
      1. Setting up your environment on Windows
      2. Setting up your environment on Mac
      3. Setting up your environment on Linux
      4. Downloading the .NET SDK (all operating systems)
    4. Setting up a gRPC server
      1. Initializing an ASP.NET Core project via an IDE
      2. Adding gRPC server components to an ASP.NET Core project
      3. Adding some code to use gRPC components
    5. Setting up a gRPC client
      1. Initializing the project for the client application
      2. Adding gRPC client components to the application
      3. Applying gRPC client components to the code
    6. Understanding how proto files generate C# code
      1. Where is auto-generated code stored?
      2. Modifying Protobuf namespaces
    7. Sharing a proto file between the client and the server
      1. Creating a shared class library
      2. Adding shared gRPC components to the class library
      3. Sharing gRPC dependencies between different projects
    8. Running a gRPC service on Mac
      1. Configuring server-side components
      2. Modifying the client-side configuration
    9. Summary
    10. Questions
    11. Further reading
  8. Chapter 2: When gRPC Is the Best Tool and When It Isn't
    1. Technical requirements
    2. Why gRPC is a great tool for microservices
      1. Setting up a solution and shared dependencies
      2. Setting up the status manager microservice
      3. Setting up a REST API gateway service
      4. Launching the distributed application
    3. How gRPC can be a good tool for asynchronous communication
      1. Adding client-streaming and server-streaming gRPC endpoints
      2. Configuring the gRPC client for asynchronous communication
      3. Testing asynchronous gRPC endpoints
    4. Why gRPC is not the best tool for browsers
      1. Setting up a Blazor WebAssembly gRPC client
      2. Modifying the gRPC server to enable gRPC-Web
      3. Launching the gRPC-Web application
    5. Where SignalR would beat gRPC
      1. Setting up a SignalR application
      2. Adding a SignalR client and launching the application
    6. Summary
    7. Questions
    8. Further reading
  9. Chapter 3: Protobuf – the Communication Protocol of gRPC
    1. Technical requirements
    2. The RPC types supported by gRPC
      1. The RPC types that Protobuf supports
      2. Making comments in Protobuf
    3. Reviewing the native Protobuf data types
      1. Integer data types
      2. Non-integer numeric types
      3. Non-numeric data types
      4. Enums
      5. Nested messages
    4. Using collections in Protobuf
      1. Repeated fields
      2. Map fields
    5. Using special keywords in Protobuf
      1. How the oneof keyword can make communication more efficient
      2. Customizing the behavior with the option keyword
    6. Referencing other proto files
      1. Importing external proto packages
      2. Referencing internal proto files
      3. Using proto files as relays
    7. Summary
    8. Questions
    9. Further reading
  10. Section 2: Best Practices of Using gRPC
  11. Chapter 4: Performance Best Practices for Using gRPC on .NET
    1. Technical requirements
    2. Why you need to reuse a gRPC channel
      1. Setting up the server application
      2. Setting up the client application
      3. Comparing the performance of different client types
    3. How to not get held up by a concurrent stream limit
      1. Configuring connection concurrency on the gRPC client
      2. Comparing the performance between a single connection and multiple connections
    4. Ensuring that your connection remains alive
      1. Setting up keep-alive pings on the gRPC client
    5. When streaming is better than individual calls
      1. Setting up a bi-directional streaming RPC
      2. Monitoring the performance of the bi-directional streaming call
    6. Using binary payloads to decrease the data's size
      1. Adding binary fields to Protobuf
    7. Summary
    8. Questions
    9. Further reading
  12. Chapter 5: Applying Versioning to the gRPC API
    1. Technical requirements
    2. Why an API versioning strategy is important
      1. Creating a server application
      2. Implementing the server-side gRPC components
      3. Creating the gRPC client application
      4. Implementing the gRPC client logic
      5. Verifying that the client can talk to the server
    3. What the sequence numbers in the proto file represent
      1. Modifying the Protobuf definition in the server application
      2. Modifying the Protobuf definition in the client application
      3. Launching modified applications
    4. Why you must not modify existing fields in future Protobuf versions
      1. Modifying Protobuf definitions on the client side
      2. Launching the applications
      3. Making further changes to the client application
      4. Re-launching the applications
    5. How to deprecate old, unused fields in gRPC
      1. Applying the reserved keyword to the server-side Protobuf interface
      2. Testing the application
    6. How to factor in API versioning at the design stage
      1. Adding multiple Protobuf versions to the server application
      2. Allowing the server application to use multiple Protobuf versions
      3. Making the gRPC client implementation version-specific
      4. Making a gRPC call to a versioned endpoint
    7. Summary
    8. Questions
    9. Further reading
  13. Chapter 6: Scaling a gRPC Application
    1. Technical requirements
    2. Introduction to load balancing
      1. Adding shared gRPC dependencies
      2. Creating a shared library for server-side application instances
      3. Creating multiple instances of the server-side application
      4. Creating a client application
      5. Running a load-balanced application
    3. Client-side load balancing with gRPC
      1. Updating the NuGet package
      2. Enabling client-side load balancing components
      3. Enabling a DNS resolver for the load balancer
      4. Using a static resolver for the load balancer
      5. Creating custom load balancers and resolvers
    4. Proxy load balancing with gRPC
      1. Building a web application to act as a proxy
      2. Launching the HTTP/2 proxy
    5. Summary
    6. Questions
    7. Further reading
  14. Section 3: In-Depth Look at gRPC on .NET
  15. Chapter 7: Using Different Call Types Supported by gRPC
    1. Technical requirements
    2. Making unary calls on gRPC
      1. Setting up shared gRPC dependencies
      2. Creating server-side implementations of the Protobuf definitions
      3. Building the gRPC client
      4. Applying different types of client-side call implementations
      5. Using gRPC dependencies in the client application
      6. Testing different types of unary call endpoints
    3. Streaming data from the client
      1. Adding a client-streaming call to the server application
      2. Adding client logic for a client-streaming gRPC call
    4. Reading streams from the server
      1. Adding a server-streaming RPC to Protobuf
      2. Setting up a server-streaming call on the server side
      3. Making a server-streaming call from a gRPC client
    5. Enabling bi-directional streaming
      1. Enabling server-side components for bi-directional streaming
      2. Adding a client-side implementation of a bi-directional streaming call
      3. Testing how to stream gRPC calls
    6. Summary
    7. Questions
    8. Further reading
  16. Chapter 8: Using Well-Known Types to Make Protobuf More Handy
    1. Technical requirements
    2. Using nullable types in Protobuf
      1. Setting up a gRPC server application
      2. Examining auto-generated code for wrapper fields
      3. Adding logic to gRPC server application
      4. Setting up shared dependencies
      5. Setting up the gRPC client
      6. Running the application
    3. Using dates and times in Protobuf
      1. Adding timestamp and duration to the server
      2. Applying changes to the gRPC client and launching the app
    4. Exchanging empty messages
      1. Adding the Empty data type to the server-side application
      2. Applying an Empty object on the client
    5. Using loosely typed fields in a Protobuf message
      1. Adding Any and Value data types to the gRPC server
      2. Populating the Any and Value fields from the gRPC client
    6. Summary
    7. Questions
    8. Further reading
  17. Chapter 9: Securing gRPC Endpoints in Your ASP.NET Core Application with SSL/TLS
    1. Technical requirements
    2. Configuring the gRPC client and server for unencrypted communication
      1. The role of TLS certificates
      2. Setting up a gRPC service application
      3. Removing TLS on both HTTP/1.1 and HTTP/2
      4. Exposing Protobuf definitions to clients
      5. Building the client for gRPC communication
      6. Adding the remaining client logic
    3. Creating and trusting a self-signed certificate
      1. The basics of a TLS certificate
      2. Trusting a default development certificate
      3. Creating a self-signed certificate on Windows using PowerShell
      4. Creating a self-signed certificate on Unix using OpenSSL
      5. Applying a certificate on ASP.NET Core
      6. Testing custom certificates and HTTPS redirection
    4. Applying certificate authentication on the gRPC client and server
      1. Configuring the gRPC server for certificate authentication
      2. Enabling certificate authentication on the gRPC client
      3. Testing certificate authentication
    5. Summary
    6. Questions
    7. Further reading
  18. Chapter 10: Applying Authentication and Authorization to gRPC Endpoints
    1. Technical requirements
    2. Setting up the authentication backend
      1. OpenID Connect and OAuth flow
      2. Configuring IdentityServer4
      3. Adding SSO users, roles, and clients
      4. Forcing login redirect on a web application
    3. Restricting gRPC endpoints to authenticated users
      1. Setting up shared gRPC dependencies
      2. Setting up the gRPC server
      3. Enabling gRPC client functionality
    4. Restricting endpoints to authorized users only
      1. Configuring SSO provider to insert role claim into the JWT
      2. Applying different authorization rules to different gRPC endpoints
      3. Applying gRPC client changes
    5. Summary
    6. Questions
    7. Further reading
  19. Chapter 11: Using Logging, Metrics, and Debugging in gRPC on .NET
    1. Technical requirements
    2. Debugging gRPC client components inside a .NET application
      1. Setting up shared gRPC dependencies
      2. Adding a gRPC service application and getting it to display detailed errors
      3. Adding a gRPC client with additional debugging capabilities
      4. Viewing gRPC error information on the client
    3. Debugging gRPC server components inside a .NET application
      1. Viewing the debug output on the gRPC server console
    4. Applying logs to gRPC
      1. Configuring a logger on the gRPC client
      2. Applying a logger on the gRPC server
      3. Testing our log output
    5. Applying metrics to gRPC
      1. Configuring metrics on the gRPC server
      2. Enabling metric collection on the gRPC client
      3. Viewing gRPC metrics
    6. Summary
    7. Questions
    8. Further reading
  20. Assessments
    1. Chapter 1, Creating a Basic gRPC Application on ASP.NET Core
    2. Chapter 2, When gRPC Is the Best Tool and When It Isn't
    3. Chapter 3, Protobuf – the Communication Protocol of gRPC
    4. Chapter 4, Performance Best Practices for Using gRPC on .NET
    5. Chapter 5, Applying Versioning to the gRPC API
    6. Chapter 6, Scaling a gRPC Application
    7. Chapter 7, Using Different Call Types Supported by gRPC
    8. Chapter 8, Using Well-Known Types to Make Protobuf More Handy
    9. Chapter 9, Securing gRPC Endpoints in Your ASP.NET Core Application with SSL/TLS
    10. Chapter 10, Applying Authentication and Authorization to gRPC Endpoints
    11. Chapter 11, Using Logging, Metrics, and Debugging in gRPC on .NET
    12. Why subscribe?
  21. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts

Product information

  • Title: Microservices Communication in .NET Using gRPC
  • Author(s): Fiodar Sazanavets
  • Release date: February 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781803236438