简体   繁体   English

.net 不支持 Grpc 请求协议 'HTTP/1.1'

[英].net Grpc Request protocol 'HTTP/1.1' is not supported

I've implemented a simple gRPC service in.net5 similar to the GreeterService in the project template.我在 .net5 中实现了一个简单的 gRPC 服务,类似于项目模板中的 GreeterService。

The service works fine when using IIS LocalHost but the client throws this exception when calling the remote server:使用 IIS LocalHost 时服务工作正常,但客户端在调用远程服务器时抛出此异常:

Grpc.Core.RpcException HResult=0x80131500 Message=Status(StatusCode="Internal", Detail="Request protocol 'HTTP/1.1' is not supported.")

I thought Grpc used HTTP/2 by default.我以为 Grpc 默认使用 HTTP/2。 What am I doing wrong?我究竟做错了什么?

I had the same problem and I have sold it with grpc web as a reverse proxy.我有同样的问题,我已经用 grpc web 作为反向代理出售了它。 first of all you should install Grpc.Net.Client.Web on your grpc service project.首先你应该在你的 grpc 服务项目上安装 Grpc.Net.Client.Web。 after that, use middleware.之后,使用中间件。

app.UseGrpcWeb();

then replace your MapGrpcService with this command.然后用这个命令替换你的 MapGrpcService 。

endpoints.MapGrpcService<yourgRPCService>().EnableGrpcWeb();

in ConfigureService method, add bellow commands.在 ConfigureService 方法中,添加以下命令。

var handler = new GrpcWebHandler(GrpcWebMode.GrpcWebText, new 
                  HttpClientHandler());
services.AddGrpcClient<YourService.YourServiceClient>(options =>
{
    options.Address = new Uri("http://YourServerAddress");
}).ConfigureChannel(o=> {
    o.HttpClient = new HttpClient(handler);
});            

now you should inject your grpc service client and call your method.现在你应该注入你的 grpc 服务客户端并调用你的方法。

Thanks for the responses.感谢您的回复。 After reading this article I realized I needed to add the Grpc-Web proxy to my app, as this translates an HTTP/1.1 client message to HTTP/2.阅读本文后,我意识到我需要将 Grpc-Web 代理添加到我的应用程序中,因为这会将 HTTP/1.1 客户端消息转换为 HTTP/2。

The code additions to client and server are explained in this article . 本文解释了客户端和服务器的代码添加。

After making these changes/additions my gRPC messaging service is working fine.进行这些更改/添加后,我的 gRPC 消息传递服务工作正常。 Importantly - I spent a lot of time trying to figure out how to reference certificates in my call options - but the messaging works fine without a certificate.重要的是——我花了很多时间试图弄清楚如何在我的看涨期权中引用证书——但是没有证书,消息传递也能正常工作。

@poury saved my life. @poury 救了我的命。 This worked for me.这对我有用。 I have to update my client's code in ConfigureService method and add code below.我必须在 ConfigureService 方法中更新我的客户端代码并在下面添加代码。 (Required Grpc.Net.Client.Web package) (需要Grpc.Net.Client.Web包)

services.AddGrpcClient<GreeterService.GreeterServiceClient>(o => { o.Address = new Uri("https://localhost:5001");})
.ConfigureChannel( o => 
{
     o.HttpHandler = new GrpcWebHandler(new HttpClientHandler());
});

Also enabled app.UseGrpcWeb() & endpoints.MapGrpcService<GreeterService>().EnableGrpcWeb();还启用app.UseGrpcWeb() & endpoints.MapGrpcService<GreeterService>().EnableGrpcWeb(); on your Server's code.在您的服务器代码上。

PS: I don't know why, because my client's side is C# also, not a browser? PS:不知道为什么,因为我客户端也是C#,不是浏览器?

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

相关问题 是否可以在 .NET Core 中使用带有 HTTP/1.1 的 gRPC? - Is it possible to use gRPC with HTTP/1.1 in .NET Core? 不支持协议“net.tcp” - Protocol 'net.tcp' is not supported 不支持协议&#39;net.tcp&#39; - The protocol 'net.tcp' is not supported HTTP mono下的WebRequest不支持协议 - HTTP protocol not supported in WebRequest under mono Blazor 错误 400,ReasonPhrase 错误请求版本:1.1 System.Net.Http.BrowserHttpHandler - Blazor Error 400,ReasonPhrase Bad Request Version: 1.1 System.Net.Http.BrowserHttpHandler 如何使用 WireMock.NET 存根 gRPC 或 HTTP/2 请求? - How can I stub a gRPC or HTTP/2 request using WireMock.NET? GRPC - HTTP/2 服务器在连接上发送了无效数据。 从 .netcore 3.1 升级到 net5 或 net6 时的 HTTP/2 错误代码“PROTOCOL_ERROR” - GRPC - The HTTP/2 server sent invalid data on the connection. HTTP/2 error code 'PROTOCOL_ERROR when upgrading from .netcore 3.1 to net5 or net6 .NET 是否支持 SOAP 消息传递协议 RPC/文字? - Is the SOAP messaging protocol RPC/literal supported in .NET? 在 .net core 中通过 http 发出 Grpc 请求时会产生以下异常。 我正在使用 .net 核心 3.1 - When making Grpc request over http in .net core creates following exception. I am using .net core 3.1 WCF服务失败-HTTP / 1.1 400错误的请求 - WCF Service failing - HTTP/1.1 400 Bad Request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM