NXDO.gRPC 1.0.0

dotnet add package NXDO.gRPC --version 1.0.0
                    
NuGet\Install-Package NXDO.gRPC -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="NXDO.gRPC" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NXDO.gRPC" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="NXDO.gRPC" />
                    
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 NXDO.gRPC --version 1.0.0
                    
#r "nuget: NXDO.gRPC, 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 NXDO.gRPC@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=NXDO.gRPC&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=NXDO.gRPC&version=1.0.0
                    
Install as a Cake Tool

关于 NXDO.gRPC

NXDO.gRPC 是一个基与 AspNetCore gRPC 的开发平台。可以在<font style='color:red;'>不使用 proto 文件</font>的前提下处理的(反)序列化。支持原生 gRPC 方法,并包含有 MessagePack,GoogleBuf,自定义序列化过程的功能实现 (自定义序列化,请咨询作者 )。

NXDO.gRPC 支持在 .Net Framework4.7.2 环境下使用。功能上做了裁减:仅支持(反)序列化,提供 gRPC 客户端请求的功能。

序列化功能

1.媒介对象

public enum Sex { Man = 0, Woman }
public class Person {
    public string Name { get; set;}
    public int Age { get; set; }
    public Sex Sex { get; set; }

    //循环引用 
    public IList<Person> Children { get; private set; } = new List<Person>();
}

2.(反)序列化

//Serialize
var personStub = RpcObjectHelper.CreateStubObject<Person>(p => { 
                                                          p.Name = "Anita"; 
                                                          p.Age = 28;
                                                          p.Sex = Sex.Woman; });
var children = (personStub as Person).Children;
children.Add(new Person { Name = "jack", Age = 2, Sex = Sex.Man });
children.Add(new Person { Name = "rose", Age = 4, Sex = Sex.Woman });

var data = personStub.ToByteArray(); //default is MessagePack
//or
//var data = personStub.ToByteArray(OptionSerializerMode.ProtoBuf);

//Deserialize, 自动检查  MessagePack or ProtoBuf
var newStub = RpcObjectHelper.CreateStubObject<Person>();
var newPerson = newStub.MergeFrom<Person>(data);

gRPC 服务端

3.服务定义

namespace gRPC.TestSvc;

public class MyHello { public string Name {get; set;}}
public class MyReply { public string Message {get; set;}}

public class RpcHello {
    [HttpPost] //Http1请求, 如在 NXDO.WebAPI 环境下,使用该环境的 JSON(微软的,不是 newtone) 设置
    public virtual Task<MyReply> SayHello(MyHello request, ServerCallContext context){
        Console.WriteLine($"service:{request.Name}");
        return Task.FromResult(new MyReply {
            Message = "nxdo.grpc:Hello " + request.Name
        });
    }
}

4.1 - 服务启动

//RpcObjectHelper.RegisterClasses(typeof(MyHello), typeof(MyReply)); //gRPC上下环境中注册存根,建议调用

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGrpcBoth(); //AddGrpc & AddGrpcMethodProvider
//builder.Services.AddGrpcCofing(opt => {}); //config serviceSide

var app = builder.Build();
app.MapGrpcService<RpcHello>();
app.Run();

4.2 - 服务启动 with NXDO.WebAPI

//入口主函数 
static void Main(string[] args) {
    NXDO.WebAPI.ApiHttpHost.Run<TestStartup>(args);
}

public class TestStartup : NXDO.WebAPI.ApiStartup {
    public override void ConfigureServices(IServiceCollection services) {
        //RpcObjectHelper.RegisterClasses(typeof(MyHello), typeof(MyReply)); //gRPC上下环境中注册存根,建议调用
        services.AddGrpcBoth(); //AddGrpc & AddGrpcMethodProvider
        base.ConfigureServices(services);
    }

    public override void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime hostApplicationLifetime) {
        base.Configure(app, env, hostApplicationLifetime);
        app.UseEndpoints(endpoints => {
            endpoints.MapGrpcService<RpcHello>();
        });
    }
}

4.3 - 服务映射的扩展

//rpc.json
{
    "grpc": {
        //gRPC 服务类型
        "services": [
            "gRPC.TestSvc.RpcHello, gRPC.TestSvc"
        ],

        //上下文环境中注册对象存根
        "classes": [
            "gRPC.TestSvc.MyHello, gRPC.TestSvc",
            "gRPC.TestSvc.MyReply, gRPC.TestSvc"
        ]
    }
}
using NXDO.gRPC;
endpoints.MapGrpcServices("rpc.json");

//or 
//app.MapGrpcServices("rpc.json");

gRPC 客户端

5.1 客户端 (Http2) 请求

using var client = RpcClientHelper.Create("https://localhost:5002");
//client.AddGrpcCofing(opt => {}); //config clientSide

var rpcMethod = "gRPC.TestSvc.RpcHello/SayHello"; //classFullName + "/" + methodName
var rs = await client.CallUnary<MyHello, MyReply>(rpcMethod, new MyHello { Name = "NoneProtoFile" });
Console.WriteLine(rs.Message);

5.2 客户端 (Http1) 请求

var myHello = { Name : 'axios' };
var url = "https://localhost:5002/grpc/gRPC.TestSvc.RpcHello/SayHello";
axios.post(url, myHello)
     .then(resp => { console.log(resp.data); })
     .catch(ex => {});    
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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 is compatible.  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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
.NET Framework net472 is compatible.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 75 7/5/2025

首次发布