đź§ Why Generating OpenApi Document at Build Time Matters
With .NET 9, Microsoft enables developers to:
- Generate OpenAPI specs at compile time, avoiding runtime dependency on Swashbuckle.
- Improve CI/CD integration by producing OpenAPI files as deterministic build artifacts.
- Ensure secure and minimal deployment environments.
You can view the full Microsoft documentation here: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/openapi/aspnetcore-openapi?view=aspnetcore-9.0&tabs=visual-studio%2Cvisual-studio-code
Before the tutorial, let’s do a very quick summary of the context.
PS.
In the article I will not say the word “Swagger” but I will express it with the concept “document generation at RunTime “.
In the article I will define CodeFirst only the RunTime approach to identify the gap with the CompileTime approach.
Don’t hate me for this! :)
One question I’ve been asking myself is why such an important feature is only being introduced now…
🔍 “Open-api spec” and the evolution through the years
What is Open Api ? From wikipedia:
The OpenAPI Specification, previously known as the Swagger Specification, is a specification for a machine-readable interface definition language for describing, producing, consuming and visualizing web services.[1] Originally developed to support the Swagger framework, it became a separate project in 2015, overseen by the OpenAPI Initiative, an open-source collaboration project of the Linux Foundation.[2][3]
From the graph we can see how the specification is improved over the years.

