简体   繁体   中英

Xamarin.Forms gRPC Error starting gRPC call: unexpected end of stream on Connection

i programming an Application for my study.

I try to use gRPC in Xamarin.Forms.

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

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

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)

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

Error starting gRPC call: Connection closed by peer

Here is the Code of the gRPC-Libary

...
        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);
        }
...

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

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

Solved it by Replacing

channel = GrpcChannel.ForAddress(connectionString);

with

        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());
        }

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

Update: May, 2021

Xamarin does not fully support gRPC, so be aware of this when developing your software on Xamarin.Forms.

Starting w/ gRPC version 2.34.X, gRPC has started partial support for Xamarin.Forms w/ Android and iOS devices.

Please see this for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM