grpc / grpc/grpc-dotnet

Connecting to gRPC Server over Unix Domain Socket

Open
#2,428 1 comment 0 reactions 0 assignees View on GitHub
question
Dominant language
C#
Stars
4.5k
Forks
836
Avg merge
6d 3h
Merged PRs (30d)
7

Description

Following the [example](https://learn.microsoft.com/en-us/aspnet/core/grpc/interprocess-uds?view=aspnetcore-8.0) in the docs, I tried to connect to a gRPC server listening on a Unix Domain Socket. I created a small program to test it
```
using System.Net;
using System.Net.Sockets;

using Grpc.Net.Client;

var socketPath = "/tmp/foo.sock";
var udsEndPoint = new UnixDomainSocketEndPoint(socketPath);
var connectionFactory = new UnixDomainSocketsConnectionFactory(udsEndPoint);
var socketsHttpHandler = new SocketsHttpHandler
{
ConnectCallback = connectionFactory.ConnectAsync
};

var grpc = GrpcChannel.ForAddress("http://localhost", new GrpcChannelOptions
{
HttpHandler = socketsHttpHandler
});

await grpc.ConnectAsync();

public class UnixDomainSocketsConnectionFactory
{
private readonly EndPoint endPoint;

public UnixDomainSocketsConnectionFactory(EndPoint endPoint)
{
this.endPoint = endPoint;
}

public async ValueTask ConnectAsync(SocketsHttpConnectionContext _,
CancellationToken cancellationToken = default)
{
var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);

try
{
await socket.ConnectAsync(this.endPoint, cancellationToken).ConfigureAwait(false);
return new NetworkStream(socket, true);
}
catch
{
socket.Dispose();
throw;
}
}
}
```

However, I am getting the following error:
```
Unhandled exception. System.InvalidOperationException: Channel is configured with an HTTP transport doesn't support client-side load balancing or connectivity state tracking. The underlying HTTP transport must be a SocketsHttpHandler with no SocketsHttpHandler.ConnectCallback configured. The HTTP transport must be configured on the channel using GrpcChannelOptions.HttpHandler.
at Grpc.Net.Client.GrpcChannel.ValidateHttpHandlerSupportsConnectivity()
at Grpc.Net.Client.GrpcChannel.ConnectAsync(CancellationToken cancellationToken)
at Program.$(String[] args) in /mnt/d/Dropbox/dev/GitHub/viam-dotnet-sdk/examples/ConsoleApp1/Program.cs:line 20
at Program.(String[] args)
Aborted
```

I'm guessing this behavior changed since 1-Jan-2024, but not sure what the solution is here.

Contributor guide

Open the contributing guide

Research direction

Start with the linked Unix Domain Socket documentation example and the GrpcChannel.ConnectAsync call at Program.cs line 20. Trace GrpcChannel.ValidateHttpHandlerSupportsConnectivity and compare its requirements with the configured SocketsHttpHandler. Done means the supported configuration or required documentation change is clearly established and verified against the reported exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, grpc
Domain
backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.