简体   繁体   English

是否可以在 .NET Core 中使用带有 HTTP/1.1 的 gRPC?

[英]Is it possible to use gRPC with HTTP/1.1 in .NET Core?

I have two network services - a gRPC client and gRPC server.我有两个网络服务——一个 gRPC 客户端和一个 gRPC 服务器。 Server is written in .NET Core, hence HTTP/2 for gRPC is enforced .服务器是用 .NET Core 编写的, 因此 gRPC 的 HTTP/2 被强制执行 Client however is a .NET Framework 4.7.2 web app hosted on IIS 8.5, so it only supports HTTP/1.1 .然而,客户端是托管在 IIS 8.5 上的 .NET Framework 4.7.2 Web 应用程序, 因此它仅支持 HTTP/1.1

Since it will take some time to upgrade the client I was thinking if it is possible to use HTTP/1.1 instead of HTTP/2 on the server side, but I cannot find any information how to achieve that.由于升级客户端需要一些时间,我在想是否可以在服务器端使用 HTTP/1.1 而不是 HTTP/2,但我找不到任何信息来实现这一点。

Is it possible to use HTTP/1.1 for gRPC server written in .NET Core?是否可以将 HTTP/1.1 用于用 .NET Core 编写的 gRPC 服务器? And if so - how?如果是这样 - 如何?

No, you cannot use gRPC on HTTP 1.1;不,您不能在 HTTP 1.1 上使用 gRPC; you may be able to use the Grpc.Core Google transport implementation, however, instead of the managed Microsoft bits;但是,您可以使用 Grpc.Core Google 传输实现,而不是托管的 Microsoft 位; this targets .NET Standard 1.5 and .NET Standard 2.0, so should work on .NET Core, and uses an OS-specific unmanaged binary (chttp2) for the transport.这针对 .NET Standard 1.5 和 .NET Standard 2.0,因此应该适用于 .NET Core,并使用特定于操作系统的非托管二进制文件 (chttp2) 进行传输。

For client-side, there is virtually no difference between the two;对于客户端,两者几乎没有区别; only the actual channel creation changes, between:只有实际的频道创建变化,在:

GrpcChannel.ForAddress(...)

with the Microsoft transport, and使用 Microsoft 传输,以及

new Channel(...)

with the Google transport.与谷歌传输。 All of the rest of the APIs are shared (in Grpc.Core.Api)所有其余的 API 都是共享的(在 Grpc.Core.Api 中)

No. The RPC call is done only over HTTP/2.不可以。RPC 调用仅通过 HTTP/2 完成。 This allows gRPC users to automatically leverage all the features of the protocol.这允许 gRPC 用户自动利用协议的所有功能。

If you don't need client streaming, you can Use the gRPC-Web protocol.如果您不需要客户端流式传输,则可以使用 gRPC-Web 协议。 Here's how you would start a client and server for such a service.以下是为此类服务启动客户端和服务器的方法。

https://docs.microsoft.com/en-us/aspnet/core/grpc/browser?view=aspnetcore-6.0 https://docs.microsoft.com/en-us/aspnet/core/grpc/browser?view=aspnetcore-6.0

Keep in mind that while it mentions blazor, this approach can be used in non-webassembly clients.请记住,虽然它提到了 blazor,但这种方法可用于非 webassembly 客户端。

I try to use Grpc in UWP aplication without success,i get allways the same error Status(StatusCode="Cancelled", Detail="No grpc-status found on response." this is my code我尝试在 UWP 应用程序中使用 Grpc 但没有成功,我总是得到相同的错误 Status(StatusCode="Cancelled", Detail="No grpc-status found on response." 这是我的代码

AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
var handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;

var channel = GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions{
                        HttpHandler = handler,
                        Credentials = Grpc.Core.ChannelCredentials.SecureSsl
                    });

var client = new Greeter.GreeterClient(channel);

var replay = await client.SayHelloAsync(
                 new HelloRequest
                  {
                      Name = "Jose Antonio"
                  };

How can i solve this problem我怎么解决这个问题

Have you tried using grpc-web?您是否尝试过使用grpc-web? I believe it has a workaround to use http1.1 instead of 2.2.我相信它有一个使用 http1.1 而不是 2.2 的解决方法。

there are some things to be aware of, like trailers and streaming which work a bit different because of http1.1 support for those features.有些事情需要注意,例如预告片和流媒体,它们的工作方式略有不同,因为 http1.1 支持这些功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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