├── .gitignore ├── AspireSwaggerUi.ApiService ├── AspireSwaggerUi.ApiService.csproj ├── Program.cs ├── Properties │ └── launchSettings.json ├── appsettings.Development.json └── appsettings.json ├── AspireSwaggerUi.AppHost ├── AspireSwaggerUi.AppHost.csproj ├── Program.cs ├── Properties │ └── launchSettings.json ├── appsettings.Development.json └── appsettings.json ├── AspireSwaggerUi.sln ├── LICENSE ├── README.md └── SwaggerUi.Aspire.Hosting ├── OpenApiRouteBuilderExtensions.cs ├── SwaggerUi.Aspire.Hosting.csproj ├── SwaggerUiAnnotation.cs ├── SwaggerUiExtensions.cs └── SwaggerUiResource.cs /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from `dotnet new gitignore` 5 | 6 | # dotenv files 7 | .env 8 | 9 | # User-specific files 10 | *.rsuser 11 | *.suo 12 | *.user 13 | *.userosscache 14 | *.sln.docstates 15 | 16 | # User-specific files (MonoDevelop/Xamarin Studio) 17 | *.userprefs 18 | 19 | # Mono auto generated files 20 | mono_crash.* 21 | 22 | # Build results 23 | [Dd]ebug/ 24 | [Dd]ebugPublic/ 25 | [Rr]elease/ 26 | [Rr]eleases/ 27 | x64/ 28 | x86/ 29 | [Ww][Ii][Nn]32/ 30 | [Aa][Rr][Mm]/ 31 | [Aa][Rr][Mm]64/ 32 | bld/ 33 | [Bb]in/ 34 | [Oo]bj/ 35 | [Ll]og/ 36 | [Ll]ogs/ 37 | 38 | # Visual Studio 2015/2017 cache/options directory 39 | .vs/ 40 | # Uncomment if you have tasks that create the project's static files in wwwroot 41 | #wwwroot/ 42 | 43 | # Visual Studio 2017 auto generated files 44 | Generated\ Files/ 45 | 46 | # MSTest test Results 47 | [Tt]est[Rr]esult*/ 48 | [Bb]uild[Ll]og.* 49 | 50 | # NUnit 51 | *.VisualState.xml 52 | TestResult.xml 53 | nunit-*.xml 54 | 55 | # Build Results of an ATL Project 56 | [Dd]ebugPS/ 57 | [Rr]eleasePS/ 58 | dlldata.c 59 | 60 | # Benchmark Results 61 | BenchmarkDotNet.Artifacts/ 62 | 63 | # .NET 64 | project.lock.json 65 | project.fragment.lock.json 66 | artifacts/ 67 | 68 | # Tye 69 | .tye/ 70 | 71 | # ASP.NET Scaffolding 72 | ScaffoldingReadMe.txt 73 | 74 | # StyleCop 75 | StyleCopReport.xml 76 | 77 | # Files built by Visual Studio 78 | *_i.c 79 | *_p.c 80 | *_h.h 81 | *.ilk 82 | *.meta 83 | *.obj 84 | *.iobj 85 | *.pch 86 | *.pdb 87 | *.ipdb 88 | *.pgc 89 | *.pgd 90 | *.rsp 91 | *.sbr 92 | *.tlb 93 | *.tli 94 | *.tlh 95 | *.tmp 96 | *.tmp_proj 97 | *_wpftmp.csproj 98 | *.log 99 | *.tlog 100 | *.vspscc 101 | *.vssscc 102 | .builds 103 | *.pidb 104 | *.svclog 105 | *.scc 106 | 107 | # Chutzpah Test files 108 | _Chutzpah* 109 | 110 | # Visual C++ cache files 111 | ipch/ 112 | *.aps 113 | *.ncb 114 | *.opendb 115 | *.opensdf 116 | *.sdf 117 | *.cachefile 118 | *.VC.db 119 | *.VC.VC.opendb 120 | 121 | # Visual Studio profiler 122 | *.psess 123 | *.vsp 124 | *.vspx 125 | *.sap 126 | 127 | # Visual Studio Trace Files 128 | *.e2e 129 | 130 | # TFS 2012 Local Workspace 131 | $tf/ 132 | 133 | # Guidance Automation Toolkit 134 | *.gpState 135 | 136 | # ReSharper is a .NET coding add-in 137 | _ReSharper*/ 138 | *.[Rr]e[Ss]harper 139 | *.DotSettings.user 140 | 141 | # TeamCity is a build add-in 142 | _TeamCity* 143 | 144 | # DotCover is a Code Coverage Tool 145 | *.dotCover 146 | 147 | # AxoCover is a Code Coverage Tool 148 | .axoCover/* 149 | !.axoCover/settings.json 150 | 151 | # Coverlet is a free, cross platform Code Coverage Tool 152 | coverage*.json 153 | coverage*.xml 154 | coverage*.info 155 | 156 | # Visual Studio code coverage results 157 | *.coverage 158 | *.coveragexml 159 | 160 | # NCrunch 161 | _NCrunch_* 162 | .*crunch*.local.xml 163 | nCrunchTemp_* 164 | 165 | # MightyMoose 166 | *.mm.* 167 | AutoTest.Net/ 168 | 169 | # Web workbench (sass) 170 | .sass-cache/ 171 | 172 | # Installshield output folder 173 | [Ee]xpress/ 174 | 175 | # DocProject is a documentation generator add-in 176 | DocProject/buildhelp/ 177 | DocProject/Help/*.HxT 178 | DocProject/Help/*.HxC 179 | DocProject/Help/*.hhc 180 | DocProject/Help/*.hhk 181 | DocProject/Help/*.hhp 182 | DocProject/Help/Html2 183 | DocProject/Help/html 184 | 185 | # Click-Once directory 186 | publish/ 187 | 188 | # Publish Web Output 189 | *.[Pp]ublish.xml 190 | *.azurePubxml 191 | # Note: Comment the next line if you want to checkin your web deploy settings, 192 | # but database connection strings (with potential passwords) will be unencrypted 193 | *.pubxml 194 | *.publishproj 195 | 196 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 197 | # checkin your Azure Web App publish settings, but sensitive information contained 198 | # in these scripts will be unencrypted 199 | PublishScripts/ 200 | 201 | # NuGet Packages 202 | *.nupkg 203 | # NuGet Symbol Packages 204 | *.snupkg 205 | # The packages folder can be ignored because of Package Restore 206 | **/[Pp]ackages/* 207 | # except build/, which is used as an MSBuild target. 208 | !**/[Pp]ackages/build/ 209 | # Uncomment if necessary however generally it will be regenerated when needed 210 | #!**/[Pp]ackages/repositories.config 211 | # NuGet v3's project.json files produces more ignorable files 212 | *.nuget.props 213 | *.nuget.targets 214 | 215 | # Microsoft Azure Build Output 216 | csx/ 217 | *.build.csdef 218 | 219 | # Microsoft Azure Emulator 220 | ecf/ 221 | rcf/ 222 | 223 | # Windows Store app package directories and files 224 | AppPackages/ 225 | BundleArtifacts/ 226 | Package.StoreAssociation.xml 227 | _pkginfo.txt 228 | *.appx 229 | *.appxbundle 230 | *.appxupload 231 | 232 | # Visual Studio cache files 233 | # files ending in .cache can be ignored 234 | *.[Cc]ache 235 | # but keep track of directories ending in .cache 236 | !?*.[Cc]ache/ 237 | 238 | # Others 239 | ClientBin/ 240 | ~$* 241 | *~ 242 | *.dbmdl 243 | *.dbproj.schemaview 244 | *.jfm 245 | *.pfx 246 | *.publishsettings 247 | orleans.codegen.cs 248 | 249 | # Including strong name files can present a security risk 250 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 251 | #*.snk 252 | 253 | # Since there are multiple workflows, uncomment next line to ignore bower_components 254 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 255 | #bower_components/ 256 | 257 | # RIA/Silverlight projects 258 | Generated_Code/ 259 | 260 | # Backup & report files from converting an old project file 261 | # to a newer Visual Studio version. Backup files are not needed, 262 | # because we have git ;-) 263 | _UpgradeReport_Files/ 264 | Backup*/ 265 | UpgradeLog*.XML 266 | UpgradeLog*.htm 267 | ServiceFabricBackup/ 268 | *.rptproj.bak 269 | 270 | # SQL Server files 271 | *.mdf 272 | *.ldf 273 | *.ndf 274 | 275 | # Business Intelligence projects 276 | *.rdl.data 277 | *.bim.layout 278 | *.bim_*.settings 279 | *.rptproj.rsuser 280 | *- [Bb]ackup.rdl 281 | *- [Bb]ackup ([0-9]).rdl 282 | *- [Bb]ackup ([0-9][0-9]).rdl 283 | 284 | # Microsoft Fakes 285 | FakesAssemblies/ 286 | 287 | # GhostDoc plugin setting file 288 | *.GhostDoc.xml 289 | 290 | # Node.js Tools for Visual Studio 291 | .ntvs_analysis.dat 292 | node_modules/ 293 | 294 | # Visual Studio 6 build log 295 | *.plg 296 | 297 | # Visual Studio 6 workspace options file 298 | *.opt 299 | 300 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 301 | *.vbw 302 | 303 | # Visual Studio 6 auto-generated project file (contains which files were open etc.) 304 | *.vbp 305 | 306 | # Visual Studio 6 workspace and project file (working project files containing files to include in project) 307 | *.dsw 308 | *.dsp 309 | 310 | # Visual Studio 6 technical files 311 | *.ncb 312 | *.aps 313 | 314 | # Visual Studio LightSwitch build output 315 | **/*.HTMLClient/GeneratedArtifacts 316 | **/*.DesktopClient/GeneratedArtifacts 317 | **/*.DesktopClient/ModelManifest.xml 318 | **/*.Server/GeneratedArtifacts 319 | **/*.Server/ModelManifest.xml 320 | _Pvt_Extensions 321 | 322 | # Paket dependency manager 323 | .paket/paket.exe 324 | paket-files/ 325 | 326 | # FAKE - F# Make 327 | .fake/ 328 | 329 | # CodeRush personal settings 330 | .cr/personal 331 | 332 | # Python Tools for Visual Studio (PTVS) 333 | __pycache__/ 334 | *.pyc 335 | 336 | # Cake - Uncomment if you are using it 337 | # tools/** 338 | # !tools/packages.config 339 | 340 | # Tabs Studio 341 | *.tss 342 | 343 | # Telerik's JustMock configuration file 344 | *.jmconfig 345 | 346 | # BizTalk build output 347 | *.btp.cs 348 | *.btm.cs 349 | *.odx.cs 350 | *.xsd.cs 351 | 352 | # OpenCover UI analysis results 353 | OpenCover/ 354 | 355 | # Azure Stream Analytics local run output 356 | ASALocalRun/ 357 | 358 | # MSBuild Binary and Structured Log 359 | *.binlog 360 | 361 | # NVidia Nsight GPU debugger configuration file 362 | *.nvuser 363 | 364 | # MFractors (Xamarin productivity tool) working folder 365 | .mfractor/ 366 | 367 | # Local History for Visual Studio 368 | .localhistory/ 369 | 370 | # Visual Studio History (VSHistory) files 371 | .vshistory/ 372 | 373 | # BeatPulse healthcheck temp database 374 | healthchecksdb 375 | 376 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 377 | MigrationBackup/ 378 | 379 | # Ionide (cross platform F# VS Code tools) working folder 380 | .ionide/ 381 | 382 | # Fody - auto-generated XML schema 383 | FodyWeavers.xsd 384 | 385 | # VS Code files for those working on multiple tools 386 | .vscode/* 387 | !.vscode/settings.json 388 | !.vscode/tasks.json 389 | !.vscode/launch.json 390 | !.vscode/extensions.json 391 | *.code-workspace 392 | 393 | # Local History for Visual Studio Code 394 | .history/ 395 | 396 | # Windows Installer files from build outputs 397 | *.cab 398 | *.msi 399 | *.msix 400 | *.msm 401 | *.msp 402 | 403 | # JetBrains Rider 404 | *.sln.iml 405 | .idea 406 | 407 | ## 408 | ## Visual studio for Mac 409 | ## 410 | 411 | 412 | # globs 413 | Makefile.in 414 | *.userprefs 415 | *.usertasks 416 | config.make 417 | config.status 418 | aclocal.m4 419 | install-sh 420 | autom4te.cache/ 421 | *.tar.gz 422 | tarballs/ 423 | test-results/ 424 | 425 | # Mac bundle stuff 426 | *.dmg 427 | *.app 428 | 429 | # content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore 430 | # General 431 | .DS_Store 432 | .AppleDouble 433 | .LSOverride 434 | 435 | # Icon must end with two \r 436 | Icon 437 | 438 | 439 | # Thumbnails 440 | ._* 441 | 442 | # Files that might appear in the root of a volume 443 | .DocumentRevisions-V100 444 | .fseventsd 445 | .Spotlight-V100 446 | .TemporaryItems 447 | .Trashes 448 | .VolumeIcon.icns 449 | .com.apple.timemachine.donotpresent 450 | 451 | # Directories potentially created on remote AFP share 452 | .AppleDB 453 | .AppleDesktop 454 | Network Trash Folder 455 | Temporary Items 456 | .apdisk 457 | 458 | # content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore 459 | # Windows thumbnail cache files 460 | Thumbs.db 461 | ehthumbs.db 462 | ehthumbs_vista.db 463 | 464 | # Dump file 465 | *.stackdump 466 | 467 | # Folder config file 468 | [Dd]esktop.ini 469 | 470 | # Recycle Bin used on file shares 471 | $RECYCLE.BIN/ 472 | 473 | # Windows Installer files 474 | *.cab 475 | *.msi 476 | *.msix 477 | *.msm 478 | *.msp 479 | 480 | # Windows shortcuts 481 | *.lnk 482 | 483 | # Vim temporary swap files 484 | *.swp 485 | -------------------------------------------------------------------------------- /AspireSwaggerUi.ApiService/AspireSwaggerUi.ApiService.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /AspireSwaggerUi.ApiService/Program.cs: -------------------------------------------------------------------------------- 1 | var builder = WebApplication.CreateBuilder(args); 2 | 3 | builder.Services.AddSwaggerGen(); 4 | builder.Services.AddEndpointsApiExplorer(); 5 | 6 | var app = builder.Build(); 7 | 8 | app.UseSwagger(); 9 | 10 | app.MapGet("/", () => "Hello World!"); 11 | app.MapGet("/foo", () => "foo"); 12 | app.MapGet("/bar", () => "bar"); 13 | 14 | app.Run(); 15 | -------------------------------------------------------------------------------- /AspireSwaggerUi.ApiService/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json.schemastore.org/launchsettings.json", 3 | "iisSettings": { 4 | "windowsAuthentication": false, 5 | "anonymousAuthentication": true, 6 | "iisExpress": { 7 | "applicationUrl": "http://localhost:20989", 8 | "sslPort": 44309 9 | } 10 | }, 11 | "profiles": { 12 | "http": { 13 | "commandName": "Project", 14 | "dotnetRunMessages": true, 15 | "launchBrowser": true, 16 | "applicationUrl": "http://localhost:5053", 17 | "environmentVariables": { 18 | "ASPNETCORE_ENVIRONMENT": "Development" 19 | } 20 | }, 21 | "https": { 22 | "commandName": "Project", 23 | "dotnetRunMessages": true, 24 | "launchBrowser": true, 25 | "applicationUrl": "https://localhost:7047;http://localhost:5053", 26 | "environmentVariables": { 27 | "ASPNETCORE_ENVIRONMENT": "Development" 28 | } 29 | }, 30 | "IIS Express": { 31 | "commandName": "IISExpress", 32 | "launchBrowser": true, 33 | "environmentVariables": { 34 | "ASPNETCORE_ENVIRONMENT": "Development" 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /AspireSwaggerUi.ApiService/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /AspireSwaggerUi.ApiService/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | }, 8 | "AllowedHosts": "*" 9 | } 10 | -------------------------------------------------------------------------------- /AspireSwaggerUi.AppHost/AspireSwaggerUi.AppHost.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net8.0 6 | enable 7 | enable 8 | true 9 | 4be63cb0-9950-4503-8ad3-a6a4c3c41323 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /AspireSwaggerUi.AppHost/Program.cs: -------------------------------------------------------------------------------- 1 | var builder = DistributedApplication.CreateBuilder(args); 2 | 3 | builder.AddProject("api").WithSwaggerUI(); 4 | 5 | builder.Build().Run(); 6 | -------------------------------------------------------------------------------- /AspireSwaggerUi.AppHost/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/launchsettings.json", 3 | "profiles": { 4 | "https": { 5 | "commandName": "Project", 6 | "dotnetRunMessages": true, 7 | "launchBrowser": true, 8 | "applicationUrl": "https://localhost:17283;http://localhost:15246", 9 | "environmentVariables": { 10 | "ASPNETCORE_ENVIRONMENT": "Development", 11 | "DOTNET_ENVIRONMENT": "Development", 12 | "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21168", 13 | "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22230" 14 | } 15 | }, 16 | "http": { 17 | "commandName": "Project", 18 | "dotnetRunMessages": true, 19 | "launchBrowser": true, 20 | "applicationUrl": "http://localhost:15246", 21 | "environmentVariables": { 22 | "ASPNETCORE_ENVIRONMENT": "Development", 23 | "DOTNET_ENVIRONMENT": "Development", 24 | "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19202", 25 | "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20043" 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /AspireSwaggerUi.AppHost/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /AspireSwaggerUi.AppHost/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning", 6 | "Aspire.Hosting.Dcp": "Warning" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /AspireSwaggerUi.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.10.34902.84 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SwaggerUI.Aspire.Hosting", "SwaggerUI.Aspire.Hosting\SwaggerUI.Aspire.Hosting.csproj", "{968DCFC9-6382-4CFB-BE3A-3446C741DC06}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspireSwaggerUI.AppHost", "AspireSwaggerUI.AppHost\AspireSwaggerUI.AppHost.csproj", "{81117F0C-5722-4211-BEE2-7BF3A506BD39}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspireSwaggerUI.ApiService", "AspireSwaggerUI.ApiService\AspireSwaggerUI.ApiService.csproj", "{5F5431BF-AE7F-4FD8-A3D2-D422634AEA3C}" 11 | EndProject 12 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{2A10082B-D22C-4EF2-BA9D-2541D913F40E}" 13 | ProjectSection(SolutionItems) = preProject 14 | README.md = README.md 15 | EndProjectSection 16 | EndProject 17 | Global 18 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 19 | Debug|Any CPU = Debug|Any CPU 20 | Release|Any CPU = Release|Any CPU 21 | EndGlobalSection 22 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 23 | {968DCFC9-6382-4CFB-BE3A-3446C741DC06}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 24 | {968DCFC9-6382-4CFB-BE3A-3446C741DC06}.Debug|Any CPU.Build.0 = Debug|Any CPU 25 | {968DCFC9-6382-4CFB-BE3A-3446C741DC06}.Release|Any CPU.ActiveCfg = Release|Any CPU 26 | {968DCFC9-6382-4CFB-BE3A-3446C741DC06}.Release|Any CPU.Build.0 = Release|Any CPU 27 | {81117F0C-5722-4211-BEE2-7BF3A506BD39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 28 | {81117F0C-5722-4211-BEE2-7BF3A506BD39}.Debug|Any CPU.Build.0 = Debug|Any CPU 29 | {81117F0C-5722-4211-BEE2-7BF3A506BD39}.Release|Any CPU.ActiveCfg = Release|Any CPU 30 | {81117F0C-5722-4211-BEE2-7BF3A506BD39}.Release|Any CPU.Build.0 = Release|Any CPU 31 | {5F5431BF-AE7F-4FD8-A3D2-D422634AEA3C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 32 | {5F5431BF-AE7F-4FD8-A3D2-D422634AEA3C}.Debug|Any CPU.Build.0 = Debug|Any CPU 33 | {5F5431BF-AE7F-4FD8-A3D2-D422634AEA3C}.Release|Any CPU.ActiveCfg = Release|Any CPU 34 | {5F5431BF-AE7F-4FD8-A3D2-D422634AEA3C}.Release|Any CPU.Build.0 = Release|Any CPU 35 | EndGlobalSection 36 | GlobalSection(SolutionProperties) = preSolution 37 | HideSolutionNode = FALSE 38 | EndGlobalSection 39 | GlobalSection(ExtensibilityGlobals) = postSolution 40 | SolutionGUId = {A3D20B0C-EE22-4FCD-99F3-CF5B993FE1E0} 41 | EndGlobalSection 42 | EndGlobal 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 David Fowler 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## SwaggerUI.Aspire.Hosting 2 | 3 | This repository shows a sample of how use aspire to show a swagger ui for any resource that exposes an open api endpoint. 4 | 5 | Just add `WithSwaggerUI()` to any project resource builder and you are good to go! 6 | 7 | ```C# 8 | var builder = DistributedApplication.CreateBuilder(args); 9 | 10 | builder.AddProject("api") 11 | .WithSwaggerUI(); 12 | 13 | builder.Build().Run(); 14 | ``` 15 | 16 | -------------------------------------------------------------------------------- /SwaggerUi.Aspire.Hosting/OpenApiRouteBuilderExtensions.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Builder; 2 | using Microsoft.AspNetCore.Http; 3 | using Microsoft.AspNetCore.Routing; 4 | 5 | internal static class OpenApiRouteBuilderExtensions 6 | { 7 | /// 8 | /// Helper method to render Swagger UI view for testing. 9 | /// 10 | public static IEndpointConventionBuilder MapSwaggerUI(this IEndpointRouteBuilder endpoints) 11 | { 12 | return endpoints.MapGet("/swagger/{resourceName}/{documentName}", (string resourceName, string documentName) => Results.Content($$""" 13 | 14 | 15 | 16 | OpenAPI -{{resourceName}}- {{documentName}} 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 43 | 44 | 45 | """, "text/html")).ExcludeFromDescription(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /SwaggerUi.Aspire.Hosting/SwaggerUi.Aspire.Hosting.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net8.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /SwaggerUi.Aspire.Hosting/SwaggerUiAnnotation.cs: -------------------------------------------------------------------------------- 1 | using Aspire.Hosting.ApplicationModel; 2 | 3 | public class SwaggerUIAnnotation(string[] documentNames, string path, EndpointReference endpointReference) : IResourceAnnotation 4 | { 5 | public string[] DocumentNames { get; } = documentNames; 6 | public string Path { get; } = path; 7 | public EndpointReference EndpointReference { get; } = endpointReference; 8 | } 9 | -------------------------------------------------------------------------------- /SwaggerUi.Aspire.Hosting/SwaggerUiExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Immutable; 2 | using Aspire.Hosting; 3 | using Aspire.Hosting.ApplicationModel; 4 | using Aspire.Hosting.Lifecycle; 5 | using Microsoft.AspNetCore.Builder; 6 | using Microsoft.AspNetCore.Hosting.Server; 7 | using Microsoft.AspNetCore.Hosting.Server.Features; 8 | using Microsoft.AspNetCore.Http; 9 | using Microsoft.AspNetCore.Http.Features; 10 | using Microsoft.Extensions.DependencyInjection; 11 | using Microsoft.Extensions.Logging; 12 | using Yarp.ReverseProxy.Forwarder; 13 | 14 | public static class SwaggerUIExtensions 15 | { 16 | /// 17 | /// Maps the swagger ui endpoint to the application. 18 | /// 19 | /// The resource builder. 20 | /// The list of open api documents. Defaults to "v1" if null. 21 | /// The path to the open api document. 22 | /// The endpoint name 23 | public static IResourceBuilder WithSwaggerUI(this IResourceBuilder builder, 24 | string[]? documentNames = null, string path = "swagger/v1/swagger.json", string endpointName = "http") 25 | { 26 | if (!builder.ApplicationBuilder.Resources.OfType().Any()) 27 | { 28 | // Add the swagger ui code hook and resource 29 | builder.ApplicationBuilder.Services.TryAddLifecycleHook(); 30 | builder.ApplicationBuilder.AddResource(new SwaggerUIResource("swagger-ui")) 31 | .WithInitialState(new CustomResourceSnapshot 32 | { 33 | ResourceType = "swagger-ui", 34 | Properties = [], 35 | State = "Starting" 36 | }) 37 | .ExcludeFromManifest(); 38 | } 39 | 40 | return builder.WithAnnotation(new SwaggerUIAnnotation(documentNames ?? ["v1"], path, builder.GetEndpoint(endpointName))); 41 | } 42 | 43 | class SwaggerUiHook(ResourceNotificationService notificationService, 44 | ResourceLoggerService resourceLoggerService) : IDistributedApplicationLifecycleHook 45 | { 46 | public async Task AfterEndpointsAllocatedAsync(DistributedApplicationModel appModel, CancellationToken cancellationToken = default) 47 | { 48 | var openApiResource = appModel.Resources.OfType().SingleOrDefault(); 49 | 50 | if (openApiResource is null) 51 | { 52 | return; 53 | } 54 | 55 | // We host a single webserver that will manage the swagger ui endpoints for all resources 56 | var builder = WebApplication.CreateSlimBuilder(); 57 | 58 | builder.Services.AddHttpForwarder(); 59 | builder.Logging.ClearProviders(); 60 | 61 | builder.Logging.AddProvider(new ResourceLoggerProvider(resourceLoggerService.GetLogger(openApiResource.Name))); 62 | 63 | var app = builder.Build(); 64 | 65 | // openapi/resourcename/documentname.json 66 | app.MapSwaggerUI(); 67 | 68 | var resourceToEndpoint = new Dictionary(); 69 | var portToResourceMap = new Dictionary)>(); 70 | 71 | foreach (var r in appModel.Resources) 72 | { 73 | if (!r.TryGetLastAnnotation(out var annotation)) 74 | { 75 | continue; 76 | } 77 | 78 | // We store the url and path for each resource so we can hit the open api endpoint 79 | resourceToEndpoint[r.Name] = (annotation.EndpointReference.Url, annotation.Path); 80 | 81 | var paths = new List(); 82 | // To avoid cors issues, we expose URLs that send requests to the apphost and then forward them to the actual resource 83 | foreach (var documentName in annotation.DocumentNames) 84 | { 85 | paths.Add($"swagger/{r.Name}/{documentName}"); 86 | } 87 | 88 | // We store the URL for the resource on the host so we can map it back to the actual address once they are allocated 89 | portToResourceMap[app.Urls.Count] = (annotation.EndpointReference.Url, paths); 90 | 91 | // We add a new URL for each resource that has a swagger ui annotation 92 | // This is because swagger ui takes over the entire url space 93 | app.Urls.Add("http://127.0.0.1:0"); 94 | } 95 | 96 | var client = new HttpMessageInvoker(new SocketsHttpHandler()); 97 | 98 | // Swagger UI will make requests to the apphost so we can avoid doing any CORS configuration. 99 | app.Map("/openapi/{resourceName}/{documentName}.json", 100 | async (string resourceName, string documentName, IHttpForwarder forwarder, HttpContext context) => 101 | { 102 | var (endpoint, path) = resourceToEndpoint[resourceName]; 103 | 104 | await forwarder.SendAsync(context, endpoint, client, (c, r) => 105 | { 106 | r.RequestUri = new($"{endpoint}/{path}"); 107 | return ValueTask.CompletedTask; 108 | }); 109 | }); 110 | 111 | app.Map("{*path}", async (HttpContext context, IHttpForwarder forwarder, string? path) => 112 | { 113 | var (endpoint, _) = portToResourceMap[context.Connection.LocalPort]; 114 | 115 | var uriBuilder = new UriBuilder(path is null ? endpoint : $"{endpoint}/{path}") 116 | { 117 | Query = context.Request.QueryString.Value 118 | }; 119 | 120 | await forwarder.SendAsync(context, endpoint, client, (c, r) => 121 | { 122 | r.RequestUri = uriBuilder.Uri; 123 | return ValueTask.CompletedTask; 124 | }); 125 | }); 126 | 127 | await app.StartAsync(cancellationToken); 128 | 129 | var addresses = app.Services.GetRequiredService().Features.GetRequiredFeature().Addresses; 130 | 131 | var urls = ImmutableArray.CreateBuilder(); 132 | 133 | // Map our index back to the actual address 134 | var index = 0; 135 | foreach (var rawAddress in addresses) 136 | { 137 | var address = BindingAddress.Parse(rawAddress); 138 | 139 | // We map the bound port to the resource URL. This lets us forward requests to the correct resource 140 | var (_, paths) = portToResourceMap[address.Port] = portToResourceMap[index++]; 141 | 142 | // We add the swagger ui endpoint for each resource 143 | foreach (var p in paths) 144 | { 145 | urls.Add(new UrlSnapshot(rawAddress, $"{rawAddress}/{p}", IsInternal: false)); 146 | } 147 | } 148 | 149 | await notificationService.PublishUpdateAsync(openApiResource, s => s with 150 | { 151 | State = "Running", 152 | Urls = urls.ToImmutableArray() 153 | }); 154 | } 155 | } 156 | 157 | private class ResourceLoggerProvider(ILogger logger) : ILoggerProvider 158 | { 159 | public ILogger CreateLogger(string categoryName) 160 | { 161 | return new ResourceLogger(logger); 162 | } 163 | 164 | public void Dispose() 165 | { 166 | } 167 | 168 | private class ResourceLogger(ILogger logger) : ILogger 169 | { 170 | public IDisposable? BeginScope(TState state) where TState : notnull 171 | { 172 | return logger.BeginScope(state); 173 | } 174 | 175 | public bool IsEnabled(LogLevel logLevel) 176 | { 177 | return logger.IsEnabled(logLevel); 178 | } 179 | 180 | public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) 181 | { 182 | logger.Log(logLevel, eventId, state, exception, formatter); 183 | } 184 | } 185 | } 186 | } 187 | -------------------------------------------------------------------------------- /SwaggerUi.Aspire.Hosting/SwaggerUiResource.cs: -------------------------------------------------------------------------------- 1 | using Aspire.Hosting.ApplicationModel; 2 | 3 | public class SwaggerUIResource(string name) : Resource(name) 4 | { 5 | 6 | } 7 | --------------------------------------------------------------------------------