为什么Xamarin Android无法发送GRPC/Http2请求? [英] Why does Xamarin Android fails to send GRPC/Http2 requests?

查看:205
本文介绍了为什么Xamarin Android无法发送GRPC/Http2请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Dot Net Conf 2019主题演讲.我已经托管了grpc服务,.net核心控制台应用程序可以从中获取数据而不会出现任何问题. 但是,在运行xamarin应用程序时,HTTP 1.1会发生ALPN协商(客户端问候),因此grpc调用会失败. 此处提供源代码

I'm trying to run the xamarin forms sample from the Dot net conf 2019 keynote. I've hosted the grpc service and a .net core console application can get data from it without any issues. But when running the xamarin app, the ALPN negotiation(Client hello) happens with http 1.1 and therefore the grpc call fails. Source code available here

错误消息:Grpc.Core.RpcException:Status(StatusCode = Internal,Detail ="Bad gRPC响应.响应协议降级为HTTP/1.1."

调试器显示xamarin下使用的SocketsHttpHandler没有支持Http 2的代码.

Debugger reveals that the SocketsHttpHandler used underneath on xamarin does not have the code that support Http 2.

问题:

  1. 显然Xamarin支持Grpc和Http.为什么失败了? 鉴于我的怀疑是正确的,所以出现问题是因为xamarin使用了不支持Http2的旧版本的System.Net.Http.dll.
  2. 如何确保Xamarin使用支持Http2的正确运行时程序集?
  3. 如果这对某人有用,可以请您共享系统配置吗?
  1. Apparently Xamarin supports Grpc and Http. Why does this fail? Given that my suspect is correct, so that the issue is because xamarin uses an old version of System.Net.Http.dll that doesn't support Http2.
  2. How to make sure Xamarin uses correct runtime assemblies that support Http2?
  3. If this works for someone, Can you please share the system configuration?

配置

使用共享的运行时:true
Mono共享的运行时版本(在android设备上可见):10.1.0-18.
Windows 10上的Visual Studio 2019:
Xamarin.Android SDK 10.1.4.0(d16-4/e44d1ae)
Xamarin.Android参考程序集和MSBuild支持.
单声道:fd9f379
Java.Interop:xamarin/java.interop/d16-4@c4e569f
Xamarin.Android工具:xamarin/xamarin-android-tools/d16-5 @ 9f4ed4b

Use shared runtime: true
Mono shared runtime version (as seen on android device): 10.1.0-18.
Visual studio 2019 on windows 10:
Xamarin.Android SDK 10.1.4.0 (d16-4/e44d1ae)
Xamarin.Android Reference Assemblies and MSBuild support.
Mono: fd9f379
Java.Interop: xamarin/java.interop/d16-4@c4e569f
Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-5@9f4ed4b

我尝试过的事情:

  1. 切换到托管的HttpClient实现/NativeMessageHandler 来自ModernHttpClient.仍然失败,并出现相同的错误. (因为ALPN 协商仍使用相同的代码吗?)
  2. 在新的HttpClient上发送带有版本2的HttpRequestMessage.仍然 基础处理程序是相同的.因此它无法协商Http2.
  3. 禁用共享运行时.没用.
  1. Switch to Managed HttpClient implementation /NativeMessageHandler from ModernHttpClient. Still fails with same error. (Because ALPN negotiation still uses same code?)
  2. Send a HttpRequestMessage with Version 2 on a new HttpClient. Still the underlying handler is the same. So it fails to negotiate Http2.
  3. Disable shared runtime. Didn't work.

在.net核心控制台应用程序上,SocketsHttpHandler使用的HttpConnectionSettings具有http2支持代码:

On .net core console app HttpConnectionSettings used by the SocketsHttpHandler has http2 supporting code:

但是在xamarin上却没有.选择了AndroidClientHandler,但将t委托给SocketsHttpHandler.它的设置不支持http2:

But on xamarin it doesn't. AndroidClientHandler is selected, but t delegates to the SocketsHttpHandler. It's settings doesn't have the http2 support:

推荐答案

我使它可以解决您提供的解决方案.抱歉,我没有介绍您的方式为何不起作用以及这一方法为何起作用的确切细节.我也无法测试iOs版本.

I make it work your provided solution. Sorry i didnt cover exact details why your way didnt worked and this one worked. Also i couldn't test iOs version.

服务器.我只更改了端口版本.我使用发行版.顺便说一句,我不确定您如何成功运行Http1AndHttp2以及相同的ip和port.我在个人项目上遇到了问题.

Server. I changed only port version. I used release version. Btw I am not sure how successfully you could run Http1AndHttp2 and same ip and port. I had issues on personal project.

移动 添加了 Grpc.Core

Mobile Added Grpc.Core and Grpc.Core.Api nugets to both projects.

我正在以不同的方式创建频道.您的解决方案版本

I am creating channel differently. Your solution version

var channel = GrpcChannel.ForAddress("123.123.123.123:123456");

我的版本更改为

var channel = new Channel("123.123.123.123:123456", ChannelCredentials.Insecure);

这不是一个非常安全的版本,但是可以使用一段时间,因为http2已经是二进制文件了.创建并使用安全的方式来签署证书并在服务器和客户端中使用它.很好的例子是

This is not very secure version but could work for some time since http2 is already binary. Create and use secure way you need to sign certificate and use it in server and client. Good example is here

我还建议重用渠道,因为创建新渠道的成本很高. Http2擅长将多个客户端保持在一个流上.您可以重新创建便宜的客户端.

Also i recommend to reuse channels since it is expensive to create new. Http2 is good at keeping multiple clients on one stream. You could recreate clients which is cheap operation.

MS的一些更新 https://docs.microsoft.com/zh-cn/aspnet/core/grpc/client?view = aspnetcore-3.1

当前未使用Grpc.Net.Client通过HTTP/2调用gRPC 在Xamarin上受支持.我们正在努力改善HTTP/2的支持, 未来的Xamarin版本. Grpc.Core和gRPC-Web是可行的替代方案 今天可以工作.

Calling gRPC over HTTP/2 with Grpc.Net.Client is currently not supported on Xamarin. We are working to improve HTTP/2 support in a future Xamarin release. Grpc.Core and gRPC-Web are viable alternatives that work today.

这篇关于为什么Xamarin Android无法发送GRPC/Http2请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