Skip to content

Commit 2383689

Browse files
Add Azure SignalR to SignalR transport
1 parent 580737e commit 2383689

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

docs/guide/messaging/transports/signalr.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,26 @@ builder.UseWolverine(opts =>
4646
// for the WolverineHub
4747
o.ClientTimeoutInterval = 10.Seconds();
4848
});
49+
50+
// Instead of self-hosting, it's also possible to
51+
// use Azure SignalR. Only one of the two SignalR
52+
// registrations are necessary. Both register the
53+
// required services in DI
54+
opts.UseAzureSignalR(hub =>
55+
{
56+
// Optionally configure the SignalR HubOptions
57+
// for the WolverineHub
58+
hub.ClientTimeoutInterval = 10.Seconds();
59+
}, service =>
60+
{
61+
// And optionally configure the Azure SignalR
62+
// options for the connection.
63+
service.ApplicationName = "wolverine";
64+
65+
// You probably want one of these from your
66+
// configuration somehow
67+
service.ConnectionString = "Endpoint=https://myresource.service.signalr.net;AccessKey=...;Version=1.0;";
68+
});
4969

5070
// Using explicit routing to send specific
5171
// messages to SignalR
@@ -59,7 +79,7 @@ builder.UseWolverine(opts =>
5979
});
6080
});
6181
```
62-
<sup><a href='https://github.com/JasperFx/wolverine/blob/main/src/Samples/WolverineChat/Program.cs#L12-L39' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_configuring_signalr_on_server_side' title='Start of snippet'>anchor</a></sup>
82+
<sup><a href='https://github.com/JasperFx/wolverine/blob/main/src/Samples/WolverineChat/Program.cs#L12-L59' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_configuring_signalr_on_server_side' title='Start of snippet'>anchor</a></sup>
6383
<!-- endSnippet -->
6484

6585
That handles the Wolverine configuration and the SignalR service registrations, but you will also need to map
@@ -84,7 +104,7 @@ app.MapWolverineSignalRHub("/api/messages");
84104

85105
return await app.RunJasperFxCommands(args);
86106
```
87-
<sup><a href='https://github.com/JasperFx/wolverine/blob/main/src/Samples/WolverineChat/Program.cs#L43-L61' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_map_wolverine_signalrhub' title='Start of snippet'>anchor</a></sup>
107+
<sup><a href='https://github.com/JasperFx/wolverine/blob/main/src/Samples/WolverineChat/Program.cs#L63-L81' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_map_wolverine_signalrhub' title='Start of snippet'>anchor</a></sup>
88108
<!-- endSnippet -->
89109

90110
## Messages and Serialization

src/Samples/WolverineChat/Program.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,26 @@
2323
// for the WolverineHub
2424
o.ClientTimeoutInterval = 10.Seconds();
2525
});
26+
27+
// Instead of self-hosting, it's also possible to
28+
// use Azure SignalR. Only one of the two SignalR
29+
// registrations are necessary. Both register the
30+
// required services in DI
31+
opts.UseAzureSignalR(hub =>
32+
{
33+
// Optionally configure the SignalR HubOptions
34+
// for the WolverineHub
35+
hub.ClientTimeoutInterval = 10.Seconds();
36+
}, service =>
37+
{
38+
// And optionally configure the Azure SignalR
39+
// options for the connection.
40+
service.ApplicationName = "wolverine";
41+
42+
// You probably want one of these from your
43+
// configuration somehow
44+
service.ConnectionString = "Endpoint=https://myresource.service.signalr.net;AccessKey=...;Version=1.0;";
45+
});
2646

2747
// Using explicit routing to send specific
2848
// messages to SignalR

src/Transports/SignalR/Wolverine.SignalR/SignalRWolverineExtensions.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,28 @@ public static SignalRListenerConfiguration UseSignalR(this WolverineOptions opti
6969
return new SignalRListenerConfiguration(transport);
7070
}
7171

72+
/// <summary>
73+
/// Adds the WolverineHub to this application for Azure SignalR message processing
74+
/// </summary>
75+
/// <param name="options"></param>
76+
/// <param name="configureHub">Optionally configure the SignalR HubOptions for Wolverine</param>
77+
/// <param name="configureSignalR">Optionally configure the Azure SignalR options for Wolverine</param>
78+
/// <returns></returns>
79+
public static SignalRListenerConfiguration UseAzureSignalR(this WolverineOptions options, Action<HubOptions>? configureHub = null, Action<Microsoft.Azure.SignalR.ServiceOptions>? configureSignalR = null)
80+
{
81+
configureHub ??= _ => { };
82+
configureSignalR ??= _ => { };
83+
84+
options.Services.AddSignalR(configureHub).AddAzureSignalR(configureSignalR);
85+
86+
var transport = options.SignalRTransport();
87+
88+
options.Services.AddSingleton<SignalRTransport>(s =>
89+
s.GetRequiredService<IWolverineRuntime>().Options.Transports.GetOrCreate<SignalRTransport>());
90+
91+
return new SignalRListenerConfiguration(transport);
92+
}
93+
7294
/// <summary>
7395
/// Syntactical shortcut to register the WolverineHub SignalR Hub for sending
7496
/// messages to this server. Equivalent to MapHub<WolverineHub>(route).

src/Transports/SignalR/Wolverine.SignalR/Wolverine.SignalR.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717

1818
<ItemGroup>
1919
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="9.0.8" />
20+
<PackageReference Include="Microsoft.Azure.SignalR" Version="1.32.0" />
2021
</ItemGroup>
2122
</Project>

0 commit comments

Comments
 (0)