Slide 1

Slide 1 text

gRPC & Blazor WebAssembly: A match made in heaven? Christian Weyer https://thinktecture.com/christian-weyer @christianweyer Co-Founder & CTO 1

Slide 2

Slide 2 text

2 gRPC & Blazor WebAssembly A match made in heaven? § Co-Founder & CTO at Thinktecture AG § Personal focus on § Pragmatic end-to-end solutions § Cloud-native & serverless architectures § Mobile & web-based application architectures § Interoperability, cross-device § Independent Microsoft Regional Director § Microsoft MVP for Developer Technologies & Azure ASPInsider, AzureInsider § Google GDE for Web Technologies, Angular [email protected] @christianweyer https://www.thinktecture.com Christian Weyer

Slide 3

Slide 3 text

3 § Server-side [micro]services with APIs § Written in C# with ASP.NET Core § Exposing Web APIs § Web-based clients § JavaScript-based SPAs § Communication via HTTP § Running on-prem, hosted, or in public Cloud § With or without containers § Infrastructure-focused gRPC & Blazor WebAssembly A match made in heaven? Typical SPA architectures for modern business applications HTTP API WS API Service D HTTPS HTTPS WebSocket Service A Service B Service C HTTP API

Slide 4

Slide 4 text

4 § Building SPAs end-to-end § Often too complex, with different stacks → Improving needed knowledge & coding habits § SPA communication with server-side APIs/services § Thinking different in controlled environments → Improving the inner loop gRPC & Blazor WebAssembly A match made in heaven? Beyond our own nose: Reconsidering SPA architectures – in a pragmatic way

Slide 5

Slide 5 text

5 gRPC & Blazor WebAssembly A match made in heaven? Two assumptions One programming language & framework Strongly typed client-server communication

Slide 6

Slide 6 text

6 gRPC & Blazor WebAssembly A match made in heaven? Two assumptions One programming language & framework Strongly typed client-server communication

Slide 7

Slide 7 text

7 § Users want § Cross-Platform, Cross-Device § No installations § Offline (important for many use cases) § Developers think § Web is the solution - but web is different § SPA is the solution - but SPA is different § JavaScript seems to be an issue for lots of .NET developers § There is existing .NET code, what to do about it? § Which technologies to choose? § JavaScript for frontend, .NET for backend? § C# & .NET everywhere? gRPC & Blazor WebAssembly A match made in heaven? C# & .NET everywhere?

Slide 8

Slide 8 text

8 Blazor gRPC & Blazor WebAssembly A match made in heaven?

Slide 9

Slide 9 text

9 § Blazor is a family of web technologies § Server § WebAssembly § Blazor WebAssembly is an SPA framework § Web, SPA & distributed computing knowledge § HTML, CSS knowledge § DOM knowledge § JavaScript for DOM interaction & 3rd party integration § C# & .NET Standard for client-side logic → Full-stack C# possible gRPC & Blazor WebAssembly A match made in heaven? What is Blazor? Mono-based Runtime .NET CLR .NET 6 .NET Framework Blazor WebAssembly Blazor Server Razor Components Browser (WASM / .NET / JS) Browser (JS Stub) Server (.NET)

Slide 10

Slide 10 text

10 § Low-level bytecode for the Web § Bring any language into the browser § Currently targeted at C/C++, Rust et. al. § Achieve superior performance in certain cases § Not a replacement for JavaScript § MVP released, more features being implemented § Goals § Fast, efficient, portable § Readable and debuggable § Use existing sandboxing tools § Don’t break the Web gRPC & Blazor WebAssembly A match made in heaven? WebAssembly (Wasm)

Slide 11

Slide 11 text

11 gRPC & Blazor WebAssembly A match made in heaven? WebAssembly browser support https://caniuse.com/#feat=wasm

Slide 12

Slide 12 text

12 gRPC & Blazor WebAssembly A match made in heaven? Blazor WebAssembly in the browser Blazor DOM Web Assembly .NET Razor Components

Slide 13

Slide 13 text

13 gRPC & Blazor WebAssembly A match made in heaven? Blazor WebAssembly technical architecture in .NET 6.0 (non-AOT) Compiler Parser JIT (.NET Assemblies are interpreted) Web APIs (DOM, IndexedDb, File Storage etc.) Browser blazor. webassembly.js Blazor SPA page (.html) JS Runtime dotnet.wasm dotnet.js mscorlib.dll etc. myapp.dll blazor.boot.json

Slide 14

Slide 14 text

14 gRPC & Blazor WebAssembly A match made in heaven? Two assumptions One programming language & framework Strongly typed client-server communication

Slide 15

Slide 15 text

😊 § JSON major data exchange format § Great tooling § Can handle arbitrary binary data § Highly interoperable § RPC-ish in many cases 🤔 § JSON text format can be verbose § Compression is usually a must § Not every MEP can be easily implemented § Loosely typed § Open API can help here gRPC & Blazor WebAssembly A match made in heaven? Reconsidering Web APIs as service interfaces § Let’s just call them Web APIs (REST, anyone?) 15

