首页 > 解决方案 > gRPC 使用 C# 连接到 Concordium 区块链 API

问题描述

我意识到这是一个相当特定于服务的主题,但我认为我的问题的原因是一般性的,因为我是 gRPC 领域的新手。

我正在尝试从 .NET Core 3.1 应用程序调用Concordium 区块链 gRPC API ( https://github.com/Concordium/concordium-grpc-api ) 中的简单方法PeerVersion,但我得到的似乎是关于 SSL 的一般错误。

我可以补充一点,我已经用 BloomRPC ( https://github.com/uw-labs/bloomrpc ) 测试了这个调用和其他调用,它工作得很好。

如果不访问 Concordium 节点,您将无法成功调用 API,但我认为比我更了解 gRPC 的人可能会看到我做错了什么。

C# 代码

static async Task Main(string[] args)
{
    using var channel = GrpcChannel.ForAddress("https://192.168.1.18:10001"); // my local node running on the Concordium testnet

    var client = new Concordium.P2P.P2PClient(channel);

    var metadata = new Grpc.Core.Metadata() // required token for the API
    {
        { "authentication", "rpcadmin" }
    };

    var request = new Concordium.Empty();

    var reply = client.PeerVersion(request, metadata);

    Console.WriteLine("peer version: " + reply.Value);

    Console.WriteLine("press any key to exit...");

    Console.ReadKey();
}

当我运行它时,我得到以下异常。(丹麦文本为“现有连接被外部主机强行断开”或类似内容)。

-       $exception  {"Status(StatusCode=\"Unavailable\", Detail=\"Error starting gRPC call. HttpRequestException: The SSL connection could not be established, see inner exception. IOException: Unable to read data from the transport connection: En eksisterende forbindelse blev tvangsafbrudt af en ekstern vært.. SocketException: En eksisterende forbindelse blev tvangsafbrudt af en ekstern vært.\", DebugException=\"System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
 ---> System.IO.IOException: Unable to read data from the transport connection: En eksisterende forbindelse blev tvangsafbrudt af en ekstern vært..
 ---> System.Net.Sockets.SocketException (10054): En eksisterende forbindelse blev tvangsafbrudt af en ekstern vært.
   --- End of inner exception stack trace ---
   at System.Net.FixedSizeReader.ReadPacketAsync(Stream transport, AsyncProtocolRequest request)
   at System.Net.Security.SslStream.ThrowIfExceptional()
   at System.Net.Security.SslStream.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Net.Security.SslStream.EndProcessAuthentication(IAsyncResult result)
   at System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult asyncResult)
   at System.Net.Security.SslStream.<>c.<AuthenticateAsClientAsync>b__65_1(IAsyncResult iar)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.GetHttp2ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at Grpc.Net.Client.Internal.GrpcCall`2.RunCall(HttpRequestMessage request, Nullable`1 timeout)\")"}  Grpc.Core.RpcException

有没有人能给我一些关于这里可能是什么问题的指示?

对于任何了解 BloobRPC 的人,这里是请求和响应的屏幕截图。 在此处输入图像描述

标签: c#.net.net-coreblockchaingrpc

解决方案


啊,我在别处得到了我的问题的答案。事实证明,我认为 Concordium 节点需要安全连接的假设是错误的,实际上它不支持安全连接,因此 URL 是错误的部分。

所以要解决这个问题,我需要像这样创建客户端:

// to allow non secure connections
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

using var channel = GrpcChannel.ForAddress("http://192.168.1.18:10001");

推荐阅读