C#,ProtoBuf protobuf,Http1AndHttp2AndHttp3, configure Kestrel to Listen HTTP1+HTTP2+HTTP3 so that protobuf service generated data can be accessed by chrome
Install-Package Google.Protobuf Install-Package Grpc.Net.Client Install-Package Grpc.Tools Install-Package Grpc.AspnetCore Install-Package Grpc.AspNetCore.Server.Reflection Install-Package Microsoft.AspNetCore.Grpc.JsonTranscoding -Version 10.0.15 Install-Package Google.Api.CommonProtos
1.Add Book.Proto file in D:\C\GrpcBookService\Protos\Book.proto, D:\C\GrpcBookService\Protos\Book.proto syntax = "proto3"; import "google/api/annotations.proto"; option csharp_namespace = "GrpcBookService"; package book; message BookProto { int64 id=1; string name=2; string isbn=3; string bookabstract=4; string author=5; string comment=6; string content=7; string summary=8; string title=9; string topic=10; } message GetBooksListRequest{ int64 cnt=1; repeated BookProto books=2; } message GetBooksListResponse{ repeated BookProto books=1; } service BookService { rpc GetBooksList(GetBooksListRequest) returns (GetBooksListResponse){ option (google.api.http)={ get:"/getbooks/{cnt}" }; } } 2.Set Book.proto property,Build Action choose Protobuf Compiler,Grpc Stub classes,choose Server and Client; 3.Rebuild 4.Add D:\C\GrpcBookService\Services\GrpcBookService.cs using GrpcBookService; using Grpc.Core; namespace GrpcBookService.Services; public class GrpcBookService : BookService.BookServiceBase { static long id = 1; private static (long, long) GetStartEnd(long cnt = 1000000) { var end = Interlocked.Add(ref id, cnt); var start = end - cnt; return (start, end); } public override Task<GetBooksListResponse> GetBooksList(GetBooksListRequest request, ServerCallContext context) { Int64 cnt = request.Cnt > 0 ? request.Cnt : 1000000; var (start, end) = GetStartEnd(cnt); List<BookProto> bksList = new List<BookProto>((int)cnt); for (long i = start; i < end; i++) { bksList.Add(new BookProto() { Id = i, Name = $"Name_{i}", Author = $"Author_{i}", Comment = $"Comment_{i}", Content = $"Content_{i}", Bookabstract = $"Abstract_{i}", Isbn = $"ISBN_{i}_{Guid.NewGuid():N}", Summary = $"Summary_{i}", Title = $"Title_{i}", Topic = $"Topic_{i}" }); } var response = new GetBooksListResponse(); response.Books.Clear(); response.Books.AddRange(bksList); Console.WriteLine($"In Grpc services,{DateTime.Now},Start:{start},end:{end - 1}"); return Task.FromResult(response); } } 5.Configure Kestrel to Listen HTTP1HTTP2HTTP3 at the same time D:\C\GrpcBookService\Program.cs using GrpcBookService; using Microsoft.AspNetCore.Server.Kestrel.Core; namespace GrpcBookService { public class Program { public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); builder.WebHost.ConfigureKestrel(x => { x.ListenAnyIP(5001, h => { h.UseHttps(); h.Protocols = HttpProtocols.Http1AndHttp2AndHttp3; }); }); builder.Services.AddGrpc(x => { x.MaxReceiveMessageSize = null; x.MaxSendMessageSize = null; }).AddJsonTranscoding(); builder.Services.AddGrpcReflection(); var app = builder.Build(); app.MapGrpcService<GrpcBookService.Services.GrpcBookService>(); app.MapGrpcReflectionService(); app.MapGet("/", () => $"{DateTime.Now},you can access the service via client!"); app.Run(); } } }

In Chrome,input
https://127.0.0.1:5001/getbooks/1000



浙公网安备 33010602011771号