Slide 16

Slide 16 text

16 § In Web Services world, we did a lot of contract-first (XSD, WSDL) § In Web API world, we do a lot of code-first (OpenAPI showed up later) gRPC & Blazor WebAssembly A match made in heaven? Service interface modeling: Code-first vs. Contract-first § Code-first § Agile, by writing platform code § Specification/contract gets derived or generated from code § May not lead to highest interoperability § Contract-first § Specification before implementation § Platform code gets generated from contract description § Formal way to ensure highest interoperability

Slide 17

Slide 17 text

17 gRPC & Blazor WebAssembly A match made in heaven? Open API / Swagger code-gen? Service proxy pattern gRPC service? Code-first gRPC service gRPC-Web facade Code-first gRPC-Web client Web APIs & HttpClient gRPC services & strongly-typed clients Our journey

Slide 18

Slide 18 text

18 gRPC & Blazor WebAssembly A match made in heaven? Open API / Swagger code-gen? Service proxy pattern gRPC service? Code-first gRPC service gRPC-Web facade Code-first gRPC-Web client Web APIs & HttpClient gRPC services & strongly-typed clients Our journey

Slide 19

Slide 19 text

19 gRPC gRPC & Blazor WebAssembly A match made in heaven?

Slide 20

Slide 20 text

20 § Initially created for interoperable synchronous [micro]service-to-[micro]service communication § WCF, anyone? § Its presence increases steadily (see GoLang) § Some people like to use gRPC between frontends and backends, as well § Uses HTTP/2 & Protocol Buffers § gRPC is genuinely contract-first gRPC & Blazor WebAssembly A match made in heaven? gRPC

Slide 21

Slide 21 text

21 § gRPC has different types of method call patterns § Unary (request-response) § Server streaming § Client streaming § Bi-directional streaming gRPC & Blazor WebAssembly A match made in heaven? gRPC call patterns

Slide 22

Slide 22 text

22 gRPC & Blazor WebAssembly A match made in heaven? ProtoBuf (Protocol Buffers) § Language-neutral, platform-neutral, extensible mechanism for describing & serializing structured data § Like XML/XSD, but smaller, faster, and partly simpler § Uses integer tokens to identify members, not names § Support for code generation for many languages, including C# § Also: (binary) serialization format on the wire message Person { required string name = 1; required int32 id = 2; optional string email = 3; }

Slide 23

Slide 23 text

23 § .NET supports gRPC since .NET Core 3.0 § Always contract-first: write .proto file first § Implement gRPC service based on generated base class § Get typed client proxy generated on the client side based § C# code gets generated when the .proto file changes § “Add Service Reference…” in VS can handle the client side for you gRPC & Blazor WebAssembly A match made in heaven? gRPC in .NET

Slide 24

Slide 24 text

24 gRPC & Blazor WebAssembly A match made in heaven? Open API / Swagger code-gen? Service proxy pattern gRPC service? Code-first gRPC service gRPC-Web facade Code-first gRPC-Web client Web APIs & HttpClient gRPC services & strongly-typed clients Our journey

Slide 25

Slide 25 text

25 § gRPC does not work for browsers § We are in .NET world on both sides § Services § Clients § We are in a controlled environment § 👉‍ Let’s do .NET, all the way! gRPC & Blazor WebAssembly A match made in heaven? OK – This is not what we want…

Slide 26

Slide 26 text

26 § We can have gRPC in .NET without having to write .proto files § Improving the inner loop § Open-source community project protobuf-net.Grpc § Implement gRPC service based on .NET interface § Get strongly-typed client proxy at runtime based on .NET interface, e.g., in Blazor § Can build on WCF contract semantics § ServiceContract, DataContract § Offers its own semantics § Service, ProtoContract gRPC & Blazor WebAssembly A match made in heaven? Code-First gRPC in .NET & Blazor

Slide 27

Slide 27 text

27 gRPC & Blazor WebAssembly A match made in heaven? Open API / Swagger code-gen? Service proxy pattern gRPC service? Code-first gRPC service gRPC-Web facade Code-first gRPC-Web client Web APIs & HttpClient gRPC services & strongly-typed clients Our journey

Slide 28

Slide 28 text

28 § Browsers are the de-facto frontend platform today § Native gRPC does not work in browsers (due to restrictions for HTTP/2 framing in browsers) § gRPC-Web is implementation of gRPC for browser clients § gRPC-Web clients connect to gRPC services via a gRPC-Web proxy § .NET offers § Server-side ASP.NET Core middleware for proxy § .NET client library (beyond JavaScript) § Unary calls & server-streaming supported gRPC & Blazor WebAssembly A match made in heaven? gRPC-Web

Slide 29

Slide 29 text

29 gRPC & Blazor WebAssembly A match made in heaven? Open API / Swagger code-gen? Service proxy pattern gRPC service? Code-first gRPC service gRPC-Web facade Code-first gRPC-Web client Web APIs & HttpClient gRPC services & strongly-typed clients Our journey

Slide 30

Slide 30 text

