![](/img/trans.png)
[英]How to call self-signed gRPC service from Xamarin.Forms (iOS and Android)
[英]Xamarin.Forms gRPC Error starting gRPC call: unexpected end of stream on Connection
我為我的學習編寫了一個應用程序。
我嘗試在 Xamarin.Forms 中使用 gRPC。
gRPC 位於單獨的 Libyry (.NET Standard 2.1) 中。 如果我使用 WPF-Core 項目中的代碼,一切正常。
但是,如果我嘗試在我的 Xamarin.Forms-Project 中使用相同的連接,則連接不起作用。
如果我使用 connectionString " http://my.server.com:5050 " 我得到這些異常
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 ”,我會收到這些異常
Error starting gRPC call: Connection closed by peer
這是 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);
}
...
我只是一個學生,如果我用谷歌搜索,它會說 Xamarin Forms 支持 gRPC。 但為什么它不起作用?
.Android 項目安裝了來自 NuGet 的 GRPC.Core 包。
通過替換解決它
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。
更新:2021 年 5 月
Xamarin 不完全支持 gRPC,因此在 Xamarin.Forms 上開發軟件時請注意這一點。
從 gRPC 版本 2.34.X 開始,gRPC 已開始部分支持帶有 Android 和 iOS 設備的 Xamarin.Forms。
請參閱此了解更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.