C# Inline Handler

Handle exceptions with a C# inline handler. Useful when your app exposes C# routes but you still want consistent JSON errors.

Full source

File: pwsh/tutorial/examples/18.3-ExceptionHandling-CSharp.ps1

#
# Sample: Exception Handling with C# Inline Code
# Demonstrates using Enable-KrExceptionHandling with C# code via Roslyn.
# FileName: 18.3-ExceptionHandling-CSharp.ps1
#
param(
    [int]$Port = 5000,
    [IPAddress]$IPAddress = [IPAddress]::Loopback
)

Initialize-KrRoot -Path $PSScriptRoot
New-KrLogger | Set-KrLoggerLevel -Level Debug |
    Add-KrSinkConsole | Register-KrLogger -Name 'console' -SetAsDefault

New-KrServer -Name 'Exception Handling - CSharp'
Add-KrEndpoint -Port $Port -IPAddress $IPAddress

$cs = @"
// The Context variable is available and typed to Kestrun.Models.KestrunContext
// You can also access HttpContext via Context.HttpContext
Context.Response.WriteJsonResponse(new {
    error = true,
    message = "Handled by C# exception handler",
    path = Context.Request.Path,
    method = Context.Request.Method
}, statusCode: 500);
"@

Enable-KrExceptionHandling -Code $cs -Language ([Kestrun.Scripting.ScriptLanguage]::CSharp)

Enable-KrConfiguration

# A normal route
Add-KrMapRoute -Verbs Get -Pattern '/hello' -ScriptBlock {
    Write-KrJsonResponse @{ msg = 'Hello from /hello' } -StatusCode 200
}

Add-KrMapRoute -Verbs Get -Pattern '/fail' -ScriptBlock {
    throw 'C# handler demo'
}

Write-Host "Server starting on http://$($IPAddress):$Port" -ForegroundColor Green
Write-Host 'Try these URLs:' -ForegroundColor Yellow
Write-Host "  http://$($IPAddress):$Port/hello   - Happy path" -ForegroundColor Cyan
Write-Host "  http://$($IPAddress):$Port/fail  - Triggers exception handled by C#" -ForegroundColor Cyan
Write-Host ''

Start-KrServer

Step-by-step

  1. Logger: Configure console logger.
  2. Server: Create and listen on 127.0.0.1:5000.
  3. Middleware: Enable exception handling with -Code in C# and -Language CSharp.
  4. Route: Map /fail that throws in C# to trigger the middleware.
  5. Response: The handler writes structured JSON with error, message, path, and method.
  6. Start: Apply configuration and start.

Try it

curl -i http://127.0.0.1:5000/fail

PowerShell:

Invoke-WebRequest -Uri http://127.0.0.1:5000/fail -UseBasicParsing -SkipHttpErrorCheck |
  Select-Object StatusCode,@{n='ContentType';e={$_.Headers['Content-Type']}},Content

Troubleshooting

  • Getting text/plain instead of application/json? Ensure your handler sets the content type.
  • Missing fields in JSON? Verify your C# code writes all keys (error, message, path, method).

References


Previous / Next

Previous: VB.Net Handler Next: ProblemDetails Fallback