30 gRPC & Blazor WebAssembly A match made in heaven? Open API / Swagger code-gen? Service proxy pattern gRPC service? Code-first gRPC service gRPC-Web facade Code-first gRPC-Web client Web APIs & HttpClient gRPC services & strongly-typed clients Our journey

Slide 31

Slide 31 text

31 Extra time gRPC & Blazor WebAssembly A match made in heaven?

Slide 32

Slide 32 text

32 § In contract-first world, you share the .proto files § In code-first, you share .NET code § For testing and debugging tools, we need runtime information from the gRPC services § gRPC server reflection protocol is a standard § gRPCurl, gRPCui, Postman are prominent testing tools § protobuf-net.Grpc has reflection support § builder.Services.AddCodeFirstGrpcReflection(); § app.MapCodeFirstGrpcReflectionService(); gRPC & Blazor WebAssembly A match made in heaven? Reflection & testing with gRPC code-first

Slide 33

Slide 33 text

33 § Transferring a stream of data over a longer period of time § Only server streaming supported in gRPC-Web § Can be used for real-time push § Server with protobuf-net.Grpc § IAsyncEnumerable and CallContext in service interface § Client with protobuf-net.Grpc § Use CallOptions to enable cancellation with CancellationTokenSource gRPC & Blazor WebAssembly A match made in heaven? Streaming data with gRPC-Web code-first

Slide 34

Slide 34 text

34 § Authentication and authorization are not gRPC-Web’s business § Usually, OAuth2- / OIDC-based token § Use ASP.NET Core’s features as with Web APIs § app.UseAuthentication(); § app.UseAuthorization(); § [Authorize] § In Blazor, use AuthorizationMessageHandler to add bearer token to gRPC-Web requests gRPC & Blazor WebAssembly A match made in heaven? Authentication & authorization for gRPC-Web

Slide 35

Slide 35 text

35 § gRPC JSON transcoding for .NET – coming with .NET 7 § Doesn’t replace MVC or Minimal APIs § Only supports JSON, very opinionated about how Protobuf maps to JSON § gRPC-Web: browser apps call gRPC services with gRPC-Web clients and Protobuf § JSON transcoding: browser apps call gRPC services as Web APIs with JSON § Important: Does not work with code-first gRPC & Blazor WebAssembly A match made in heaven? “I am glad you asked…”: gRPC & Web APIs

Slide 36

Slide 36 text

36 Wrap-up gRPC & Blazor WebAssembly A match made in heaven?

Slide 37

Slide 37 text

37 gRPC & Blazor WebAssembly A match made in heaven? SPA-to-API communication – in controlled scenarios Web API gRPC (-Web) SignalR Different API styles possible (RPC, REST, GraphQL, OData etc.) RPC Bi-directional RPC HTTP HTTP/2 WebSockets (transport protocol fallbacks) Stateless Stateless Stateful Code-first Contract-first with external tooling Contract-first Code-first with 3rd party lib Code-first Code sharing of DTOs Code-gen possible Full code sharing w/ Code-First Code-gen with Contract-first Not intended for code sharing No client lib needed (fetch) Needs client lib Needs client lib Usually request-response Push possible (e.g. via long polling) Can do streaming Several message exchange patterns Push possible Efficient data streaming Dual one-way Optimized for push Broadcast possible Strong tooling ecosystem Evolving tooling ecosystem No tooling ecosystem

Slide 38

Slide 38 text

38 § SPA architectures reconsidered (in controlled scenarios) § Blazor WebAssembly gives us C# & .NET power for SPAs in the browser § ASP.NET Core gives us gRPC & gRPC-Web power for SPA-to-service communication § Choose wisely J gRPC & Blazor WebAssembly A match made in heaven? Summary – a match made in heaven?

Slide 39

Slide 39 text

Thank you! Christian Weyer https://thinktecture.com/christian-weyer @christianweyer [email protected] Demo: https://github.com/thinktecture/blazor-wasm-grpc-code-first Blazor Knowledge Hub: https://www.thinktecture.com/de/asp-dotnet-core-blazor/ Articles about Blazor, PWA, Web Components, Angular, .NET Core & more: https://www.thinktecture.com/en/newsletter 39

Slide 40

Slide 40 text

40 § ASP.NET Core Blazor § https://dotnet.microsoft.com/apps/aspnet/web-apps/blazor § Protocol Buffers § https://developers.google.com/protocol-buffers § gRPC § https://grpc.io/docs/what-is-grpc/introduction/ § https://grpc.io/docs/languages/csharp/basics/ § gRPC in .NET Core § https://docs.microsoft.com/en-us/aspnet/core/tutorials/grpc/grpc-start § gRPC-Web in .NET Core § https://docs.microsoft.com/en-us/aspnet/core/grpc/browser § gRPC Code-First in .NET Core § https://github.com/protobuf-net/protobuf-net.Grpc § gRPC JSON transcoding for .NET § https://devblogs.microsoft.com/dotnet/announcing-grpc-json-transcoding-for-dotnet/ gRPC & Blazor WebAssembly A match made in heaven? Resources