Skip to content

feat(http-client-csharp): Enable C# emitter in the TypeSpec playground #10228

@JoshLove-msft

Description

@JoshLove-msft

Summary

Add support for running the C# emitter in the TypeSpec playground. Since the C# emitter requires a .NET backend to run the generator (Microsoft.TypeSpec.Generator.dll), this involves both browser-side emitter changes and a new server component.

Problem

The TypeSpec playground currently only supports client-side emitters (OpenAPI3, JSON Schema, etc.). The C# emitter cannot run entirely in the browser because it spawns a .NET subprocess to generate C# code. Users have no way to preview generated C# output from TypeSpec definitions in the playground.

Solution

1. Browser-compatible emitter

  • Make child_process, fs, path, and url imports dynamic (lazy) so the emitter module loads in browsers without crashing
  • Add playground-server-url emitter option
  • When in a browser environment, POST the serialized code model to a remote server instead of spawning dotnet locally
  • Read server URL from globalThis.TYPESPEC_PLAYGROUND_SERVER_URL for deployment configuration

2. .NET playground server

  • ASP.NET Core minimal API (playground-server/) that accepts code model JSON + configuration, runs the .NET generator, and returns generated C# files
  • Rate limiting (10 requests/minute) and CORS protection
  • Dockerfile for Azure Container Apps deployment
  • Generator name passed from emitter (supports both unbranded and Azure emitters via MEF)

3. Playground UI improvements

  • Compiling spinner overlay during server round-trips
  • Preserve previous output files during transient syntax errors (mid-typing)
  • Race condition fix: discard stale compilation results
  • Opt-in change highlighting: bold green file names in tree for changed files

4. Bundle upload script

  • Standalone script to bundle and upload the C# emitter to the playground package storage account
  • Follows the same pattern as core packages but runs independently since http-client-csharp is outside the pnpm workspace

Deployment

  • Deploy the .NET server to Azure Container Apps
  • Set PLAYGROUND_URL env var on the server for CORS
  • Set TYPESPEC_PLAYGROUND_SERVER_URL in the playground HTML for the deployed server URL
  • Run the upload script to publish the bundled emitter to the storage account

Azure playground

The same architecture supports the Azure playground (typespec-azure repo) with @azure-typespec/http-client-csharp. The server accepts a generatorName parameter so the same server instance can serve both unbranded and Azure emitters (different MEF-discovered generator classes, same generator DLL + Azure plugin DLL).

Metadata

Metadata

Assignees

Labels

emitter:client:csharpIssue for the C# client emitter: @typespec/http-client-csharp

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions