NRPC is a .NET library that provides RPC-like programming interfaces using message queue services or other distributed communication facilities. It allows you to create distributed applications with clean service interfaces while abstracting away the underlying communication mechanisms.
NRPC enables you to:
- Define service contracts using simple .NET interfaces with attributes
- Implement RPC-style method calls over various transport mechanisms
- Support for both synchronous and asynchronous communication patterns
- Generate proxy implementations at runtime
- High-performance service execution with pre-compiled method handlers
The repository consists of several components:
- NRPC.Abstractions: Core interfaces, models, and metadata for RPC communication
- NRPC.Caller: Client-side implementation for sending requests and handling responses (formerly NRPC.Client)
- NRPC.Proxy: Dynamic proxy generation for RPC interfaces
- NRPC.Executor: Server-side implementation for handling requests with compiled service handlers
dotnet add package NRPC- Define your service interface with the ServiceContract attribute:
usingNRPC.Caller;[ServiceContractAtribute]publicinterfaceICalculator{Task<int>Add(intx,inty);Task<string>Concat(stringx,stringy);TaskExecuteVoid(stringcommand);}- Implement the service:
publicclassCalculatorService:ICalculator{publicTask<int>Add(intx,inty)=>Task.FromResult(x+y);publicTask<string>Concat(stringx,stringy)=>Task.FromResult(x+y);publicTaskExecuteVoid(stringcommand){Console.WriteLine($"Executing: {command}");returnTask.CompletedTask;}}- Create and use the RPC caller:
usingNRPC.Caller;usingNRPC.Abstractions;// Create a connection factory (implement IRpcConnectionFactory)varconnectionFactory=newYourRpcConnectionFactory();// Create the caller factoryvarcallerFactory=newRpcCallerFactory<ICalculator>(connectionFactory);// Create and use the callervarcalculator=awaitcallerFactory.CreateCaller();varresult=awaitcalculator.Add(5,3);// Returns 8- Handle requests on the server side:
usingNRPC.Executor;usingNRPC.Abstractions.Metadata;// Create service handlervarserviceHandler=newCompiledServiceHandler<ICalculator>();varservice=newCalculatorService();// Handle incoming requestvarrequest=newRpcRequest{Method="Add",Parameters=newobject[]{5,3}};varresponse=awaitserviceHandler.HandleRequestAsync(service,request);- Interface-based: Define clean service contracts using C# interfaces with ServiceContract attributes
- Dynamic Proxies: Runtime generation of proxy types for seamless method calls
- Task-based API: All remote operations are asynchronous by design
- Transport Abstraction: Independent from the underlying communication mechanism through IRpcConnection
- High Performance: Pre-compiled service handlers for optimal execution speed
- Error Handling: Built-in RPC exception handling with detailed error information
- Metadata System: Rich metadata support for service and method information
- Extensible: Support for custom serialization, transport, and calling adapters
Mark your interfaces with [ServiceContractAttribute] to define RPC service contracts:
[ServiceContractAttribute]publicinterfaceIMyService{Task<string>GetData(intid);}Implement IRpcConnection to define how requests and responses are transmitted:
publicinterfaceIRpcConnection{TaskSendAsync(RpcRequestrequest);Task<RpcResponse>ReceiveAsync(CancellationTokencancellationToken=default);}Use CompiledServiceHandler<T> for high-performance server-side request handling with pre-compiled method invocation.
- .NET 6.0+
- .NET Standard 2.0+
NRPC follows a clean architecture with clear separation of concerns:
- Abstractions Layer: Core interfaces and data models (
RpcRequest,RpcResponse,RpcError) - Metadata System: Service and method metadata for efficient runtime operations
- Caller Layer: Client-side proxy generation and request/response handling
- Executor Layer: Server-side request processing with compiled handlers
- Proxy Layer: Dynamic proxy generation for transparent method calls
The project includes comprehensive tests covering:
- RPC connection workflows
- Client-to-server communication
- Service handler functionality
- Proxy generation and method invocation
- Error handling scenarios
This project is licensed under the terms of the license included in the repository.
Contributions are welcome! Please feel free to submit a Pull Request.