-
-
Save fzankl/08cfc8a59f32a40b62367048ffc037dd to your computer and use it in GitHub Desktop.
Sample how build a gRPC client using .NET and Unix Domain Sockets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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