Errors when trying to generate swagger.json at build time

Gill, Liam 20 Reputation points
2025-07-29T15:20:08.0533333+00:00

I've created a new .net 9 isolated function app using Visual Version 17.14.9.

I'm using the Microsoft.Azure.Functions.Worker.Extensions.OpenAPI (1.51) package to add swagger definitions to the functions. This works fine, and I can get the swagger file via the browser / function query.

I'd like to generate the Swagger.json file as part of the build process and I'm trying to use the Microsoft.Extensions.ApiDescription.Server (9.0.7) to do this.

When I try to build I get the following build error:

System.AggregateException: One or more errors occurred. (Configuration is missing the 'HostEndpoint' information. Please ensure an entry with the key 'Functions:Worker:HostEndpoint' is present in your configuration.)

I have tried adding the above to my local.setting.json but it makes no difference.

Project CS:

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>

<TargetFramework>net9.0</TargetFramework>

<AzureFunctionsVersion>v4</AzureFunctionsVersion>

<OutputType>Exe</OutputType>

<ImplicitUsings>enable</ImplicitUsings>

<Nullable>enable</Nullable>

</PropertyGroup>

<ItemGroup>

<FrameworkReference Include="Microsoft.AspNetCore.App" />

<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.23.0" />

<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.0.0" />

<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="2.0.0" />

<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="2.0.2" />

<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.OpenApi" Version="1.5.1" />

<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.0.5" />

<PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="9.0.7">

<PrivateAssets>all</PrivateAssets>

<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

</PackageReference>

</ItemGroup>

</Project>

Program.cs

using Microsoft.Azure.Functions.Worker;

using Microsoft.Azure.Functions.Worker.Builder;

using Microsoft.Extensions.DependencyInjection;

using Microsoft.Extensions.Hosting;

var builder = FunctionsApplication.CreateBuilder(args);

builder.ConfigureFunctionsWebApplication();

builder.Services

.AddApplicationInsightsTelemetryWorkerService()

.ConfigureFunctionsApplicationInsights();

builder.Build().Run();

Developer technologies | ASP.NET Core | Other
0 comments No comments

Answer accepted by question author
Danny Nguyen (WICLOUD CORPORATION) 8,900 Reputation points Microsoft External Staff Moderator
2025-07-30T08:54:57.73+00:00

Hi @Gill, Liam ,

Thank you for your detailed code.

Unfortunately, Microsoft.Wxtensions.ApiDescription.Server is not designed to work with Azure Functions, especially in isolated process mode. That package is intended for ASP.NET Core apps and expects certain WebHost/ApiExplorer infrastructure that Azure Functions simply doesn’t have.


I suggest the following:

  1. Don’t use Microsoft.Extensions.ApiDescription.Server in Azure Functions

That package is meant for generating Swagger from ASP.NET Core middleware, which Azure Functions (even isolated) doesn't use. Using it causes the error because it tries to build a service provider expecting ASP.NET Core configuration keys and middleware (HostEndpoint, etc.).

  1. Use Microsoft.Azure.Functions.Worker.Extensions.OpenApi at runtime instead

The official and supported way to get a swagger.json for Azure Functions is via HTTP endpoints, not during build.

You already have this working:

GET http://localhost:<port>/api/swagger.json

This is the correct and supported way with:

<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.OpenApi" Version="1.5.1" />
  1. Automate Swagger File Extraction During CI/CD or Build Using a Script

Since swagger.json can only be generated at runtime, you can automate a local HTTP call to extract it and save the file after starting the Function app.

For example, in a CI/CD pipeline or post-build step:

  • Bash example:
func start --csharp --verbose &
sleep 10 # wait for app to boot
 
curl http://localhost:xxxx/api/swagger.json -o swagger.json
kill $(lsof -t -i:7071)
  • PowerShell (for Windows-based dev):
Start-Process -FilePath "func.exe" -ArgumentList "start" -NoNewWindow
Start-Sleep -Seconds 10
 
Invoke-WebRequest -Uri http://localhost:xxxx/api/swagger.json -OutFile swagger.json
 
Stop-Process -Name "func"

You can plug this into your build.ps1 or postbuild script.


Here are some additional on Azure Functions that you might want to check out:


Hope this helps. Feel free to let me know if you need any help or any additional clarification.

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.