Beyond HTTP in ASP.NET Core 3 with gRPC

863d6186d3bc32b7c9036101c47d5d5b?s=47 Steve Gordon
September 11, 2019

Beyond HTTP in ASP.NET Core 3 with gRPC

In this session, Steve will share gRPC, a modern high-performance RPC framework for inter-service communication. You'll discover how gRPC stacks up against existing REST-based communication and explore the benefits this technology has to offer, for example, its use of HTTP/2 as its transport protocol and ProtoBuf encoded content for efficient, fast communication.

You'll explore C# examples of how to build client and server gRPC components using ASP.NET Core 3.0, using the blazingly fast Kestrel web server. You'll learn how to define your proto3 service definition (contract) and integrate with MSBuild tooling to code-gen the server and client components you'll need. You'll be amazed at how quickly a service can be created with little effort or boilerplate code required. You'll also see how additional clients can easily be generated for other languages such as Node.js to make connecting polyglot systems a breeze.

It's a great time to begin taking notice of gRPC as a worthy contender to take over from REST for inter-service communication in microservice-based environments. It's time to start letting service talk their own language!

863d6186d3bc32b7c9036101c47d5d5b?s=128

Steve Gordon

September 11, 2019
Tweet

Transcript

  1. BEYOND HTTP IN ASP.NET CORE 3.0 WITH gRPC Steve Gordon

    @stevejgordon | stevejgordon.co.uk http://bit.ly/dotnet-grpc
  2. FULL DISCLOSURE

  3. STORY TIME ARE YOU SITTING COMFORTABLY?

  4. @stevejgordon www.stevejgordon.co.uk HTTP 3rd Party API

  5. WHAT HAPPENED NEXT?...

  6. …BUZZWORDS

  7. @stevejgordon www.stevejgordon.co.uk 3rd Party API 3rd Party API 3rd Party

    API
  8. WHAT IS WRONG WITH REST?

  9. gRPC REMOTE PROCEDURE CALL

  10. @stevejgordon www.stevejgordon.co.uk • Contract based • Supports code generation •

    Multi-platform C#, C++, Dart, Go, Java, Node, Objective-C, PHP, Python, Ruby • HTTP/2 • Uses a binary, Protobuf payload
  11. @stevejgordon www.stevejgordon.co.uk 2001 Google Stubby March 2015 gRPC open sourced

    August 2016 gRPC v1.0.0 Sept 2019 gRPC .NET with .NET Core 3.0
  12. @stevejgordon www.stevejgordon.co.uk Getting Started Create a new project Select 'gRPC

    Service'
  13. @stevejgordon www.stevejgordon.co.uk Project Structure

  14. *.PROTO

  15. @stevejgordon www.stevejgordon.co.uk syntax = "proto3"; option csharp_namespace = "HelloWorld"; package

    Greet; service Greeter { rpc SayHello (HelloRequest) returns (HelloReply) {} } message HelloRequest { string name = 1; } message HelloReply { string message = 1; }
  16. @stevejgordon www.stevejgordon.co.uk syntax = "proto3"; option csharp_namespace = "HelloWorld"; package

    Greet; service Greeter { rpc SayHello (HelloRequest) returns (HelloReply) {} } message HelloRequest { string name = 1; } message HelloReply { string message = 1; }
  17. @stevejgordon www.stevejgordon.co.uk syntax = "proto3"; option csharp_namespace = "HelloWorld"; package

    Greet; service Greeter { rpc SayHello (HelloRequest) returns (HelloReply) {} } message HelloRequest { string name = 1; } message HelloReply { string message = 1; }
  18. @stevejgordon www.stevejgordon.co.uk syntax = "proto3"; option csharp_namespace = "HelloWorld"; package

    Greet; service Greeter { rpc SayHello (HelloRequest) returns (HelloReply) {} } message HelloRequest { string name = 1; } message HelloReply { string message = 1; }
  19. @stevejgordon www.stevejgordon.co.uk syntax = "proto3"; option csharp_namespace = "HelloWorld"; package

    Greet; service Greeter { rpc SayHello (HelloRequest) returns (HelloReply) {} } message HelloRequest { string name = 1; } message HelloReply { string message = 1; }
  20. GETTING STARTED DEMO …

  21. gRPC & MICROSERVICES

  22. @stevejgordon www.stevejgordon.co.uk SERVICE B (NODE) SERVICE C (.NET) SERVER SERVICE

    A (.NET) SERVER CLIENT SERVICE D (GO) SERVER CLIENT CLIENT PROTO A PROTO A PROTO B PROTO B PROTO C PROTO C
  23. @stevejgordon www.stevejgordon.co.uk • Unary Single request with a single response

    – A normal function call • Server Streaming Single request with a stream of responses for the client to consume • Client Streaming Client sends a stream of requests, each receiving a single response • Bidirectional Streaming Client sends a stream of requests and consumes a stream of responses
  24. STREAMING DEMOS

  25. PERFORMANCE

  26. REST Body 10,072 Bytes GRPC Body 1,916 Bytes RESULTS 80.98%

    SMALLER
  27. @stevejgordon www.stevejgordon.co.uk • No need for *.proto • Apply attributes

    to .NET interfaces and classes • Interface(s) define your services • Classes define your message models • Can also use a *.proto to generate the C# code • Share contracts as libraries between .NET servers and clients
  28. CODE-FIRST gRPC DEMO

  29. THE BIG FINISH DEMO!

  30. @stevejgordon www.stevejgordon.co.uk ASP.NET Core Service (.NET) CLIENT weather.proto gRPC Weather

    Service (.NET) SERVER weather.proto HTTP SignalR HTTP gRPC …
  31. @stevejgordon www.stevejgordon.co.uk • Performance • Efficient binary serialisation (low CPU

    overhead) using Protobuf • Smaller payloads • HTTP/2 is fundamentally quicker • Code-gen server and clients for many languages/frameworks • Contract based • Supports first class streaming
  32. @stevejgordon www.stevejgordon.co.uk • Code generation in .NET is C# only

    • Kestrel doesn't support HTTP/2 with TLS on macOS https://docs.microsoft.com/en-us/aspnet/core/grpc/aspnetcore?view=aspnetcore-3.0&tabs=visual-studio#grpc-and-aspnet-core-on-macos • Harder to exercise without tools such as Postman
  33. @stevejgordon www.stevejgordon.co.uk • A great choice for microservice communication •

    A good way to support server-to-server communication in polyglot environments • Easy to get started using the gRPC service template • gRPC .NET libraries make creating servers and clients very easy https://github.com/grpc/grpc-dotnet
  34. THANK YOU @stevejgordon www.stevejgordon.co.uk https://app.pluralsight.com/profile/author/steve-gordon http://bit.ly/dotnet-grpc