Skip to content

Instantly share code, notes, and snippets.

@fzankl
Created February 2, 2022 17:09
Show Gist options
  • Save fzankl/08cfc8a59f32a40b62367048ffc037dd to your computer and use it in GitHub Desktop.
Save fzankl/08cfc8a59f32a40b62367048ffc037dd to your computer and use it in GitHub Desktop.
Sample how build a gRPC client using .NET and Unix Domain Sockets
using System.Net.Sockets;
using Grpc.Net.Client;
var app = WebApplication.CreateBuilder(args).Build();
app.MapGet("/", async () =>
{
var socketsHttpHandler = new SocketsHttpHandler
{
ConnectCallback = async (_, cancellationToken) =>
{
var endpoint = new UnixDomainSocketEndPoint("/tmp/foo.sock");
var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
try
{
await socket.ConnectAsync(endpoint, cancellationToken).ConfigureAwait(false);
return new NetworkStream(socket, false);
}
catch
{
socket.Dispose();
throw;
}
}
};
var channel = GrpcChannel.ForAddress("http://localhost", new GrpcChannelOptions
{
HttpHandler = socketsHttpHandler
});
var foo = new UnixSocketDemo.Foo.FooClient(channel);
var response = await foo.GetFooAsync(new UnixSocketDemo.FooRequest
{
Message = "Hello from .NET"
});
return response.Message;
});
app.Run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment