Table of Contents

Class ServiceCollectionExtensions

Namespace
Codebelt.Extensions.Swashbuckle.AspNetCore
Assembly
Codebelt.Extensions.Swashbuckle.AspNetCore.dll

Extension methods for the IServiceCollection interface.

public static class ServiceCollectionExtensions
Inheritance
ServiceCollectionExtensions

Examples

Register comprehensive Swagger documentation for a RESTful API. This extension method is the entry point to add OpenAPI services to the dependency injection container. Call it during application startup in Program.cs to enable both Swagger generation and Swagger UI. The example shows the basic setup with metadata configuration and XML documentation loading:

using System;
using Codebelt.Extensions.Swashbuckle.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace MySwaggerExample;

public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        builder.Services.AddControllers();
        builder.Services.AddEndpointsApiExplorer();

        // Use ServiceCollectionExtensions.AddRestfulSwagger to configure Swagger
        builder.Services.AddRestfulSwagger(options =>
        {
            options.OpenApiInfo.Title = "My API";
            options.OpenApiInfo.Description = "API with comprehensive documentation";
            options.XmlDocumentations.AddByType<Program>();
        });

        var app = builder.Build();

        if (string.Equals(app.Environment.EnvironmentName, "Development", StringComparison.OrdinalIgnoreCase))
        {
            app.UseSwagger();
            app.UseSwaggerUI();
        }

        app.MapControllers();
        app.Run();
    }
}

Methods

AddRestfulSwagger(IServiceCollection, Action<RestfulSwaggerOptions>)

Adds complementary configurations for both SwaggerGenOptions and SwaggerUIOptions - optimized for RESTful APIs.

public static IServiceCollection AddRestfulSwagger(this IServiceCollection services, Action<RestfulSwaggerOptions> setup = null)

Parameters

services IServiceCollection

The IServiceCollection to extend.

setup Action<RestfulSwaggerOptions>

The RestfulSwaggerOptions that may be configured.

Returns

IServiceCollection

A reference to services so that additional calls can be chained.

Examples

Configure Swagger with custom settings through the RestfulSwaggerOptions parameter. This overload accepts a setup action where you can customize API metadata, enable XML documentation loading, and configure advanced SwaggerGen behavior. The example shows how to set document title and version, enable controller XML comments, add API-wide documentation, and use the all-of schema extension for better reference-type composition:

using System;
using Codebelt.Extensions.Swashbuckle.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace MySwaggerExample;

public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        builder.Services.AddControllers();
        builder.Services.AddEndpointsApiExplorer();

        builder.Services.AddRestfulSwagger(options =>
        {
            options.OpenApiInfo.Title = "Custom API";
            options.OpenApiInfo.Description = "Customized API documentation";
            options.IncludeControllerXmlComments = true;
            options.XmlDocumentations.AddByType<Program>();
            options.Settings.UseAllOfToExtendReferenceSchemas();
        });

        var app = builder.Build();

        if (string.Equals(app.Environment.EnvironmentName, "Development", StringComparison.OrdinalIgnoreCase))
        {
            app.UseSwagger();
            app.UseSwaggerUI();
        }

        app.MapControllers();
        app.Run();
    }
}

Remarks

This method expect a call to services.AddRestfulApiVersioning() prior to this.