Xamarin.Forms gRPC启动gRPC调用时出错:Connection上的流意外结束

问题描述:

我为我的学习编写了应用程序.

i programming an Application for my study.

我尝试在Xamarin.Forms中使用gRPC.

I try to use gRPC in Xamarin.Forms.

gRPC位于单独的Libyry(.NET Standart 2.1)中. 如果我在WPF-Core Project中使用代码,那么一切都会很好.

The gRPC is in a seperate Libyry (.NET Standart 2.1). If i use the code in WPF-Core Project every thing works fine.

但是,如果我尝试在Xamarin.Forms-Project中使用相同的功能,则连接将无法正常工作.

But if I try to use the same in my Xamarin.Forms-Project the Connection don't work.

如果我使用connectionString" http://my.server.com:5050 "我得到这些异常

if I use the connectionString "http://my.server.com:5050" I get these Exception

Error starting gRPC call: unexpected end of stream on Connection{my.server.com:5050, proxy=DIRECT hostAddress=5.189.149.82 cipherSuite=none protocol=http/1.1} (recycle count=0)

如果我使用SSL版本" https://my.server.com:5050 "我得到这些异常

if I the SSL Version"https://my.server.com:5050" I get these Exception

Error starting gRPC call: Connection closed by peer

这是gRPC库的代码

...
        if (connectionString.Contains("http://"))
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

        channel = GrpcChannel.ForAddress(connectionString);
        client = new Haushaltsbuch.HaushaltsbuchClient(channel);

        SuccsessReply reply = new SuccsessReply { Result = false };

        try
        {
            reply = client.Login(new UserRequest
            {
                User = new GRPC_User
                {
                    Username = username,
                    PassHash = passHash
                }
            });
        }
        catch (RpcException e) when (e.Status.Detail.Contains("The SSL connection could not be established"))
        {
            client = null;
            throw new CommunicationException("Fehler mit SSL-Zertifikat des Servers", e);
        }
        catch (RpcException e)
        {
            client = null;
            throw new CommunicationException("Server nicht erreichbar", e);
        }
...

我只是一个学生,如果我用google搜索,则说明Xamarin Forms支持gRPC. 但是为什么它不起作用?

I am only a student and if I google, then it says that Xamarin Forms is supporting gRPC. But why is it not working?

.Android项目具有来自NuGet的GRPC.Core软件包.

the .Android Project has the GRPC.Core package from NuGet istalled.

通过替换解决了问题

channel = GrpcChannel.ForAddress(connectionString);

使用

        if (connectionString.Contains("http://"))
        {
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            string newConString = connectionString.Replace("http://", "");
            return new Channel(newConString, ChannelCredentials.Insecure);
        }
        else
        {
            string newConString = connectionString.Replace("https://", "");
            return new Channel(newConString, new SslCredentials());
        }

似乎GrpcChannel类在Andriod上不起作用.

It seems like the GrpcChannel Class isn't working on Andriod.