locked
Grpc BlazorServer WEB服务端 不工作 RRS feed

  • 问题

  • 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.AspNetCore  

    6、在 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

答案

  • 您好,

    我认为可能是由于版本的更新,某些配置出现了更新的情况. 针对于你这个错误,请问你的详细错误信息是什么?具体错误出现在哪边?是否有依赖包?

    2021年9月7日 5:20

全部回复

  • 您好,

    请问您有什么报错信息吗? 你可以按F12,在Network 中查看是否有详细的报错信息.

    2021年9月1日 8:18
  • Unhandled exception. Grpc.Core.RpcException: Status(StatusCode="Internal", Detail="Failed to deserialize response message.")
    2021年9月2日 5:40
  • 您好,

    请问您是什么操作系统以及是什么版本的? 可以给出详细的错误信息吗? 安装了什么安装包?

    猜测可能是反序列化异常,或者可能与其他软件包安装不兼容有关.

    2021年9月2日 8:31
  • 谢谢您!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:27
  • 您好,

    我认为可能是由于版本的更新,某些配置出现了更新的情况. 针对于你这个错误,请问你的详细错误信息是什么?具体错误出现在哪边?是否有依赖包?

    2021年9月7日 5:20