积极答复者
Grpc BlazorServer WEB服务端 不工作

问题
-
1、创建 Blazor Server 应用,目标框架 .net 5.0,勾选 配置 HTTPS(H), 创建。
2、创建 Protos\greet.proto,内容如下:
syntax = "proto3"; option csharp_namespace = "GrpcGreeter"; package greet; // The greeting service definition. service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply); } // The request message containing the user's name. message HelloRequest { string name = 1; } // The response message containing the greetings. message HelloReply { string message = 1; }
3、添加引用包,项目文件如下:
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net5.0</TargetFramework> </PropertyGroup> <ItemGroup> <None Remove="Protos\greet.proto" /> </ItemGroup> <ItemGroup> <PackageReference Include="Grpc.AspNetCore" Version="2.39.0" /> <PackageReference Include="Grpc.AspNetCore.Web" Version="2.39.0" /> </ItemGroup> <ItemGroup> <Protobuf Include="Protos\greet.proto" GrpcServices="Server" /> </ItemGroup> <ItemGroup> <Folder Include="Protos\" /> </ItemGroup> </Project>
4、创建文件夹 Services,并再该文件夹中,添加类 GreeterService,代码如下:
public class GreeterService : Greeter.GreeterBase { private readonly ILogger<GreeterService> _logger; public GreeterService(ILogger<GreeterService> logger) { _logger = logger; } public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context) { return Task.FromResult(new HelloReply { Message = "Hello " + request.Name }); } }
5、在 _Imports.razor,文件中,添加引用
@using Grpc.AspNetCore.Web
@using Grpc.AspNetCore6、在 Startup.cs 文件中,添加
services.AddGrpc();
app.UseGrpcWeb(new GrpcWebOptions { DefaultEnabled = true });
endpoints.MapGrpcService<GreeterService>();代码如下:
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); services.AddServerSideBlazor(); services.AddSingleton<WeatherForecastService>(); services.AddGrpc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); //app.UseGrpcWeb(); app.UseGrpcWeb(new GrpcWebOptions { DefaultEnabled = true }); app.UseEndpoints(endpoints => { endpoints.MapBlazorHub(); endpoints.MapFallbackToPage("/_Host"); endpoints.MapGrpcService<GreeterService>(); }); } }
运行后,https://localhost:5001,该地址,还是无法连接?不知道是哪里出错了?
项目地址:https://github.com/Sg3361382/GrpcBlazorServer
2021年8月31日 10:26
答案
全部回复
-
谢谢您!vs版本2022 pre,net 6,反序列化异常。
以下代码,在vs2019,net 5,正常通过,请问:在net 6 中,要怎样序列化 proto 文件的消息?
using var channel = GrpcChannel.ForAddress("https://localhost:5001"); var client = new Greeter.GreeterClient(channel); var reply = await client.SayHelloAsync( new HelloRequest { Name = "GreeterClient" }); Console.WriteLine("Greeting: " + reply.Message);
- 已编辑 诺奇玛浓 2021年9月6日 5:28
2021年9月6日 5:27