MsgRpc.Generator 1.0.0

Suggested Alternatives

PureRpc

dotnet add package MsgRpc.Generator --version 1.0.0
                    
NuGet\Install-Package MsgRpc.Generator -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MsgRpc.Generator" Version="1.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MsgRpc.Generator" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="MsgRpc.Generator">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MsgRpc.Generator --version 1.0.0
                    
#r "nuget: MsgRpc.Generator, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MsgRpc.Generator@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MsgRpc.Generator&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=MsgRpc.Generator&version=1.0.0
                    
Install as a Cake Tool

MsgRpc

MsgRpc 是一个基于 .NET 的高性能、轻量级 RPC 框架。它通过 Roslyn Source Generators 消除了运行时反射,并深度集成 MessagePack 序列化,旨在为分布式系统提供接近原生调用的通信体验。

🌟 核心特性

  • ⚡ 零反射 (Zero-Reflection):利用源码生成技术在编译期生成代理和分发逻辑,彻底告别运行时反射带来的性能损耗。
  • 🚀 极致性能:深度使用 System.BuffersReadOnlySequence<T>ArrayPool<byte> 内存复用技术,最大限度减少 GC 压力。
  • 📦 紧凑序列化:集成 MessagePack 协议,相比 JSON 拥有更小的包体体积和更快的编解码速度。
  • 🛡️ 弹性设计:内置 ReliableMsgRpcClient 装饰器,支持指数退避(Exponential Backoff)和抖动(Jitter)重试策略。
  • 🧬 异步优先:全链路支持 Task/ValueTaskCancellationToken,适配高并发异步场景。
  • 🛠️ 易于扩展:采用插件式传输层设计,默认支持基于 System.IO.Pipelines 的高性能 TCP 实现。

🏗️ 架构概览

MsgRpc 的设计核心在于将“契约”与“实现”完全解耦:

  1. Contract: 用户定义带特性的接口。
  2. Generator: 编译时自动生成 DTO 结构体、客户端 Proxy 和服务端 Dispatcher。
  3. Transport: 负责字节流在网络中的可靠传输。
  4. Builder: 统一的 Fluent API 引导容器构建与服务注册。

🚀 快速上手

1. 定义服务契约

使用 [MsgRpcService] 标记接口,并为方法分配唯一的 MethodId

[MsgRpcService("WeatherService")]
public interface IWeatherService
{
    [MsgRpc(1)]
    Task<int> GetTemperatureAsync(string city);
}

2. 服务端:注册并启动

通过 MsgRpcServerBuilder 快速构建服务端环境。

var server = new MsgRpcServerBuilder()
    .WithTransport<TcpServerTransport>()
    .WithLogging(log => log.AddConsole())
    .WithOptions(opt => opt.WithCompression(MessagePackCompression.Lz4BlockArray))
    // 'WithWeatherService' 是由 Source Generator 自动生成的扩展方法
    .WithWeatherService<WeatherImplementation>() 
    .Build();

await server.StartAsync(new IPEndPoint(IPAddress.Any, 6000));

3. 客户端:透明调用

像调用本地方法一样进行远程调用。

var client = await new MsgRpcClientBuilder()
    .WithEndPoint(new IPEndPoint(IPAddress.Loopback, 6000))
    .WithRetryPolicy(maxRetries: 3)
    .BuildAsync();

// 'CreateWeatherService' 是生成的扩展方法
var service = client.CreateWeatherService();
int temp = await service.GetTemperatureAsync("Beijing");

📈 性能表现

在基准测试环境(Loopback, 100 并发任务)下,MsgRpc 展现了卓越的吞吐能力:

指标 测试结果
平均 QPS 24,000+
平均延迟 < 4ms (100 并发下)
内存分配 极低 (得益于 ArrayPool 复用)

🛠️ 技术栈


📄 开源协议

本项目采用 MIT License 开源协议。

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on MsgRpc.Generator:

Package Downloads
MsgRpc

High-performance RPC framework for .NET.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 138 3/22/2026 1.0.0 is deprecated because it is no longer maintained.