Skip to content

Instantly share code, notes, and snippets.

@wi7a1ian
Created February 23, 2024 16:47
Show Gist options
  • Save wi7a1ian/5794f6e50f91c5f48a65f1275febf25c to your computer and use it in GitHub Desktop.
Save wi7a1ian/5794f6e50f91c5f48a65f1275febf25c to your computer and use it in GitHub Desktop.
Using Windows named pipes in #csharp
using System.IO.Pipes;
var httpHandler = new SocketsHttpHandler
{
ConnectCallback = async (ctx, ct) =>
{
var pipeClientStream = new NamedPipeClientStream(
serverName: ".",
pipeName: "my-test-pipe",
PipeDirection.InOut,
PipeOptions.Asynchronous);
await pipeClientStream.ConnectAsync(ct);
return pipeClientStream;
}
};
// Create an HttpClient using the named pipe handler
var httpClient = new HttpClient(httpHandler)
{
BaseAddress = new Uri("http://localhost");
};
var result = await httpClient.GetStringAsync("/test");
Console.WriteLine(result);
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(
opts => opts.ListenNamedPipe("my-test-pipe"));
builder.WebHost.UseNamedPipes(opts =>
{
// Bump the buffer sizes to 4MB (defaults to 1MB)
opts.MaxWriteBufferSize = 4 * 1024 * 1024;
opts.MaxReadBufferSize = 4 * 1024 * 1024;
});
var app = builder.Build();
app.MapGet("/test", () => "Hello world!");
app.Run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment