Skip to content

AnupamAkib/gRPC-streaming-with-JsonTranscoding-SwaggerUI-support

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

gRPC Proof of Concept (POC)

This repository is a proof of concept (POC) demonstrating how to implement and test different gRPC communication patterns in .NET, including:

  • ✅ Unary gRPC
  • ✅ Server‑side streaming
  • ✅ Bi‑directional streaming
  • ✅ JSON Transcoding (gRPC ↔ REST)

The solution also includes a .NET Console–based gRPC client used to test streaming scenarios that are not easily testable via browsers or REST tools.


📂 Solution Structure

The solution contains two projects:

GrpcService.sln
│
├── GrpcService              # gRPC Server (.NET)
│   ├── Protos
│   │   └── v1
│   │       ├── auth.proto
│   │       ├── greet.proto
│   │       ├── stream.proto
│   │       └── bidirectional.proto
│   │
│   ├── Services
│   │   ├── AuthService.cs
│   │   ├── GreeterService.cs
│   │   ├── LocationService.cs
│   │   └── LocationTrackerService.cs
│   │
│   ├── appsettings.json
│   └── Program.cs
│
└── GrpcLocationClient       # gRPC Client (.NET Console App)
    ├── Clients
    │   ├── Interfaces
    │   │   └── IClient.cs
    │   ├── BidirectionalStreamClient.cs
    │   └── ServerStreamClient.cs
    │
    ├── Protos
    │   └── v1
    │       ├── stream.proto
    │       └── bidirectional.proto
    │
    └── Program.cs

🔌 gRPC Communication Patterns Covered

1️⃣ Unary gRPC

  • Request–Response communication
  • Implemented in AuthService
  • Used for JWT authentication
Client → Request → Server
Client ← Response ← Server

JSON Transcoding (REST support)

Unary gRPC is exposed as a REST endpoint using JSON Transcoding.

import "google/api/annotations.proto";

service Authorizer {
  rpc JwtAuth (AuthRequest) returns (AuthResponse) {
    option (google.api.http) = {
      post: "/api/v1/auth",
      body: "*"
    };
  }
}

✅ Can be tested via:

  • Postman (gRPC protocol)
  • Web browser / REST client (HTTP)

2️⃣ Server‑Side Streaming

  • Client sends one request
  • Server sends multiple responses as a stream
Client → Request
Server → Stream(Response)

Used to demonstrate scenarios like:

  • Continuous updates
  • Notifications
  • Data feeds

Tested via the Console gRPC Client.


3️⃣ Bi‑Directional Streaming

  • Client and server send streams simultaneously
  • Full‑duplex communication over a single HTTP/2 connection
Client ⇄ Server (Streams in both directions)

Example use case implemented:

  • Client sends live user location (lat, long, id)
  • Server streams back nearby bus locations

This pattern is ideal for:

  • Real‑time tracking
  • Live maps
  • IoT & telemetry systems

Tested via a .NET Console gRPC Client.


🧪 Testing Strategy

Unary gRPC

  • ✅ Postman (gRPC)
  • ✅ REST via JSON Transcoding
  • ✅ Browser (HTTP endpoint)

Streaming gRPC

  • ❌ Not supported via REST / browsers
  • ✅ Tested using .NET Console Client

The client application contains separate implementations for:

  • Server‑side streaming
  • Bi‑directional streaming

🧠 Why Console Client?

Streaming gRPC requires:

  • Persistent HTTP/2 connections
  • Open streams for continuous data flow

These features are not supported by typical REST tools or browsers, so a native gRPC client is required for:

  • Server‑side streaming
  • Bi‑directional streaming

🧱 Technologies Used

  • .NET (ASP.NET Core gRPC)
  • gRPC
  • Protocol Buffers (proto3)
  • JSON Transcoding
  • HTTP/2
  • Swagger / OpenAPI (for transcoded HTTP APIs)
  • Postman (for gRPC testing)

📌 Notes

  • Protobuf files are versioned under Protos/v1
  • JSON Transcoding is intentionally applied only to unary gRPC
  • Streaming endpoints are pure gRPC by design
  • Swagger UI is enabled only for transcoded unary gRPC endpoints

🔀 JSON Transcoding & Swagger Support

This project integrates JSON Transcoding to expose selected unary gRPC endpoints as traditional HTTP APIs.

Configuration

builder.Services
    .AddGrpc()
    .AddJsonTranscoding();

Swagger Integration

Swagger is enabled to visualize and test HTTP-mapped unary gRPC endpoints:

builder.Services.AddGrpcSwagger();
builder.Services.AddSwaggerGen();

In development mode:

  • Swagger UI is available for HTTP APIs
  • gRPC Reflection is enabled for Postman and tooling support
if (app.Environment.IsDevelopment())
{
    app.MapGrpcReflectionService().AllowAnonymous();
    app.UseSwaggerUI();
}

Important Limitations

  • ❌ Server-side streaming and bi-directional streaming cannot be exposed via JSON Transcoding
  • ❌ Swagger UI does not support streaming gRPC
  • ✅ JSON Transcoding is suitable only for unary gRPC

This design keeps streaming endpoints efficient and protocol-pure while still offering REST compatibility where needed.


🚀 Future Enhancements

  • Authentication & authorization for streaming endpoints
  • gRPC‑Web support for browser clients
  • Redis / Geo‑spatial integration for location queries
  • Observability (logging, metrics, tracing)

📷 Project Architecture

Refer to the attached screenshot for a visual overview of the solution structure and architecture.


✅ Summary

This POC demonstrates:

  • Proper separation of gRPC server and client
  • Real‑world usage of all major gRPC patterns
  • Practical JSON Transcoding integration
  • Streaming‑first design for real‑time systems

It serves as a solid foundation for building production‑grade, real‑time gRPC‑based systems in .NET.

About

A simple POC on gRPC unary, server-side streaming & bidirectional streaming. JSON transcoding is integrated with the unary gRPC along with SwaggerUI support. A gRPC client is developed with .NET console app to check gRPC server-side data streaming.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages