A set of technologies in .NET for building web applications and web services. Miscellaneous topics that do not fit into specific categories.
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:
- Don’t use
Microsoft.Extensions.ApiDescription.Serverin 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.).
- Use
Microsoft.Azure.Functions.Worker.Extensions.OpenApiat 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" />
- 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:
- Create serverless APIs using Azure Functions and API Management | Microsoft Learn
- Guide for running C# Azure Functions in an isolated worker process | Microsoft Learn
- Azure Functions Core Tools reference | Microsoft Learn
Hope this helps. Feel free to let me know if you need any help or any additional clarification.