eng/packages/http-client-csharp/doc/DEV_ONBOARDING.md
Welcome to the TypeSpec C# Generator project! This guide will help SDK developers understand the generator ecosystem and get started with generating .NET libraries from TypeSpec definitions.
The TypeSpec C# Generator (also known as Microsoft TypeSpec Generator or MTG) is a tool that generates .NET client libraries from TypeSpec service definitions. This replaces the legacy AutoRest-based code generation pipeline and provides improved type safety, better API design, and enhanced maintainability.
There are three variants of the C# emitter, each serving different scenarios:
@typespec/http-client-csharp)eng/http-client-csharp-emitter-package.json@azure-typespec/http-client-csharp)eng/azure-typespec-http-client-csharp-emitter-package.json@azure-typespec/http-client-csharp-mgmt)eng/azure-typespec-http-client-csharp-mgmt-emitter-package.jsontsp-client is the orchestration tool that partners and build pipelines use to invoke the TypeSpec compiler and emitters.
package.json file to install the correct emitter and compile TypeSpec definitionsDefines the TypeSpec compilation settings and emitter configuration.
Required Configuration for .NET Generation:
options:
"@azure-typespec/http-client-csharp":
emitter-output-dir: "{output-dir}/{service-dir}/{namespace}"
namespace: Azure.Data.AppConfiguration
model-namespace: false
Key Options:
emitter-output-dir: Directory where generated code will be placednamespace: The .NET namespace for the generated clientmodel-namespace: Whether to use a separate namespace for models (typically false)Maps the library to its TypeSpec specification and emitter configuration.
Key Properties:
directory: Path to the TypeSpec specification (typically in azure-rest-api-specs repo)commit: Git commit SHA of the spec version to userepo: Repository containing the TypeSpec specificationemitterPackageJsonPath: Path to the emitter package.json artifactExample:
directory: specification/cognitiveservices/OpenAI.Inference
commit: abc123def456
repo: Azure/azure-rest-api-specs
emitterPackageJsonPath: eng/azure-typespec-http-client-csharp-emitter-package.json
Follow these steps to generate or regenerate a .NET library from TypeSpec:
Ensure your library's tspconfig.yaml has the appropriate emitter options configured:
options:
"@azure-typespec/http-client-csharp":
emitter-output-dir: "{output-dir}/{service-dir}/{namespace}"
namespace: Azure.YourService.YourClient
model-namespace: false
Replace Azure.YourService.YourClient with your actual .NET namespace.
Set the emitterPackageJsonPath to the appropriate artifact:
eng/azure-typespec-http-client-csharp-emitter-package.jsoneng/azure-typespec-http-client-csharp-mgmt-emitter-package.jsoneng/http-client-csharp-emitter-package.jsonExample:
emitterPackageJsonPath: "eng/azure-typespec-http-client-csharp-emitter-package.json"
If your library is being migrated from AutoRest based generation, remove the following property from your .csproj file:
<IncludeAutorestDependency>true</IncludeAutorestDependency>
If the TypeSpec specification has been updated, modify the commit field in tsp-location.yaml to point to the latest commit SHA:
commit: <new-commit-sha>
From the library root directory (e.g., sdk/openai/Azure.AI.OpenAI/), execute:
dotnet build /t:GenerateCode
What Happens:
If errors occur during generation:
client.tsp (client customizations)# Generate code for a library
dotnet build /t:GenerateCode
✅ Before generating code, verify:
tspconfig.yaml has the correct emitter options configured (namespace, emitter-output-dir, etc.)tsp-location.yaml has the correct emitterPackageJsonPathtsp-location.yaml has the correct spec commit SHA (if updating)Welcome to the team! Happy generating! 🚀