Created
February 23, 2024 16:47
-
-
Save wi7a1ian/5794f6e50f91c5f48a65f1275febf25c to your computer and use it in GitHub Desktop.
Using Windows named pipes in #csharp
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.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); |
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
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