🔍 “Open-api spec generation approach” evolution through the years in MS ecosystem
The OpenApi document generation approach of our API interface contracts has evolved over the years in the Microsoft ecosystem.
Approaches to the Open Api Documentation generation:
- OpenApi documentation at Run Time (Code First)
- ApiOps
- Open Api documentation at CompileTime
Recently, the ApiOps approach (a GitOps methodology for APIs – see: https://learn.microsoft.com/en-gb/azure/architecture/example-scenario/devops/automated-api-deployments-apiops) has gained popularity.
Starting with .NET 9, Microsoft introduced a way to obtain documentation at compile time.

Here is the timeline chart showing the evolution of OpenAPI release approaches in the Microsoft ecosystem. Each line spans the years during which the approach has been in use and continues through 2025, indicating they are still relevant today.
Nuget trends show a deep use of RunTime Generation in the last six months.(you can see it here - https://www.nuget.org/stats/packages/Swashbuckle.AspNetCore.Swagger?groupby=Version)

Statistics must necessarily be interpreted: numerically it is not a very important data if we consider that part of the downloads are coming from automation pipelines and the different net version…).
This number becomes important if we compare it with the OpenApi library used for the new Compile-Time feature :
The stats show the situation in the last six month.
What does this number tell me?
Even if the introduction of the new “OpenApi Generated at Compile-Time” feature comes late (even if there are new methodologies like ApiOps) the IT world does not keep up with the evolution of technologies and continues to be poorly updated and does not take advantage of the improvements until years later.
Perhaps this feature isn’t as delayed as initially thought! I’ve finished my critique! :D Let’s proceed to the comparison of approaches…
🔍 Comparing OpenAPI Doc Generation Approaches
đź§ ApiOps (Contract-First Approach)
âś… Pros:
- Separation of concerns: API contracts are designed independently from code, encouraging better architecture and team collaboration.
- Strong governance: Contracts can be reviewed, approved, and versioned separately.
- CI/CD integration: Fits well into DevOps workflows and API Management tools like Azure APIM.
- Prevents errors early: Contract validation before implementation reduces mismatches and surprises in later stages.
❌ Cons:
- Higher complexity: Requires maintaining contracts separately from the codebase.
- Learning curve: Teams unfamiliar with contract-first design may need time to adopt it.
- Risk of drift: If not strictly enforced, code and contract can become out of sync.
⚙️ Compile-Time OpenAPI Generation
âś… Pros:
- Automatic alignment: The OpenAPI spec is generated directly from the code, always in sync.
- Simplicity: Less tooling required, ideal for teams focused on delivery speed.
- Native support: Supported out of the box in .NET 9, especially for Minimal APIs.
- Better performance: No runtime overhead; the spec is generated once at build time.
❌ Cons:
- Less control: May expose unintended details unless configured correctly.
- Limited customization: Harder to fine-tune the output compared to a hand-written contract.
- Code-coupled: Bugs or inconsistencies in code affect the generated contract directly.
đź•’ Runtime OpenAPI Generation
âś… Pros:
- Always up-to-date: The documentation reflects the current state of the running application.
- Easy to implement: Often just requires adding libraries like Swashbuckle or NSwag.
- Great for dev/testing: Enables instant feedback and interactive testing tools like Swagger UI.
❌ Cons:
- Performance impact: Generating documentation at runtime can affect startup or request latency.
- Security risk: Could accidentally expose sensitive endpoints or internal APIs in production.
- Not production-friendly: Generally recommended only for development environments.
📊 Comparison Table
⚠️ Note: A “No” in this table doesn’t necessarily imply a disadvantage. For example, the lack of separation in compile-time generation simplifies workflows and reduces overhead — which might be a benefit depending on your team and context.
| Feature | ApiOps (Contract-First) | Compile-Time (.NET 9) | Runtime |
|---|---|---|---|
| Contract/Code Separation | ✅ Yes | ❌ No | ❌ No |
| Governance | ✅ Strong | ❌ Limited | ❌ Limited |
| Ease of Implementation | ❌ Complex | ✅ Simple | ✅ Simple |
| Customization Flexibility | ✅ High | ❌ Limited | ❌ Limited |
| Automatic Sync with Code | ❌ Manual | ✅ Automatic | ✅ Automatic |
| Risk of Data Exposure | ✅ Low | ❌ Medium (if misconfigured) | ❌ High (in production) |
| Production-Readiness | ✅ Yes | ✅ Yes | ❌ No |
🔍 Comparing RunTime vs Compile-Time Open Api Generation in NET 9
Typically, runtime generation of Open API documentation has some prerequisites:
Your Web API must be deployed before the code and must be up and running.
There’s a waiting period during which the app might not be immediately available due to deployment.
And what you deploy does not always work (e.g. missed configuration…) :)
You must call the endpoint that exposes it, possibly from your CD-pipeline/agent in a complex security context (e.g., route openings, inbound rules…. ).
Fortunately, there’s now an easier way that avoids runtime generation of the documentation.
If you’re using .NET 9 Web API, in some scenarios — instead of using an ApiOps methodology — it might be more effective and efficient to generate the Open API documentation at compile time.
The Open Api Generation works with a mock server (According to Microsoft documentation):
“Build-time OpenAPI document generation functions by launching the app’s entry point with a mock server implementation. A mock server is required to produce accurate OpenAPI documents because all information in the OpenAPI document can’t be statically analyzed.”
Getting Started: Implementing Open Api Generation at Compile Time in a .NET 9 Web Api
On your .NET 9 project, install these two NuGet packages:
Microsoft.AspNetCore.OpenApiMicrosoft.Extensions.ApiDescription.Server
In your
Program.cs, add the service extension method:AddOpenApi();
And add the middleware:
app.MapOpenApi();
If you want to expose an Open API UI for a developer-friendly experience, you can add Scalar. Add the nuget package *Scalar.AspNetCore* and this line of code on your program.cs app.MapScalarApiReference(). The UI will be available at a URL like https://localhost:port/scalar/v1.
In alternative you can use SwaggerUI : Swashbuckle.AspNetCore.SwaggerUi package and add the middleware on program.cs with this line of code app.UseSwaggerUI(options =>{options.SwaggerEndpoint("/openapi/v1.json", "v1");}); reachable at the url https://localhost:port/swagger.
You can use both if you want :) …
- Add this to your
.csprojfile:
<PropertyGroup>
<OpenApiGenerateDocuments>true</OpenApiGenerateDocuments>
<OpenApiDocumentsDirectory>bin\$(Configuration)\$(TargetFramework)</OpenApiDocumentsDirectory>
<OpenApiGenerateDocumentsOptions>--file-name open-api</OpenApiGenerateDocumentsOptions>
</PropertyGroup>
This code will generate a file called open-api.json in your bin folder — that’s your documentation.

