Demystifying gRPC in .Net by John Staveley

johnstaveley 118 views 17 slides May 24, 2024
Slide 1
Slide 1 of 17
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17

About This Presentation

Intrigued by why some of the world's largest companies (Netflix, Google, Cisco, Twitter, Uber etc) are using gRPC? In this demo based talk we delve into the world of gRPC in .Net, what it does and why we should use it. We compare the interface with both Rest and graphQL. We will show you how to ...


Slide Content

Demystifying gRPC By John Staveley MVP 23/05/2023 https://uk.linkedin.com/in/johnstaveley/ @ johnstaveley

Overview Who uses it? What is it? Why use it? Basic gRPC method calls in C# Tooling in VS Streaming Using over the web

Who uses gRPC ? created in 2015

What is gRPC ? Http2 Protocol buffers Binary formatting TLS Support for cross cutting concerns such as authentication and error handling Good language support: C# / .NET, C++, Dart, Go, Java, Kotlin, Node, Objective-C, PHP, Python, Ruby, JavaScript

Why? gRPC vs Both cross platform gRPC 7 to 10 times faster than REST REST has native browser support, gRPC has support via gRPC -Web REST has loose contracts, gRPC has strict contracts Json is human readable REST is better for massive payloads

Both: gRPC and Released 2015/2016 Advantages over Rest Interface Definition Language Open source Publish Single Endpoint

Why? gRPC vs Transport Agnostic (Mostly http) Uses Json Can get all data together (easier to combine method calls) Weaker coupling One-way streaming Caching of Get requests does not work by default Used over Http/2 Uses Protobuf (Smaller packet size, faster serialisation) Single method at a time Stronger coupling Two-way streaming

How? Getting started with C# NuGet packages Default project ( ASP.NET Core gRPC service) Setting up your service project The .proto file Startup

DEMO 1: Hello world Server Basic gRPC project Greeter service Call using postman or grpccurl

Protocol buffers Interface Definition Language Types: bool, string, bytes, float, double, int32, int64, uint32, uint64, sint32, sint64. Full list here: https://protobuf.dev/programming-guides/proto3/#scalar Other types: import " myproject / other_protos.proto "; E.g. Timestamp Field numbers must be unique!

DEMO 2: Client Console project Install packages Add a service reference of type gRPC Add client code DEMO: Calling new method from client code

Extending the basic service Timestamp Enum Reserve keyword Optional keyword Empty type Streaming (1 way / 2 way) Authentication https://protobuf.dev/reference/csharp/api-docs/

Cloud gRPC support EC2 and Application Load Balancer Azure App Service Http = 2.0 Http 2.0 Proxy = On/ gRPC Only End to End Encryption = On Both support unary and streaming modes

Calling from the web Server uses grpc -web proxy Install-Package grpc.aspnetcore.web Web client Install Npm packages Talk through and execute Generate.ps1 Talk through send.js

In Summary gRPC : High speed, well supported Good for streaming and polyglot services Used on the web via grpc -web proxy Well supported with tooling in VS, Postman etc

Resources https://stackoverflow.blog/2022/11/28/when-to-use-grpc-vs-graphql/ https://app.pluralsight.com/library/courses/aspdotnet-core-6-using-grpc/ https://grpc.io/docs/ Source code: https://github.com/johnstaveley/DataLogger Slides: https://www.slideshare.net/johnstaveley/

Any Questions? @ johnstaveley Demo code: https://github.com/johnstaveley/DataLogger/ Slides: https://www.slideshare.net/johnstaveley/ Connect: https://uk.linkedin.com/in/johnstaveley/