Table of Contents

ServiceCollectionExtensions Class

Definition

Namespace
Codebelt.Extensions.Swashbuckle.AspNetCore
Assemblies
Codebelt.Extensions.Swashbuckle.AspNetCore.dll
Source
src/Codebelt.Extensions.Swashbuckle.AspNetCore/ServiceCollectionExtensions.cs

Extension methods for the IServiceCollection interface.

public static class ServiceCollectionExtensions
Inheritance
object
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

Name Description
AddRestfulSwagger(IServiceCollection, Action<RestfulSwaggerOptions>)

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