从 .Net Framework 客户端调用 gRPC 服务器时出现异常

Q-b*_*uit 5 .net c# protocol-buffers grpc

我有一个在 .Net Core 中实现的 gRPC 服务器,我想与在 .Net Framework 中运行的控制台应用程序联系。

这是该问题的工作存储库:

https://github.com/q-bertsuit/grpc-framework-core-issue

以下 .Net Core 客户端工作正常:

class Program
{
    static async Task Main(string[] args)
    {
        
        var input = new MyModel();

        var channel = GrpcChannel.ForAddress("https://localhost:5001");
        var client = new Test.TestClient(channel);

        var reply = await client.SetTestAsync(input);

        Console.ReadLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

以下 .Net Framework 客户端引发异常:

class Program
{
    static void Main(string[] args)
    {
        Channel channel = new Channel("https://localhost:5001", ChannelCredentials.Insecure);
        
        var    client = new Test.TestClient(channel);

        MyModel request = new MyModel();

        var reply = client.SetTest(request);

        channel.ShutdownAsync().Wait();


        Console.ReadLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

发送请求时出现以下异常:

Grpc.Core.RpcException:'Status(StatusCode =“不可用”,Detail =“服务的DNS解析失败:https:// localhost:5001”,DebugException =“Grpc.Core.Internal.CoreErrorDetailException:{“创建”:” @1600333768.055000000","描述":"解析器瞬时故障","文件":"T:\src\github\grpc\workspace_csharp_ext_windows_x86\src\core\ext\filters\client_channel\resolving_lb_policy.cc","file_line":215 ,"referenced_errors":[{"created":"@1600333768.055000000","description":"服务的 DNS 解析失败: https://localhost:5001","file":"T:\src\github\grpc\ workspace_csharp_ext_windows_x86\src\core\ext\filters\client_channel\resolver\dns\c_ares\dns_resolver_ares.cc","file_line":378,"grpc_status":14,"referenced_errors":[{"已创建":"@1600333768.055000000", "description":"C-ares 状态不是 ARES_SUCCESS qtype=A name=https://localhost:5001 is_balancer=0: 无法联系 DNS 服务器","file":"T:\src\github\grpc\workspace_csharp_ext_windows_x86 \src\core\ext\filters\client_channel\resolver\dns\c_ares\grpc_ares_wrapper.cc","file_line":287,"referenced_errors":[{"创建":"@1600333768.055000000","描述":"C- ares 状态不是 ARES_SUCCESS qtype=AAAA name=https://localhost:5001 is_balancer=0: 无法联系 DNS 服务器","file":"T:\src\github\grpc\workspace_csharp_ext_windows_x86\src\core\ext\过滤器\client_channel\resolver\dns\c_ares\grpc_ares_wrapper.cc","file_line":287}]}]}]}")'

在服务器上我收到此错误:

dbug:Microsoft.AspNetCore.Server.Kestrel[17] 连接 id“0HM2QU64LCR9J”错误请求数据:“无法识别的 HTTP 版本:'HTTP/2.0'”Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException:无法识别的 HTTP 版本:'HTTP /2.0' 在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser 1.RejectUnknownVersion(Byte* version, Int32 length) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser1.ParseRequestLine(TRequestHandler 处理程序,Byte* 数据,Int32 长度) 在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser 1.ParseRequestLine(TRequestHandler handler, ReadOnlySequence1&缓冲区、SequencePosition& 消耗、SequencePosition& 检查)在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser 1.Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.IHttpParser<TRequestHandler>.ParseRequestLine(TRequestHandler handler, ReadOnlySequence1& 缓冲区、SequencePosition& 消耗、SequencePosition& 检查)在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection。 TakeStartLine(ReadOnlySequence 1& buffer, SequencePosition& consumed, SequencePosition& examined) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.ParseRequest(ReadOnlySequence1&缓冲区,SequencePosition&消耗,SequencePosition&检查)在Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.TryParseRequest(ReadResult结果,Boolean&endConnection)在Microsoft.AspNetCore.Server.Kestrel.Core.Internal。 Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication 1 application) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequestsAsync[TContext](IHttpApplication1 application) dbug: Microsoft.AspNetCore.Server.Kestrel[10] 连接 id“0HM2QU64LCR9J”断开连接。dbug:Microsoft.AspNetCore.Server.Kestrel[2] 连接 ID“0HM2QU64LCR9J”已停止。dbug:Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets[6] 连接 ID“0HM2QU64LCR9J”收到 FIN。dbug:Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets[7] 连接 id“0HM2QU64LCR9J”发送 FIN,因为:“套接字传输的发送循环正常完成。”

按照建议在 Kestrel 中添加了 Http/2 支持,但仍然出现错误:

Grpc.Core.RpcException:'Status(StatusCode =“未知”,Detail =“收到状态为307的http2标头”,DebugException =“Grpc.Core.Internal.CoreErrorDetailException:{“创建”:“@1600342649.633000000”,“描述":"收到具有非 200 OK 状态的 http2 :status 标头","file":"T:\src\github\grpc\workspace_csharp_ext_windows_x86\src\core\ext\filters\http\client\http_client_filter.cc"," file_line":130,"grpc_message":"收到状态为 307 的 http2 标头","grpc_status":2,"value":"307"}")'