13 Building C# Clients and Servers with .Net and Windows
This chapter introduces how Apache Thrift enables polyglot, cross-platform RPC in the .NET/CLR ecosystem, parallel to the Java/JVM world, with a focus on building C# clients and servers on Windows. It guides you through configuring a Windows development environment, using Visual Studio and NuGet, and shows how Thrift’s language-agnostic IDL and generated code simplify interop between C# and many other languages. Beyond setup and a first working example, the chapter surveys the C# transport, protocol, and server options, and culminates with a practical long‑polling design built on named pipes.
You begin by preparing the essentials: Visual Studio with the .NET Framework SDK, the thrift.exe IDL compiler, and the Thrift C# runtime (built from source or installed via NuGet). A step-by-step “Hello World” solution organizes three projects—client, server, and a shared service library—driven by a simple hello.thrift IDL. Visual Studio is wired to invoke the Thrift compiler via a pre-build event, the generated C# stubs are compiled into the shared library, and both client and server reference that library and Thrift.dll. The server implements a handler and runs over TServerSocket using TSimpleServer, while the client connects with TSocket, layers TBufferedTransport and TBinaryProtocol, and exercises the service end to end.
The chapter then details the .NET Thrift stack: base and server transports (TServerSocket, TTLSServerSocket, TNamedPipeServerTransport), endpoint transports (TSocket, TTLSSocket, THttpClient, TNamedPipeClientTransport, TMemoryBuffer), layered transports (TBufferedTransport, TFramedTransport), protocols (TBinaryProtocol, TCompactProtocol, TJSONProtocol), and servers (TSimpleServer, TThreadedServer, TThreadPoolServer) with trade‑offs for concurrency and throughput. An advanced example models long polling for streaming trade reports: a PNWF TradeStream IDL, NuGet-managed Thrift dependency, a multithreaded named‑pipe server (TThreadedServer + TNamedPipeServerTransport + TCompactProtocol) with a TServerEventHandler to subscribe/unsubscribe clients, and a TradeEventGenerator using ThreadLocal events and concurrent collections. A compact client connects via TNamedPipeClientTransport and repeatedly calls GetNextTrade, demonstrating scalable, asynchronous-style delivery within an RPC architecture.
- Building the C# library with Visual Studio
- Creating a new C# solution
- Creating an RPC client project
- Client, Server and Service Interface Projects
- Adding Apache Thrift IDL to the Solution
- Saving files in ASCII format
- Adding an IDL Compiler pre-build event
- IDL Compiler C# output
- Successful helloService build
- Successful server build
- Testing helloServer.exe
- C# Server Transport Hierarchy
- C# End Point Transport Hierarchy
- C# Layered Transport Hierarchy
- The NuGet Package Manager Console
- The tradeService Project
- Three concurrent clients connected to the named pipe tradeServer
Summary
This chapter has covered:
- How to configuring Apache Thrift for use with .Net on Windows.
- How to create Apache Thrift applications with Visual Studio.
- How to build C# RPC clients and servers with Apache Thrift.
- The various End Point Transports available in C#.
- The Layered Transports available in C#.
- The C# supported Protocols.
- How to use C# concurrency features with multithreaded Apache Thrift servers.
- How to use long polling to supply asynchronous data feeds to clients.
Programmer’s Guide to Apache Thrift ebook for free