What if Dependency Injection (DI) is Broken , misconfigured or Requires Custom Config?
If you don’t use correcly the DI pattern the compile time Open api generation won’t work… :( You can see the sum of several errors in the code below and their explanation:
//Program.cs
var builder = WebApplication.CreateBuilder(args);
var connectionString = Environment.GetEnvironmentVariable("MyEnvVar"); //Maybe in your local it works but in the CI phase the Agent does not have the environment variable and the mock server will not be able to generate the documentation: you should avoid conditioning the Program, instead you could pass IConfiguration to your class
builder.Services.AddScoped(MyService(connectionString)); //Use the interface "builder.Services.AddScoped<IMyService, MyService>()": DI is important not only for the mock server but also for unit tests.
//your service class
public class MyService
{
public string _myConnectionString;
public SQLServiceClientFactory(string myConnectionString)
{
//You are not using DI but you are conditioning and validating the service by inserting an exception if the passed parameter is not consistent with what is expected. Sure, it works locally ... an old story ... but when the mock server will try to generate the documentation it will execute the service constructor and will not be able to generate the documentation at Compile Time. I'll just add that you can use IConfiguration in the constructor: you just have to want it!
if (string.IsNullOrWhiteSpace(myConnectionString))
{
throw new Exception("Give me the connstring dude!");
}
_myConnectionString = myConnectionString;
}
...
I don’t want to give you a lesson in Clean code in this article but just mention some real problems that the mock server will have if you don’t use DI correctly or perform misconfiguration.
But what if I still want to continue writing bad code?
You’ll need to exclude the problematic code in Program.cs. You can do it using a static method like that:
public class BuildTimeOpenApiGeneration
{
public static bool IsOpenApiMockServer()
{
return Assembly.GetEntryAssembly()?.GetName().Name =="GetDocument.Insider";
}
}
This method wraps the potentially breaking code.
In build-time, the .NET mock server will skip any code in Program.cs marked to be excluded.

Alternatively, you can use the AddServiceDefaults extension method in Program.cs which, per documentation, “adds common .NET Aspire services such as service discovery, resilience, health checks, and OpenTelemetry.”
if (BuildTimeOpenApiGeneration.IsOpenApiMockServer())
{
builder.AddServiceDefaults();
}
CI Step
The below template it’s an ADO yaml Pipeline.
After building your .NET 9 project in CI, you can publish a new artifact containing your documentation.
Please note that I use some variables such as a variable to indicate where to retrieve the SourceCode to build ( $(SourceFolder) var).
steps:
- task: DotNetCoreCLI@2
displayName: 'Build .NET'
inputs:
command: build
projects: $(SourceFolder)/*.sln
arguments: '--configuration $(build_configuration)'
verbosityRestore: Detailed
verbosityPack: Detailed
- task: CopyFiles@2
displayName: 'Copy OpenAPI JSON to publish directory'
inputs:
SourceFolder: '$(SourceFolder)'
Contents: '**/bin/$(build_configuration)/**/open-api.json'
TargetFolder: '$( TargetFolder)'
flattenFolders: true
- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact: openapi.json'
inputs:
PathtoPublish: '$( TargetFolder)/open-api.json'
ArtifactName: $(artifactName)-openapi
condition: succeeded()
CD Step
The below template it’s an ADO yaml Pipeline.
In this example, deployment is to Azure API Management Service (apim). The shell script is called from a DevOps task. Here’s a template.yaml and shell script. But remember that you could release your documentation anywhere. :)
Open Api is a standard!
(By default, the standard used is open api spec v.3 (https://spec.openapis.org/oas/v3.0.0), but you can change it! see https://learn.microsoft.com/en-gb/aspnet/core/fundamentals/openapi/aspnetcore-openapi?view=aspnetcore-9.0&tabs=visual-studio%2Cvisual-studio-code#customize-the-openapi-version-of-a-generated-document)
parameters:
- name: serviceConnection
type: string
#Your Apim Resource Group
- name: apim_resourceGroup
type: string
#Your Apim Resource Name
- name: apim_ServiceName
type: string
#the api path for the apim api
- name: apim_apiPath
type: string
#the api Id for the apim api
- name: apim_apiId
type: string
#the path at your generated at compile time json :)
- name: specificationPath
type: string
#the api webserviceUrl --> open api deploy can override the backend url setted on you api policy... then I added it!]
- name: webServiceUrl
type: string
steps:
- task: DownloadPipelineArtifact@2
displayName: "Download Open API JSON from artifact"
inputs:
buildType: 'specific'
artifactName: $(artifactName)-openapi
targetPath: $(artifact)
project: '$(DEVAZURE-PROJECT-NAME)'
definition: $(ID-PIPELINE-BUILD)
buildVersionToDownload: 'specific'
pipelineId: $(runIdBuildPipeline)
- task: AzureCLI@2
displayName: "APIM API import from Artifact Code"
inputs:
azureSubscription: ${{ parameters.serviceConnection }}
scriptType: 'bash'
scriptLocation: inlineScript
inlineScript: |
export apimServiceName="${{ parameters.apim_ServiceName }}"
export resourceGroupName="${{ parameters.apim_resourceGroup }}"
export apiPath="${{ parameters.apim_apiPath }}"
export apiId="${{ parameters.apim_apiId }}"
export specificationPath="${{ parameters.specificationPath }}"
export webServiceUrl="$(echo ${{ parameters.webServiceUrl }} | awk '{print tolower($0)}')"
chmod a=rwx $(myscriptpath)/ImportApimFromCompileTime.sh
. $(myscriptpath)/ImportApimFromCompileTime.sh
Shell Script:
#!/bin/bash
echo "##[info]apimServiceName=$apimServiceName"
echo "##[info]resourceGroupName=$resourceGroupName"
echo "##[info]apiPath=$apiPath"
echo "##[info]apiId=$apiId"
echo "##[info]specificationPath=$specificationPath"
specificationFormat=OpenApiJson
echo "Importing API..."
if [ ! -f "$specificationPath" ]; then
no_env_file="Does not exist a file with path [$specificationPath]!"
echo "##[error]$no_env_file"
echo "##vso[task.logissue type=error;sourcepath=$specificationPath;]$no_env_file"
exit 1
fi
az apim api import --path $apiPath --resource-group $resourceGroupName --service-name $apimServiceName --api-id $apiId --display-name $apiId --specification-format $specificationFormat --specification-path $specificationPath --service-url $webServiceUrl
Just as a note: the release strategy used in the tutorial is effectively an Open Api As Artifact strategy.
đź§© Conclusion
There is much more to explore — we could dive into managing APIM policies or advanced security validation. But the purpose of this article was to showcase how this new .NET 9 feature simplifies OpenAPI handling in DevOps workflows .
Why only now? Microsoft’s timing for introducing compile-time OpenAPI generation likely reflects both technical complexity and market readiness. Historically, Microsoft prioritizes stability and gradual innovation, especially in enterprise contexts. Additionally, the increased maturity of DevOps practices, community feedback, and widespread adoption of standardized API governance models (like ApiOps) probably contributed to the timing. Thus, even though this feature might seem late, it aligns well with the broader industry’s readiness to embrace such changes.
At the end “What’s the right strategy”?
While ApiOps remains the ideal approach for enterprises that require strict governance and contract reuse, compile-time generation is perfect for agile teams focused on fast iterations and minimal tooling but your project needs to be at least net9. As for runtime generation, it still has utility in development or debugging, but it’s no longer ideal for production.
It depends on your team’s scale, delivery model, governance requirements and used stack. And remember — these approaches are not mutually exclusive. You can even combine them in a hybrid architecture.