How to Configure Windows Sandbox Local Ocelot API Gateway via XML in Windows 11

In enterprise .NET architectures, Ocelot is one of the most widely adopted open-source API gateways. Built on top of ASP.NET Core, Ocelot provides routing, request aggregation, authentication integration, rate limiting, and caching tailored specifically for C# and .NET microservices. When testing declarative route definitions (ocelot.json) or evaluating upstream load-balancing rules, running gateway services directly on a development machine can introduce conflicting port bindings and background .NET processes. Windows Sandbox in Windows 11 Pro and Enterprise provides an ephemeral, hardware-isolated guest instance ideal for disposable API gateway testing.

By pairing automated Windows Sandbox configuration files (.wsb) with PowerShell setup scripts, developers can spin up an ephemeral Ocelot testing sandbox with a single double-click, validate routing rules thoroughly, and discard the environment cleanly upon exit.

Windows Sandbox Architecture and Setup Primitives

Windows Sandbox provides a pristine, virtualized Windows 11 environment powered by Microsoft’s Hyper-V hypervisor. A .wsb XML file configures container parameters:

  • <Networking>: Must be set to Enable to download the .NET SDK and Ocelot NuGet packages.
  • <MappedFolders>: Maps a host directory into the container to supply bootstrap scripts, ocelot.json files, and test payloads.
  • <LogonCommand>: Automatically executes PowerShell setup scripts when the sandbox desktop loads.

Creating the Ocelot Automated Setup Script

Create a directory on your host machine (e.g., C:\Sandbox\Ocelot) and save the following script as bootstrap-ocelot.ps1:

# bootstrap-ocelot.ps1 - Automated Ocelot Gateway Setup for Windows Sandbox
$ErrorActionPreference = "Stop"

Write-Host "Configuring Ocelot Gateway Sandbox Environment..." -ForegroundColor Cyan

# Install .NET SDK via winget
Write-Host "Installing .NET SDK..." -ForegroundColor Yellow
winget install Microsoft.DotNet.SDK.8 --silent --accept-package-agreements --accept-source-agreements

# Refresh environment variables
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")

# Define Ocelot project workspace
$projDir = "C:\OcelotApp"
New-Item -ItemType Directory -Path $projDir -Force
Set-Location $projDir

# Create ASP.NET Core Web project and add Ocelot package
Write-Host "Scaffolding ASP.NET Core Web project..." -ForegroundColor Yellow
dotnet new web -n OcelotGateway --no-https
Set-Location "$projDir\OcelotGateway"
dotnet add package Ocelot

# Author ocelot.json configuration
$ocelotJson = @"
{
  "Routes": [
    {
      "DownstreamPathTemplate": "/get",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "httpbin.org",
          "Port": 443
        }
      ],
      "UpstreamPathTemplate": "/gateway/test",
      "UpstreamHttpMethod": [ "Get" ]
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:5000"
  }
}
"@
$ocelotJson | Out-File -FilePath "$projDir\OcelotGateway\ocelot.json" -Encoding utf8

# Author Program.cs to configure Ocelot middleware
$programCs = @"
using Ocelot.DependencyInjection;
using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);
builder.Services.AddOcelot(builder.Configuration);

var app = builder.Build();
await app.UseOcelot();
app.Run("http://localhost:5000");
"@
$programCs | Out-File -FilePath "$projDir\OcelotGateway\Program.cs" -Encoding utf8

# Build and run Ocelot Gateway in background
Write-Host "Launching Ocelot Gateway on port 5000..." -ForegroundColor Green
Start-Process "dotnet" -ArgumentList "run" -WorkingDirectory "$projDir\OcelotGateway"

# Open Edge to test proxied endpoint
Start-Sleep -Seconds 8
Start-Process "msedge.exe" -ArgumentList "http://localhost:5000/gateway/test"

Authoring the Windows Sandbox WSB File

In the same folder, author the configuration file named OcelotSandbox.wsb:

<Configuration>
  <VGpu>Enable</VGpu>
  <Networking>Enable</Networking>
  <MappedFolders>
    <MappedFolder>
      <HostFolder>C:\Sandbox\Ocelot</HostFolder>
      <SandboxFolder>C:\OcelotSetup</SandboxFolder>
      <ReadOnly>true</ReadOnly>
    </MappedFolder>
  </MappedFolders>
  <LogonCommand>
    <Command>powershell.exe -ExecutionPolicy Bypass -NoExit -File C:\OcelotSetup\bootstrap-ocelot.ps1</Command>
  </LogonCommand>
</Configuration>

Running and Testing Ocelot Inside the Sandbox

  1. Double-click OcelotSandbox.wsb on your Windows 11 host.
  2. Windows Sandbox initializes, maps the configuration folder, and executes the PowerShell setup script.
  3. The .NET SDK installs silently, compiles the ASP.NET Core Ocelot project, and launches on port 5000.
  4. Microsoft Edge launches automatically displaying the proxied response from upstream.
  5. When finished, close the Sandbox window; all temporary project files, binaries, and logs are instantly purged.

This automated workflow provides an efficient, clean environment for testing Ocelot API Gateway configurations on Windows 11 without impacting your host development system.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.