Named Pipes (Windows)

Bind Kestrun to a Windows named pipe for local IPC instead of a TCP port.

Prerequisites: see Introduction.

Full source

File: pwsh/tutorial/examples/7.4-Named-Pipes.ps1

<#
        Sample: Kestrun Server over a Windows Named Pipe
        Demonstrates binding to a Windows named pipe for local IPC instead of a TCP port.
        File:   7.4-Named-Pipes.ps1
#>

param(
    [string]$NamedPipeName = 'kestrun.demo.pipe'
)

# 1. Configure logging (console sink so we can see events)
New-KrLogger |
    Add-KrSinkConsole |
    Register-KrLogger -Name 'console' -SetAsDefault | Out-Null

# 2. Create the server host
New-KrServer -Name 'Endpoints Pipes'

# 3. Add a named pipe listener. DO NOT prepend \\./pipe/. Provide only the short name.
Add-KrNamedPipeListener -NamedPipeName $NamedPipeName

# 5. Finalize configuration (after this, mappings are locked in)
Enable-KrConfiguration

# 6. Map a GET /pipe route that returns a simple text response
Add-KrMapRoute -Verbs Get -Pattern '/pipe' -ScriptBlock {
    Write-KrLog -Level Debug -Message 'Pipe route hit'
    Write-KrTextResponse -InputObject 'Hello over named pipe' -StatusCode 200
}

# 7. Informational log so the console shows readiness
Write-KrLog -Level Information -Message 'Named pipe listener active {Pipe}' -Values $NamedPipeName

# 8. Start the server and close all the loggers when the server stops
# This is equivalent to calling Close-KrLogger after Start-KrServer
Start-KrServer -CloseLogsOnExit

Step-by-step

  1. Guard: exit if not Windows.
  2. Create server.
  3. Add named pipe listener with Add-KrNamedPipeListener.
  4. Enable configuration.
  5. Map /pipe route.
  6. Start server.

Try it

In a second PowerShell session (after the server is running):

Invoke-KrWebRequest -NamedPipeName 'kestrun.demo.pipe' -Method GET -Path '/pipe'

Expected (truncated example):

StatusCode : 200
Content    : Hello over named pipe

If you get 404, confirm the route pattern is /pipe and the pipe name matches.

Why use pipes?

Benefit Detail
Local only Not exposed on network interfaces
Avoid ports No port conflicts / firewall tweaks
IPC perf Efficient for tool -> service scenarios

Troubleshooting

Symptom Cause Fix
Cannot connect Client not using pipe Ensure correct pipe name in client
Script exits immediately Not Windows Run only on Windows
404 Not Found Wrong path Use /pipe exactly
Access denied Pipe name already in use / permission Stop previous instance or choose a unique name

See also: Routes · Logging


References

Previous / Next