Skip to content
Dorin Mocan edited this page Jun 15, 2023 · 12 revisions

Welcome to the SignalRSwaggerGen wiki!

Nuget link: https://www.nuget.org/packages/SignalRSwaggerGen

This package can be used to generate Swagger documentation for SignalR hubs.

Usage

Let's look at the most simple example of how we can use this package to enable Swagger documentation for our SignalR hubs.

First we decorate our hubs with attributes from SignalRSwaggerGen.Attributes namespace:

Simple hub

[SignalRHub]
public class SomeHub : Hub
{
    public async Task Send(int arg1, string arg2, [SignalRHidden] CancellationToken ct = default)
    {
        await Clients.All.SendAsync("Receive", arg1, arg2, ct);
    }
}

Strongly typed hub

[SignalRHub]
public interface ISomeStronglyTypedHub
{
    Task Receive(int arg1, string arg2);
}

public class SomeStronglyTypedHub : Hub<ISomeStronglyTypedHub>
{
    public async Task Send(int arg1, string arg2)
    {
        await Clients.All.Receive(arg1, arg2);
    }
}

Second and last, we add SignalRSwaggerGen to Swagger generator:

services.AddSwaggerGen(options =>
{
    options.SwaggerDoc("v1", new OpenApiInfo { Title = "Some API v1", Version = "v1" });
    // some other configs
    options.AddSignalRSwaggerGen();
});

That's it!

For more complex scenarios, please check the XML documentation of the API.

For Swagger CLI users

Please make sure you explicitly specify the assemblies to be scanned by SignalRSwaggerGen, because by default only the entry assembly will be scanned, and in this case the entry assembly will be the one of the Swagger CLI tool, which obviously does not contain any SignalR hubs. You can specify the assemblies like this:

options.AddSignalRSwaggerGen(ssgOptions => ssgOptions.ScanAssemblies(...));

Hope this tutorial and package helps you. Good luck! :)

Clone this wiki locally