question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

troubles using grpc-web client from .net framework

See original GitHub issue

im running this on Win10 21H2 (Build 19044.1415) and trying to make a call from the .net framework to my .net 6 greeter service

Server

using web.Services;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddGrpc();

var app = builder.Build();
app.UseGrpcWeb();

// Configure the HTTP request pipeline.
app.MapGrpcService<GreeterService>().EnableGrpcWeb();
app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client.");

app.Run();

client project

<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<OutputType>Exe</OutputType>
		<TargetFrameworks>net48;net6.0</TargetFrameworks>
		<ImplicitUsings>enable</ImplicitUsings>
		<LangVersion>10.0</LangVersion>
	</PropertyGroup>

	<ItemGroup>
		<PackageReference Include="Google.Protobuf" Version="3.19.1" />
		<PackageReference Include="Grpc.Net.Client" Version="2.41.0" />
		<PackageReference Include="Grpc.Net.Client.Web" Version="2.41.0" />
		<PackageReference Include="Grpc.Tools" Version="2.42.0">
			<PrivateAssets>all</PrivateAssets>
			<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
		</PackageReference>
	</ItemGroup>

	<ItemGroup Condition="$(TargetFramework) == 'net48'" >
		<PackageReference Include="System.Net.Http" Version="4.3.4" />
	</ItemGroup>

	<ItemGroup>
		<Protobuf Include="..\Proto\greet.proto" GrpcServices="Client" Link="Proto\greet.proto" />
	</ItemGroup>

</Project>

client code

using Grpc.Net.Client;
using Greet;
using Grpc.Net.Client.Web;

var version =
#if NET48
    "net48";
#else
    "net6.0";
#endif

var handler = new GrpcWebHandler(new HttpClientHandler());
var options = new GrpcChannelOptions {HttpHandler = handler};

var channel = GrpcChannel.ForAddress("http://localhost:5001", options);
var client = new Greeter.GreeterClient(channel);

var result = await client.SayHelloAsync(new HelloRequest { Name = version });
Console.WriteLine(result.Message);

which works fine with net6

dotnet run --framework net6.0
Hello net6.0

but fails on .net framework 4.8

dotnet run --framework net48

Unhandled Exception: Grpc.Core.RpcException: Status(StatusCode="Internal", Detail="Error starting gRPC call. HttpRequestException: An error occurred while sending the request. WebException: The server committed a protocol violation. Section=ResponseStatusLine", DebugException="System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The server committed a protocol violation. Section=ResponseStatusLine
   at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
   at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
...

any hint would be highly appreciated

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
ThorstenReichertcommented, Jan 4, 2022

@JamesNK indeed, changing the appsettings json from

{ "Kestrel": { "EndpointDefaults": { "Protocols": "Http2" }}}

to

{ "Kestrel": { "EndpointDefaults": { "Protocols": "Http1AndHttp2" }}}

solved the issue for me, thanks a lot!

1reaction
JamesNKcommented, Jan 4, 2022

.NET Framework only supports HTTP/1.1 (unless WinHttpHandler is configured). That’s fine, because gRPC-Web works over HTTP/1.1. An ASP.NET Core gRPC project is configured by default to HTTP/2 only. This is most likely your problem

You’re both sending non-TLS requests (i.e. the address is http and not https). That means the .NET Framework client is sending HTTP/1.1 to a server that expects HTTP/2.

To fix this, you must either use TLS and allow HTTP/1.1 and 2 (TLS allows negotiation between the client and server of which to use) or have a separate HTTP/1.1 only http port.

Read more comments on GitHub >

github_iconTop Results From Across the Web

gRPC-Web for .NET now available
gRPC is a modern high-performance RPC (Remote Procedure Call) framework. gRPC is based on HTTP/2, Protocol Buffers and other modern standard- ...
Read more >
c# - Grpc client in .net standard fails when called from . ...
Grpc.Net.Client only works on .NET Framework (as opposed to .NET Core and .NET5+) with limitations. The reason is that .
Read more >
.NET & GRPC What they forgot to tell you | FAESEL.COM
NET Framework client app consume a .NET Core GRPC server? How to debug with tools, call an endpoint; Authentication and authorization ...
Read more >
gRPC-Web with .NET - YouTube
gRPC-Web allows browser based applications to call into gRPC services via a special proxy. . NET developers are now able to build and...
Read more >
Use gRPC in the Browser With gRPC-Web and .NET5
This will walk you through creating your first gRPC server and client using .NET 5). 1 — The Problem. In a previous article,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found