ZeroRPC.NET
1.0.0
dotnet add package ZeroRPC.NET --version 1.0.0
NuGet\Install-Package ZeroRPC.NET -Version 1.0.0
<PackageReference Include="ZeroRPC.NET" Version="1.0.0" />
<PackageVersion Include="ZeroRPC.NET" Version="1.0.0" />
<PackageReference Include="ZeroRPC.NET" />
paket add ZeroRPC.NET --version 1.0.0
#r "nuget: ZeroRPC.NET, 1.0.0"
#:package ZeroRPC.NET@1.0.0
#addin nuget:?package=ZeroRPC.NET&version=1.0.0
#tool nuget:?package=ZeroRPC.NET&version=1.0.0
ZeroRPC for .NET
ZeroRPC is a .NET standard library designed to enable seamless communication between multiple applications using the ZeroMQ Dealer-Router pattern. It provides an elegant, attribute-based approach to defining remote methods and namespaces, making it easy to invoke server-side methods directly from client applications via interfaces.
This library offering lightweight, fast, and customizable Remote Procedure Call (RPC) functionality. With ZeroRPC, developers can focus on building distributed systems without worrying about the complexities of communication protocols.
Features
- Attribute-Based RPC: Use
[RemoteMethod],[RemoteService], and[RemoteExecutionRule]attributes to define remote methods and namespaces. - ZeroMQ Dealer-Router Pattern: Leverages ZeroMQ for high-performance, asynchronous messaging.
- Timeout Rules: Configure timeout and retry logic for remote method calls.
- Seamless Integration: Easily integrate with .NET dependency injection for both clients and servers.
- Task and Void Support: Automatically handles return types, including
voidandTask. - Async Operations: Supports asynchronous method calls for non-blocking operations.
- Fire & Forget: Send messages without waiting for a response.
- Exception Handling: Propagates server-side exceptions back to the client.
- Serialization: Uses JSON for serialization and deserialization of method arguments and responses.
- .NET Framework and .NET Core Support: Compatible with both .NET Framework and .NET Core applications.
Installation
Install the library via NuGet:
dotnet add package ZeroRPC.NET
Getting Started
1. Define Remote Interfaces and Methods
Use the [RemoteService], [RemoteMethod] and [RemoteExecutionRule] attributes to define your remote interfaces and methods.
//Client Interface
[RemoteService("IMyService","MyApp")]
public interface IMyService
{
[RemoteExecutionRule(timeoutMillisecond: 5000)]
Task<string> GetDataAsync(int id);
void SendMessage(string message);
}
//Server Interface
namespace MyApp;
public interface IMyService
{
[RemoteMethod("GetDataAsync")]
Task<string> GetDataAsync(int id);
[RemoteMethod("SendMessage")]
void SendMessage(string message);
}
2. Implement Server-Side Logic
Implement the interface on the server side.
public class MyService : IMyService
{
public Task<string> GetDataAsync(int id)
{
return await find(id);
}
public void SendMessage(string message)
{
Console.WriteLine($"Message received: {message}");
}
}
3. Setup the Server
Register the server and bind it to a port.
var services = new ServiceCollection();
var cancellationTokenSource = new CancellationTokenSource();
services.AddSingleton<IExampleService, ExampleService>();
// Register ZeroRPC server to DI
services.AddZeroRpcServer();
var serviceProvider = services.BuildServiceProvider();
var server = serviceProvider.GetRequiredService<ZeroRpcServer>();
server.RunZeroRpcServer(new ConnectionConfiguration("*", 5556), cancellationTokenSource.Token);
4. Setup the Client
Register the client and connect to the server.
services.AddZeroRpcClient<IExampleService>(new ClientConfiguration()
{
Connection = new ConnectionConfiguration("127.0.0.1", 5556, ProtocolType.Tcp),
DefaultTimeout = TimeSpan.FromSeconds(15)
});
Advanced Features
Timeout for Queries
Use [RemoteExecutionRule] to configure retries and timeouts for remote method calls.
[RemoteExecutionRule(timeoutMillisecond: 10000)]
string GetDataAsync(int id);
Exception Handling
ZeroRPC propagates server-side exceptions back to the client. You can handle exceptions as follows:
try
{
var data = await client.GetDataAsync(999);
}
catch (ZeroRpcException ex)
{
Console.WriteLine($"RPC Error: {ex.Message}");
}
Example Code
Server
var services = new ServiceCollection();
services.AddSingleton<IExampleService, ExampleService>();
services.AddZeroRpcServer();
var serviceProvider = services.BuildServiceProvider();
var server = serviceProvider.GetRequiredService<ZeroRpcServer>();
server.RunZeroRpcServer(new ConnectionConfiguration("*", 5556));
Console.ReadLine();
Client
var services = new ServiceCollection();
services.AddZeroRpcClient<IExampleService>(new ClientConfiguration());
var serviceProvider = services.BuildServiceProvider();
var client = serviceProvider.GetRequiredService<IMyService>();
var data = await client.GetDataAsync(42);
Console.WriteLine(data);
Example Project
Run both ZeroRPC.Client and ZeroRPC.Server inside examples folder projects using:
dotnet run
How It Works
ZeroRPC uses the ZeroMQ Dealer-Router pattern for communication:
- Client: Sends requests to the server using a
DealerSocketasynchronously. - Server: Listens for incoming requests using a
RouterSocketasynchronously. - Attributes: Define namespaces, methods, and execution rules for remote calls.
- Serialization: Arguments and responses are serialized using JSON.
- Fire & Forget: Do not wait for response for the methods that has return type of "void" or "Task"
- Request TTL: Server doesn't process incoming requests if the timeout reached (client not listening for respnose anymore)
- Exception Handling: Server-side exceptions are propagated back to the client.
- Dependency Injection: ZeroRPC integrates with .NET's built-in dependency injection for easy service registration and resolution.
- Asynchronous: All methods are asynchronous by default, allowing for non-blocking operations.
- Multiple Protocols: Support for multiple protocols (TCP, IPC, etc.) in the same application.
Benchmark (via BenchmarkDotNet v0.14.0)
Apple M1 Pro, 1 CPU, 10 logical and 10 physical cores
| Method | Mean | Error | StdDev | Median | Gen0 | Allocated |
|---|---|---|---|---|---|---|
| WaitAndReturn | 302.21 us | 5.758 us | 13.906 us | 296.31 us | 0.4883 | 3.1 KB |
| WaitAndReturnAsync | 302.85 us | 6.002 us | 12.790 us | 297.97 us | 0.4883 | 3.86 KB |
| MultipleParameter | 305.07 us | 5.885 us | 11.888 us | 303.22 us | 0.4883 | 3.2 KB |
| WaitAndReturnModelAsync | 300.77 us | 6.006 us | 8.614 us | 298.94 us | 0.4883 | 4 KB |
| FireAndForgetAsync | 77.88 us | 1.421 us | 1.187 us | 77.59 us | 0.2441 | 2.21 KB |
Run time: 00:03:03 (183.33 sec), executed benchmarks: 5
Contributing
Contributions are welcome! If you find a bug or want to add a feature, feel free to open an issue or submit a pull request.
License
ZeroRPC is licensed under the MIT License. See the LICENSE file for details.
Links
Example Repository
Check out the example repository for a full implementation of ZeroRPC: ZeroRPC Example
Roadmap
- Adding more and more unit tests.
- Add support for streaming RPC calls.
- Improve error handling and logging.
- Optimize serialization/deserialization performance.
- Add more examples and documentation.
Feel free to copy and adapt this README for your GitHub repository!
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.3)
- NetMQ (>= 4.0.1.13)
- System.Text.Json (>= 9.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.