Each microservice has its own OpenApi document, but in actual development, we prefer to aggregate the OpenApi documents of all microservices together for easy viewing and debugging. Microservices are all developed based on the Aspire framework, so we can use the service discovery function provided by the Aspire framework to automatically aggregate the OpenApi documents of all microservices.
Automatically configure OpenApi documentation using Aspire Service Discovery
public class OpenApiConfigureOptions() : IConfigureOptions<SwaggerUIOptions>
builder.Services.AddTransient<IConfigureOptions<SwaggerUIOptions>, OpenApiConfigureOptions>();
Customize OpenApi document styles
Create the Custom.css file under the Resource/OpenApi folder.
.swagger-ui .topbar-wrapper img {
content: url('https://test.com/logo.svg');
}
.swagger-ui .topbar-wrapper .link::after {
margin-left: 0.5rem;
content: "HelloWorld";
}
Associate CSS files to OpenApi documents
public static class OpenApiExtensions
{
public static IServiceCollection AddOpenApi(this IServiceCollection)
{
services.Configure<SwaggerUIOptions>(options =>
{
options.DocumentTitle = Assembly.GetExecutingAssembly().GetName().Name;
options.InjectStylesheet("/ServiceDefaults/Resources/OpenApi/Custom.css");
});
}
}
public static IApplicationBuilder UseOpenApi(this IApplicationBuilder app)
{
// Configure the HTTP request pipeline.
app.UseSwagger(apiConfigureOptions)
app.Map("/ServiceDefaults", appBuilder => appBuilder.UseStaticFiles(new StaticFileOptions
{
FileProvider = new EmbeddedFileProvider(Assembly.GetExecutingAssembly())
}))
return app;
}