0%

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