簡體   English   中英

從 .Net Framework 客戶端調用 gRPC 服務器時出現異常

[英]Exception when calling gRPC server from .Net Framework client

我有一個在 .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();
    }
}

以下 .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 解析失敗服務:https://localhost:5001", DebugException="Grpc.Core.Internal.CoreErrorDetailException: {"created":" @16003333768.055000000","description":"Resolver瞬態故障","file":"T:\\src\\github\\grpc\\workspace_csharp_ext_windows_x86\\src\\core\\ext\\filters\\client_channel\\resolving_lb_policy.cc","215_line" ,"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":[{"created":"@156003000000 "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":[{"created":"@1600333768.055000000","description":"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\\filters\\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.HttpParser 1.ParseRequestLine( TRequestHandler 處理程序,字節 * 數據,Int32 長度)在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpParser 1.ParseRequestLine(TRequestHandler handler, ReadOnlySequence 1& 緩沖區,SequencePosition& 消耗,SequencePosition& 審查)在 Microsoft.AspNetCore.Server。 Kestrel.Core.Internal.Http.HttpParser 1.Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.IHttpParser 1.Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.IHttpParser<TRequestHandler>.ParseRequestLine(TRequestHandler handler, ReadOnlySequence 1& 緩沖區,SequencePosition& 消耗,SequencePosition& 檢查)在 Microsoft.AspNetCore。 1& buffer, SequencePosition& consumed, SequencePosition& examined) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.ParseRequest(ReadOnlySequence 1& 緩沖區, SequencePosition& 消耗, SequencePosition& 檢查)在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.TryParseRequest(ReadResult result, 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](IHttpApplication 1 應用程序)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,因為:“Socket 傳輸的發送循環正常完成。”

按照建議在 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 ":"收到的 http2 :status 標頭具有非 200 OK 狀態","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"}")'

我不相信Channel的構造函數參數期望值是 URI 格式; 你最好的選擇可能是:

Channel channel = new Channel("localhost", 5001, ChannelCredentials.Insecure)

雖然它也可以作為工作:

Channel channel = new Channel("localhost:5001", ChannelCredentials.Insecure);

我按照這個答案讓它工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM