Using Postman with .net gRPC endpoints

Hey everyone,

Just a quick post on how to use postman with a gRPC endpoint using .net core.

Add the grpc reflection package to your project:

dotnet add package Grpc.AspNetCore.Server.Reflection

Add to the container and include the middleware in your program.cs:

builder.Services.AddGrpc();

# Add this line.
builder.Services.AddGrpcReflection();

var app = builder.Build();

app.MapGrpcService<GreeterService>();

# Add this line.
app.MapGrpcReflectionService();

Startup your project and then open postman. Create a new gRPC request by:

  • Clicking file > new
  • Select gRPC Request (currently has a beta tag)
  • Enter your url e.g. localhost:20257
  • Click Using server reflection Refresh

You should now be able to see your gRPC service listed to the right. Click the Invoke button.

Thanks to the following links for the info:

System.IO.IOException with a .NET gRPC Project on Mac

Hey everyone,


I created a new gRPC project with Visual Studio on a Mac and ran into the following error when trying to run it:

System.IO.IOException
...
http/2 over tls is not supported on macos due to missing alpn support

It turns out that Kestrel does not support HTTP/2 with TLS on MacOS. In order to get around it we end up having to configure it to not use TLS.

In program.cs, add the lines below with your HTTP port (e.g. 20257):

var builder = WebApplication.CreateBuilder(args);

builder.WebHost.ConfigureKestrel(options =>
{
    // Setup a HTTP/2 endpoint without TLS.
    options.ListenLocalhost(20257, o => o.Protocols =
        HttpProtocols.Http2);
});

Thanks to the following links for the info:

“‘protoc-gen-go’ is not recognized as an internal or external command” – Go

Hi everyone,

I’m currently looking into Go and have hit the following error while trying to run proto:

`protoc-gen-go` is not recognized as an internal or external command

I am using Windows and have installed the required libraries:

go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go

In my case the issue turned out to be that proto-gen hadn’t been added to my path: E:\repos\gocode\bin

Note that the new path won’t be available until you restart your terminal.

A subsequent error I encountered was the following:

'protoc-gen-go-grpc' is not recognized as an internal or external command,
operable program or batch file.

I just had to run the following to resolve it:

go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

.Net Core Web Api Returning Binary File with No Headers Instead of Expected Response

Hey everyone,

A small issue I’ve run into while setting up a .net core web api microservice that also utilises a number of gRPC services. Symptoms were as follows:

Response Read via Fiddler
– HTTP/1.0 200 This buggy server did not return headers
– Value was binary and unable to be read

Server logs

03:59:23 DBG] Connection id “0HM01U963EUAR” accepted.
[03:59:23 DBG] Connection id “0HM01U963EUAR” started.
[03:59:23 DBG] Connection id “0HM01U963EUAR”: HTTP/2 connection error.
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.Http2ConnectionErrorException: HTTP/2 connection error (PROTOCOL_ERROR): Invalid HTTP/2 connection preface.
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.Http2Connection.ParsePreface(ReadOnlySequence`1& buffer, SequencePosition& consumed, SequencePosition& examined)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.Http2Connection.TryReadPrefaceAsync()
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.Http2Connection.ProcessRequestsAsync[TContext](IHttpApplication`1 application)
[03:59:23 DBG] Connection id “0HM01U963EUAR” is closed. The last processed stream ID was 0.
[03:59:23 VRB] Connection id “0HM01U963EUAR” sending GOAWAY frame for stream ID 0 with length 8 and flags 0x0
[03:59:23 DBG] Connection id “0HM01U963EUAR” stopped.
[03:59:23 DBG] Connection id “0HM01U963EUAR” sending FIN because: “The Socket transport’s send loop completed gracefully.”

The issue turned out to be that in appSettings.json the default kestrel endpoint had been set to utilise http2. Removing this immediately resolved the issue. It may also be worth double checking your launch settings, startup.cs and program.cs to ensure that nothing’s been overridden.

I expect that any similar routing issues would result in similar symptoms so hopefully this post will be able to save a few people a bit time!

Cheers,
Chris

Http gRPC with .NET Core and Docker – Error starting gRPC call. System.Net.Http.HttpRequestException: An error occurred while sending the request. —> System.IO.IOException: The response ended prematurely.

Hi everyone,

I’ve been mucking around with gRPC today while using .NET Core and Docker. I have two Microservices in the setup, one server and one client. Because these services will only be communicating internally I intended to use HTTP instead of HTTPS. Unfortunately, I hit the following error while attempting to set this up:

Error starting gRPC call. System.Net.Http.HttpRequestException: An error occurred while sending the request. —> System.IO.IOException: The response ended prematurely.

According Microsoft’s eshoponcontainers documentation there are a few extra steps for getting this to work:

Using gRPC without TLS
gRPC works with HTTP/2 only. Usually when a client connects to a server, the connection is done using HTTP1.1 and promoted to HTTP/2 only if both, server and client, support HTTP/2. This promotion is performed using a protocol negotiation, usually implemented using ALPN protocol which requires TLS.

In order to get it to work you need to add the following to your server :

.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup()

.ConfigureKestrel(options =>
{
options.Listen(IPAddress.Any, 5001, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http2;
});
});
});

You then need to explicitly allow HTTP/2 without TLS when creating the client:

AppContext.SetSwitch(“System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport”, true);
AppContext.SetSwitch(“System.Net.Http.SocketsHttpHandler.Http2Support”, true);

// The port number(5001) must match the port of the gRPC server.
var channel = GrpcChannel.ForAddress(endpoint, new GrpcChannelOptions { LoggerFactory = loggerFactory });
var client = new CatalogService.Catalog.CatalogClient(channel);

Note that the AppContext.SetSwitch statements need to appear before the client is created to work. There’s also a bit of an overview on the following page: https://github.com/dotnet-architecture/eShopOnContainers/wiki/gRPC

The following stackoverflow post also helped with the ordering issue: https://stackoverflow.com/a/58053460/522859

error : “Timestamp” is not defined. – .net core and gRPC

Hi everyone,

I’m currently testing out gRPC with .NET Core and hit the following error when attempting to add a timestamp field to my proto file:

error : “Timestamp” is not defined.

My proto file was as follows:

syntax = “proto3”;
import “google/protobuf/timestamp.proto”;

option csharp_namespace = “publisher_api”;

package Weather;

// The weather forecast service definition
service Weather {

// Gets weather forecast
rpc Forecast(WeatherForecastRequest) returns (WeatherForecastResponse);
}

// The request message
message WeatherForecastRequest {

}

// The response message
message WeatherForecastResponse {
timestamp date = 1;
int32 temperatureC = 2;
int32 temperatureF = 3;
string summary = 4;
}

The solution was to fully qualify the timestamp namespace:


message WeatherForecastResponse {
google.protobuf.Timestamp date = 1;
int32 temperatureC = 2;
int32 temperatureF = 3;
string summary = 4;
}

Also ensure that you’ve added the protobuff package:

dotnet add package Google.Protobuf

See the following repo for a full example: https://github.com/Buzzology/microservicesDockerRabbitMQ