https://github.com/Lakerfield/Lakerfield.Rpc
https://github.com/Lakerfield/Lakerfield.Rpc
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/Lakerfield/Lakerfield.Rpc
- Owner: Lakerfield
- Created: 2024-07-24T14:28:45.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-17T11:51:34.000Z (9 months ago)
- Last Synced: 2025-09-11T06:20:04.257Z (4 months ago)
- Language: C#
- Size: 193 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- RSCG_Examples - laker
README
# Lakerfield.Rpc
Lakerfield.Rpc is a flexible RPC implementation for .NET, enabling remote procedure calls over WebSockets (with optional TCP support) using BSON for message serialization via the `MongoDB.Bson / Lakerfield.Bson` library.
## Features
- Define a single interface to represent your RPC contract; source generators automatically generate all client and server code based on that interface
- Supports WebSocket transport (TCP support available)
- Compact, binary messages with BSON serialization
## Requirements
- .NET 8.0 or higher
## Installation
Install the core RPC library:
```bash
dotnet add package Lakerfield.Rpc
dotnet add package Lakerfield.Rpc.SourceGenerator
```
Install the client RPC libary:
```bash
dotnet add package Lakerfield.Rpc.SourceGenerator
dotnet add package Lakerfield.Rpc.WebSocketClient
```
Install the server RPC libary:
```bash
dotnet add package Lakerfield.Rpc.SourceGenerator
dotnet add package Lakerfield.Rpc.WebSocketServer
```
## Dependencies
- **Lakerfield.Bson**: A fork of MongoDB.Bson (with only a namespace change) to avoid conflicts when using MongoDB.Bson in your application, used for efficient BSON serialization.
- **System.Reactive**: Required by both WebSocket client and server for reactive message handling.
- **Microsoft.AspNetCore.App**: Framework reference for the WebSocket server.
## Usage Example (WebSocket)
### Shared libary with the interface and bson configuration
```csharp
[RpcService]
public interface IRpcTestService
{
Task CompanyFindById(Guid id);
}
public static partial class RpcTestServiceBsonConfigurator
{
static partial void PreConfigure()
{
// configure bson mapping of the models
Lakerfield.Bson.Serialization.BsonClassMap.RegisterClassMap(AutoMap);
}
}
```
### WebSocket Client
```csharp
var networkClient = new NetworkClient(new Uri("ws://localhost/ws"));
var client = new RpcTestServiceClient(networkClient);
var company = await client.CompanyFindById(Guid.NewGuid());
```
```csharp
[RpcClient]
public partial class RpcTestServiceClient : IRpcTestService
{
// implementation is generated by sourcegenerator
}
```
### WebSocket Server
```csharp
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRpcWebSocketServer();
var app = builder.Build();
app.UseWebSockets(new WebSocketOptions
{
KeepAliveInterval = TimeSpan.FromSeconds(120),
AllowedOrigins = { "*" } // Pas dit aan voor productie!
});
app.UseRpcWebSocketServer("/ws");
await app.RunAsync();
```
```csharp
[RpcServer]
public partial class RpcTestWebSocketServiceServer : Lakerfield.Rpc.LakerfieldRpcWebSocketServer
{
public override ILakerfieldRpcClientMessageHandler CreateConnectionMessageRouter(LakerfieldRpcWebSocketServerConnection connection)
{
return new ClientConnectionMessageHandler(connection as LakerfieldRpcWebSocketServerConnection);
}
public partial class ClientConnectionMessageHandler // implements IRpcTestService
{
public async Task CompanyFindById(System.Guid id)
{
return new Models.Company()
{
Id = id.ToString(),
Name = "The company",
};
}
}
```
## Example Project
See the complete WebSocket demo in the `RpcDemo` projects of the rosacode repository:
https://github.com/Lakerfield/rosacode
## Contributing
Contributions, issues, and feature requests are welcome. Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
## License
This project is licensed under the MIT License. See [LICENSE](LICENSE) for details.