mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2026-06-13 14:38:43 +00:00
RPC Test
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using Echo.Model.Grpc;
|
||||
using Google.Protobuf;
|
||||
using Google.Protobuf.Collections;
|
||||
using Grpc.Core;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using static Echo.Model.Grpc.EchoService;
|
||||
|
||||
namespace Echo
|
||||
{
|
||||
public class EchoServiceImpl : EchoServiceBase
|
||||
{
|
||||
public override Task<BytesResponse> EchoBytes(BytesRequest request, ServerCallContext context)
|
||||
{
|
||||
var rt = new BytesResponse();
|
||||
rt.Data = ByteString.CopyFrom(request.Data.ToArray());
|
||||
return Task.FromResult(rt);
|
||||
}
|
||||
|
||||
public override Task<DocumentsResponse> EchoDocuments(DocumentsRequest request, ServerCallContext context)
|
||||
{
|
||||
var rt = new DocumentsResponse();
|
||||
rt.Docs.AddRange(request.Docs);
|
||||
return Task.FromResult(rt);
|
||||
}
|
||||
|
||||
public override Task<EnumResponse> EchoEnumArray(EnumArrayRequest request, ServerCallContext context)
|
||||
{
|
||||
var rt = new EnumResponse();
|
||||
rt.DocTypes.AddRange(request.DocTypes);
|
||||
return Task.FromResult(rt);
|
||||
}
|
||||
|
||||
public override Task<IntArrayResponse> EchoIntArray(IntArrayRequest request, ServerCallContext context)
|
||||
{
|
||||
var rt = new IntArrayResponse();
|
||||
rt.Array.AddRange(request.Array);
|
||||
return Task.FromResult(rt);
|
||||
}
|
||||
|
||||
public override Task<DocMapResponse> EchoMap(DocMapRequest request, ServerCallContext context)
|
||||
{
|
||||
var rt = new DocMapResponse();
|
||||
foreach(var kv in request.Map)
|
||||
rt.Map.Add(kv.Key, kv.Value);
|
||||
|
||||
return Task.FromResult(rt);
|
||||
}
|
||||
|
||||
public override Task<StringArrayResponse> EchoStringArray(StringArrayRequest request, ServerCallContext context)
|
||||
{
|
||||
var rt = new StringArrayResponse();
|
||||
rt.Array.AddRange(request.Array);
|
||||
return Task.FromResult(rt);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Google.Protobuf" Version="3.33.2" />
|
||||
<PackageReference Include="Grpc" Version="2.46.6" />
|
||||
<PackageReference Include="Grpc.AspNetCore" Version="2.71.0" />
|
||||
<PackageReference Include="Grpc.Tools" Version="2.76.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.8" />
|
||||
<PackageReference Include="protobuf-net" Version="3.2.56" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Protobuf Include="echo.proto" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,6 @@
|
||||
@RPC.gRPC_HostAddress = http://localhost:5193
|
||||
|
||||
GET {{RPC.gRPC_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
@@ -0,0 +1,18 @@
|
||||
|
||||
|
||||
using Echo;
|
||||
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.WebHost.ConfigureKestrel(o =>
|
||||
{
|
||||
// Listen on 5300 and force HTTP/2 (h2c)
|
||||
o.ListenAnyIP(5300, lo => lo.Protocols = HttpProtocols.Http2);
|
||||
});
|
||||
|
||||
builder.Services.AddGrpc();
|
||||
var app = builder.Build();
|
||||
app.MapGrpcService<EchoServiceImpl>();
|
||||
app.Urls.Add("http://0.0.0.0:5300");
|
||||
app.Run();
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "http://localhost:5193",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "https://localhost:7236;http://localhost:5193",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package gpmodel;
|
||||
|
||||
option csharp_namespace = "Echo.Model.Grpc";
|
||||
|
||||
// ========================= Enums =========================
|
||||
|
||||
enum Currency {
|
||||
CURRENCY_IQD = 0;
|
||||
CURRENCY_CNH = 1;
|
||||
CURRENCY_USD = 2;
|
||||
CURRENCY_EUR = 3;
|
||||
CURRENCY_JPY = 4;
|
||||
CURRENCY_GBP = 5;
|
||||
}
|
||||
|
||||
enum DocType {
|
||||
DOC_TYPE_QUOTE = 0;
|
||||
DOC_TYPE_ORDER = 1;
|
||||
DOC_TYPE_INVOICE = 2;
|
||||
DOC_TYPE_CREDIT_NOTE = 3;
|
||||
}
|
||||
|
||||
enum PaymentMethod {
|
||||
PAYMENT_METHOD_CASH = 0;
|
||||
PAYMENT_METHOD_CARD = 1;
|
||||
PAYMENT_METHOD_WIRE = 2;
|
||||
PAYMENT_METHOD_CRYPTO = 3;
|
||||
PAYMENT_METHOD_OTHER = 4;
|
||||
}
|
||||
|
||||
enum LineType {
|
||||
LINE_TYPE_PRODUCT = 0;
|
||||
LINE_TYPE_SERVICE = 1;
|
||||
LINE_TYPE_DISCOUNT = 2;
|
||||
LINE_TYPE_SHIPPING = 3;
|
||||
}
|
||||
|
||||
// Variant.Kind
|
||||
enum Kind {
|
||||
KIND_NULL = 0;
|
||||
KIND_BOOL = 1;
|
||||
KIND_INT64 = 2;
|
||||
KIND_UINT64 = 3;
|
||||
KIND_DOUBLE = 4;
|
||||
KIND_DECIMAL = 5;
|
||||
KIND_STRING = 6;
|
||||
KIND_BYTES = 7;
|
||||
KIND_DATETIME = 8;
|
||||
KIND_GUID = 9;
|
||||
}
|
||||
|
||||
// ========================= Variant =========================
|
||||
//
|
||||
// C# Variant:
|
||||
// Tag: Kind
|
||||
// Bool: bool?
|
||||
// I64: long?
|
||||
// U64: ulong?
|
||||
// F64: double?
|
||||
// Str: string?
|
||||
// Bytes: byte[]?
|
||||
// Dt: DateTime? -> int64 (ticks or epoch millis)
|
||||
// Guid: byte[]?
|
||||
|
||||
message Variant {
|
||||
Kind tag = 1;
|
||||
bool bool_val = 2; // optional in C#
|
||||
int64 i64_val = 3; // optional in C#
|
||||
uint64 u64_val = 4; // optional in C#
|
||||
double f64_val = 5; // optional in C#
|
||||
string str_val = 6; // optional in C#
|
||||
bytes bytes_val = 7; // optional in C#
|
||||
int64 dt_val = 8; // optional in C# (DateTime)
|
||||
bytes guid_val = 9; // optional in C# (Guid as 16-byte array)
|
||||
}
|
||||
|
||||
// Optional helpers mirroring MetaEntry / ExtEntry (not required for RPC)
|
||||
message MetaEntry {
|
||||
string key = 1;
|
||||
Variant value = 2;
|
||||
}
|
||||
|
||||
message ExtEntry {
|
||||
string key = 1;
|
||||
Variant value = 2;
|
||||
}
|
||||
|
||||
// ========================= Address & Party =========================
|
||||
|
||||
message Address {
|
||||
string line1 = 1;
|
||||
string line2 = 2; // optional in C#
|
||||
string city = 3;
|
||||
string region = 4;
|
||||
string country = 5;
|
||||
string postal_code = 6; // optional in C#
|
||||
}
|
||||
|
||||
message Party {
|
||||
uint64 id = 1;
|
||||
string name = 2;
|
||||
string tax_id = 3; // optional in C#
|
||||
string email = 4; // optional in C#
|
||||
string phone = 5; // optional in C#
|
||||
Address address = 6; // optional in C#
|
||||
string preferred_language = 7; // optional in C#
|
||||
}
|
||||
|
||||
// ========================= DocumentHeader =========================
|
||||
//
|
||||
// DateTime/DateTime? -> int64 (ticks or epoch millis, your choice)
|
||||
|
||||
message DocumentHeader {
|
||||
bytes doc_id = 1;
|
||||
DocType type = 2;
|
||||
int32 version = 3;
|
||||
int64 created_at = 4; // DateTime
|
||||
int64 updated_at = 5; // optional in C#
|
||||
Currency currency = 6;
|
||||
string notes = 7; // optional in C#
|
||||
map<string, Variant> meta = 8;
|
||||
}
|
||||
|
||||
// ========================= LineItem, Payment, Attachment =========================
|
||||
|
||||
message LineItem {
|
||||
int32 line_no = 1;
|
||||
LineType type = 2;
|
||||
string sku = 3;
|
||||
string description = 4;
|
||||
double qty = 5;
|
||||
string qty_unit = 6;
|
||||
double unit_price = 7;
|
||||
double vat_rate = 8; // optional in C#
|
||||
double discount = 9; // optional in C#
|
||||
map<string, Variant> ext = 10;
|
||||
}
|
||||
|
||||
message Payment {
|
||||
PaymentMethod method = 1;
|
||||
double amount = 2;
|
||||
string reference = 3; // optional in C#
|
||||
int64 timestamp = 4; // DateTime
|
||||
double fee = 5; // optional in C#
|
||||
Currency currency = 6;
|
||||
}
|
||||
|
||||
message Attachment {
|
||||
string name = 1;
|
||||
string mime_type = 2;
|
||||
bytes data = 3;
|
||||
}
|
||||
|
||||
// ========================= Top-level BusinessDocument =========================
|
||||
|
||||
message BusinessDocument {
|
||||
DocumentHeader header = 1;
|
||||
Party seller = 2;
|
||||
Party buyer = 3;
|
||||
repeated LineItem items = 4;
|
||||
repeated Payment payments = 5;
|
||||
repeated Attachment attachments = 6;
|
||||
repeated int32 risk_scores = 7;
|
||||
}
|
||||
|
||||
// ========================= Echo Service Messages =========================
|
||||
//
|
||||
// C# signatures:
|
||||
//
|
||||
// byte[] EchoBytes(byte[])
|
||||
// BusinessDocument[] EchoDocuments(BusinessDocument[] docs)
|
||||
// int[] EchoIntArray(int[] array)
|
||||
// string[] EchoStringArray(string[] array)
|
||||
// Map<string, BusinessDocument> EchoMap(Map<string, BusinessDocument> map)
|
||||
// DocType EchoEnumArray(DocType[] docTypes);
|
||||
|
||||
// --- Bytes ---
|
||||
message BytesRequest {
|
||||
bytes data = 1;
|
||||
}
|
||||
|
||||
// --- Bytes ---
|
||||
message BytesResponse {
|
||||
bytes data = 1;
|
||||
}
|
||||
|
||||
|
||||
// --- Documents ---
|
||||
message DocumentsRequest {
|
||||
repeated BusinessDocument docs = 1;
|
||||
}
|
||||
|
||||
message DocumentsResponse {
|
||||
repeated BusinessDocument docs = 1;
|
||||
}
|
||||
|
||||
// --- Int array ---
|
||||
message IntArrayRequest {
|
||||
repeated int32 array = 1;
|
||||
}
|
||||
|
||||
message IntArrayResponse {
|
||||
repeated int32 array = 1;
|
||||
}
|
||||
|
||||
// --- String array ---
|
||||
message StringArrayRequest {
|
||||
repeated string array = 1;
|
||||
}
|
||||
|
||||
message StringArrayResponse {
|
||||
repeated string array = 1;
|
||||
}
|
||||
|
||||
// --- Map<string, BusinessDocument> ---
|
||||
message DocMapRequest {
|
||||
map<string, BusinessDocument> map = 1;
|
||||
}
|
||||
|
||||
message DocMapResponse {
|
||||
map<string, BusinessDocument> map = 1;
|
||||
}
|
||||
|
||||
// --- Enum array (returns a single DocType, e.g. last element) ---
|
||||
message EnumArrayRequest {
|
||||
repeated DocType doc_types = 1;
|
||||
}
|
||||
|
||||
message EnumResponse {
|
||||
repeated DocType doc_types = 1;
|
||||
}
|
||||
|
||||
// ========================= Echo Service =========================
|
||||
|
||||
service EchoService {
|
||||
rpc EchoBytes (BytesRequest) returns (BytesResponse);
|
||||
rpc EchoDocuments (DocumentsRequest) returns (DocumentsResponse);
|
||||
rpc EchoIntArray (IntArrayRequest) returns (IntArrayResponse);
|
||||
rpc EchoStringArray (StringArrayRequest) returns (StringArrayResponse);
|
||||
rpc EchoMap (DocMapRequest) returns (DocMapResponse);
|
||||
rpc EchoEnumArray (EnumArrayRequest) returns (EnumResponse);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"version": "3.0",
|
||||
"defaultProvider": "cdnjs",
|
||||
"libraries": []
|
||||
}
|
||||
Reference in New Issue
Block a user