gRPC with Service Fabric Stateful Service

Lili Xu
2 min readNov 8, 2020

This post is mainly about integrating gRPC with service fabric stateful service using Kestrel and custom ServicePartitionClient.

Add New Kestrel Listener and endpoint for gRPC

First we need to add a new endpoint to ServiceManifest.xml

<Endpoint Protocol=”https” Name=”GrpcEndpoint” />

For gRPC, it has restriction for the URL being in the root. so that we just add the kestrel listener without bringing in the UseServiceFabricIntegration middleware. This middleware is suggested by service fabric in here.

The Starup.cs could be found here in ASP.NET Core doc.

So far we completed the server side setup.

Create Partition Client Factory for gRPC

As we disabled the service fabric integration middleware, manual address resolving and failure retrying is now required on client side.

Service fabric provides ServicePartitionClient<TCommunicationClient> for injecting custom client factory and setup for talking to the desired service on chosen replica.

As gRPC protobuf provides us GeneratedGrpcClient class inherits Grpc.Core.ClientBase, we’ll need to have

GrpcCommunicationClient : ICommunicationClient

GrpcCommunicationClientFactory : ICommunicationClientFactory<GrpcCommunicationClient>

GrpcCommunicationClient could also take a generic Type parameter for serving multiple gRPC services

Next step, as suggested. we could register the GrpcCommunicationClientFactory as a singleton for managing the gRPC channels.

Get Service Partition Client and Send gRPC Call

Finally, we get our custom Partition Client and send the call to the stateful service using gRPC and integrated SF naming service address resolving.

Reference

--

--