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

标签 c# .net protocol-buffers grpc

我有一个在 .Net Core 中实现的 gRPC 服务器,我想与在 .Net Framework 中运行的控制台应用程序联系。
这是该问题的工作 repo :
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();
    }
}
以下 .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();
    }
}
发送请求时出现以下异常:

Grpc.Core.RpcException: 'Status(StatusCode="Unavailable", Detail="DNS resolution failed for service: https://localhost:5001", DebugException="Grpc.Core.Internal.CoreErrorDetailException: {"created":"@1600333768.055000000","description":"Resolver transient failure","file":"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 resolution failed for service: 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":[{"created":"@1600333768.055000000","description":"C-ares status is not ARES_SUCCESS qtype=A name=https://localhost:5001 is_balancer=0: Could not contact DNS servers","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":[{"created":"@1600333768.055000000","description":"C-ares status is not ARES_SUCCESS qtype=AAAA name=https://localhost:5001 is_balancer=0: Could not contact DNS servers","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}]}]}]}")'


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

dbug: Microsoft.AspNetCore.Server.Kestrel[17] Connection id "0HM2QU64LCR9J" bad request data: "Unrecognized HTTP version: 'HTTP/2.0'" Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Unrecognized HTTP version: 'HTTP/2.0' at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser1.RejectUnknownVersion(Byte* version, Int32 length) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser1.ParseRequestLine(TRequestHandler handler, Byte* data, Int32 length) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser1.ParseRequestLine(TRequestHandler handler, ReadOnlySequence1& buffer, SequencePosition& consumed, SequencePosition& examined) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser1.Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.IHttpParser<TRequestHandler>.ParseRequestLine(TRequestHandler handler, ReadOnlySequence1& buffer, SequencePosition& consumed, SequencePosition& examined) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.TakeStartLine(ReadOnlySequence1& buffer, SequencePosition& consumed, SequencePosition& examined) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.ParseRequest(ReadOnlySequence1& buffer, SequencePosition& consumed, SequencePosition& examined) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.TryParseRequest(ReadResult result, Boolean& endConnection) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication1 application) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequestsAsync[TContext](IHttpApplication1 application) dbug: Microsoft.AspNetCore.Server.Kestrel[10] Connection id "0HM2QU64LCR9J" disconnecting. dbug: Microsoft.AspNetCore.Server.Kestrel[2] Connection id "0HM2QU64LCR9J" stopped. dbug: Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets[6] Connection id "0HM2QU64LCR9J" received FIN. dbug: Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets[7] Connection id "0HM2QU64LCR9J" sending FIN because: "The Socket transport's send loop completed gracefully."


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

Grpc.Core.RpcException: 'Status(StatusCode="Unknown", Detail="Received http2 header with status: 307", DebugException="Grpc.Core.Internal.CoreErrorDetailException: {"created":"@1600342649.633000000","description":"Received http2 :status header with non-200 OK 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":"Received http2 header with status: 307","grpc_status":2,"value":"307"}")'

最佳答案

我不相信 Channel 的构造函数参数期望值采用 URI 格式;你最好的选择可能是:

Channel channel = new Channel("localhost", 5001, ChannelCredentials.Insecure)
虽然它也可以作为:
Channel channel = new Channel("localhost:5001", ChannelCredentials.Insecure);

关于c# - 从 .Net Framework 客户端调用 gRPC 服务器时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63934850/

相关文章:

c# - 有没有办法使用 XCode 创建 ASP .NET 文件?

c# - 基于 XSD 创建表单生成 XML 的智能方式

c# - 如果打开它们的应用程序崩溃,打开的 DDE channel 会发生什么情况?

java - 将短整数附加到 CodedOutputStream 的干净方法

sockets - 从套接字读取消息时如何获取字节顺序?

c# - Windows 服务 : using of BitmapEncoder or BitmapDecoder ends with «The operation completed successfully»

c# - 如何从WebBrowser控件中提取XML?

c# - 从 32 位应用程序使用 64 位库

.net - 事件无法设置在其范围内定义的变量?

c++ - 如何在 C++ 中将 double 转换为 C# 小数?