Skip to content

Commit 393243f

Browse files
committed
Draft - WIP.
1 parent 3b1e241 commit 393243f

File tree

4 files changed

+113
-17
lines changed

4 files changed

+113
-17
lines changed

docs/real-time/azure-signalr-scenario.md

Lines changed: 75 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
---
22
title: .NET Aspire Azure SignalR Service integration
33
description: Learn how to integrate Azure SignalR Service with .NET Aspire.
4-
ms.date: 03/05/2025
4+
ms.date: 03/06/2025
55
---
66

77
# .NET Aspire Azure SignalR Service integration
88

9+
[!INCLUDE [includes-hosting](../includes/includes-hosting.md)]
10+
911
[Azure SignalR Service](/azure/azure-signalr/signalr-overview) is a fully managed real-time messaging service that simplifies adding real-time web functionality to your applications. The .NET Aspire Azure SignalR Service integration enables you to easily provision, configure, and connect your .NET applications to Azure SignalR Service instances.
1012

1113
This article describes how to integrate Azure SignalR Service into your .NET Aspire applications, covering both hosting and client integration.
@@ -17,7 +19,7 @@ The .NET Aspire Azure SignalR Service hosting integration models Azure SignalR r
1719
- <xref:Aspire.Hosting.Azure.AzureSignalRResource>: Represents an Azure SignalR Service resource, including connection information to the underlying Azure resource.
1820
- <xref:Aspire.Hosting.Azure.AzureSignalREmulatorResource>: Represents an emulator for Azure SignalR Service, allowing local development and testing without requiring an Azure subscription.
1921

20-
To access the hosting APIs, install the [📦 Aspire.Hosting.Azure.SignalR](https://www.nuget.org/packages/Aspire.Hosting.Azure.SignalR) NuGet package in your [app host](../fundamentals/app-host-overview.md#app-host-project) project:
22+
To access the hosting types and APIs for expressing these resources in the distributed application builder, install the [📦 Aspire.Hosting.Azure.SignalR](https://www.nuget.org/packages/Aspire.Hosting.Azure.SignalR) NuGet package in your [app host](../fundamentals/app-host-overview.md#app-host-project) project:
2123

2224
### [.NET CLI](#tab/dotnet-cli)
2325

@@ -38,7 +40,7 @@ For more information, see [dotnet add package](/dotnet/core/tools/dotnet-add-pac
3840

3941
### Add an Azure SignalR Service resource
4042

41-
To add an Azure SignalR Service resource to your app host project, call the `AddAzureSignalR` method:
43+
To add an Azure SignalR Service resource to your app host project, call the <xref:Aspire.Hosting.AzureSignalRExtensions.AddAzureSignalR*> method:
4244

4345
```csharp
4446
var builder = DistributedApplication.CreateBuilder(args);
@@ -128,7 +130,7 @@ var signalR = builder.AddAzureSignalR("signalr")
128130
.ConfigureInfrastructure(infra =>
129131
{
130132
var resources = infra.GetProvisionableResources();
131-
var signalRResource = resources.OfType<Azure.Provisioning.SignalR.SignalRService>().Single();
133+
var signalRResource = resources.OfType<SignalRService>().Single();
132134

133135
signalRResource.Sku.Name = "Premium_P1";
134136
signalRResource.Sku.Capacity = 2;
@@ -139,18 +141,41 @@ var signalR = builder.AddAzureSignalR("signalr")
139141

140142
### Connect to an existing Azure SignalR Service
141143

142-
To connect to an existing Azure SignalR Service, use the `AddConnectionString` method:
144+
You might have an existing Azure SignalR Service that you want to connect to. You can chain a call to annotate that your <xref:Aspire.Hosting.ApplicationModel.AzureSignalRResource> is an existing resource:
145+
146+
```csharp
147+
var builder = DistributedApplication.CreateBuilder(args);
148+
149+
var existingSignalRName = builder.AddParameter("existingSignalRName");
150+
var existingSignalRResourceGroup = builder.AddParameter("existingSignalRResourceGroup");
151+
152+
var signalr = builder.AddAzureSignalR("signalr")
153+
.AsExisting(existingSignalRName, existingSignalRResourceGroup);
154+
155+
builder.AddProject<Projects.ExampleProject>()
156+
.WithReference(signalr);
157+
158+
// After adding all resources, run the app...
159+
```
160+
161+
For more information on treating Azure SignalR resources as existing resources, see [Use existing Azure resources](../azure/integrations-overview.md#use-existing-azure-resources).
162+
163+
Alternatively, instead of representing an Azure SignalR resource, you can add a connection string to the app host. Which is a weakly-typed approach that's based solely on a `string` value. To add a connection to an existing Azure SignalR Service, call the <xref:Aspire.Hosting.ParameterResourceBuilderExtensions.AddConnectionString%2A> method:
143164

144165
```csharp
145166
var builder = DistributedApplication.CreateBuilder(args);
146167

147-
var signalR = builder.AddConnectionString("signalr");
168+
var signalr = builder.ExecutionContext.IsPublishMode
169+
? builder.AddAzureSignalR("signalr")
170+
: builder.AddConnectionString("signalr");
148171

149172
builder.AddProject<Projects.ApiService>("apiService")
150-
.WithReference(signalR);
173+
.WithReference(signalr);
151174
```
152175

153-
The connection string is configured in the app host's configuration, typically under [User Secrets](/aspnet/core/security/app-secrets):
176+
[!INCLUDE [connection-strings-alert](../includes/connection-strings-alert.md)]
177+
178+
The connection string is configured in the app host's configuration, typically under [User Secrets](/aspnet/core/security/app-secrets), under the `ConnectionStrings` section:
154179

155180
```json
156181
{
@@ -160,13 +185,34 @@ The connection string is configured in the app host's configuration, typically u
160185
}
161186
```
162187

163-
## Client integration
188+
For more information, see [Add existing Azure resources with connection strings](../azure/integrations-overview.md#add-existing-azure-resources-with-connection-strings).
189+
190+
### Add an Azure SignalR Service emulator resource
191+
192+
The Azure SignalR Service emulator is a local development and testing tool that emulates the behavior of Azure SignalR Service. This emulator only supports [Serverless_ mode](/azure/azure-signalr/concept-service-mode#serverless-mode), which requires a specific configuration when using the emulator.
193+
194+
To use the emulator, chain a call to the <xref:Aspire.Hosting.AzureSignalRExtensions.RunAsEmulator(Aspire.Hosting.ApplicationModel.IResourceBuilder{Aspire.Hosting.ApplicationModel.AzureSignalRResource},System.Action{Aspire.Hosting.ApplicationModel.IResourceBuilder{Aspire.Hosting.Azure.AzureSignalREmulatorResource}})> method:
195+
196+
```csharp
197+
using Aspire.Hosting.Azure;
198+
199+
var builder = DistributedApplication.CreateBuilder(args);
200+
201+
var signalR = builder.AddAzureSignalR("signalr", AzureSignalRServiceMode.Serverless)
202+
.RunAsEmulator();
164203

165-
The client integration enables your ASP.NET Core application to use Azure SignalR Service for real-time messaging.
204+
builder.AddProject<Projects.ApiService>("apiService")
205+
.WithReference(signalR)
206+
.WaitFor(signalR);
166207

167-
### Install the NuGet package
208+
// After adding all resources, run the app...
209+
```
210+
211+
In the preceding example, the `RunAsEmulator` method configures the Azure SignalR Service resource to run as an emulator. The emulator is started when the app host is run, and it will be stopped when the app host is stopped.
168212

169-
Install the [📦 Microsoft.Azure.SignalR](https://www.nuget.org/packages/Microsoft.Azure.SignalR) NuGet package in the project hosting your SignalR hub:
213+
## Client integration
214+
215+
There isn't an official .NET Aspire Azure SignalR client integration. However, there is limited support for similar experiences. There are two specific packages available for [.NET from the Azure team](https://github.com/Azure/azure-signalr) that enable scenarios such as managing the client connection to Azure SignalR Service, and hooking up to the Azure SignalR Service resource. To get started, install the [📦 Microsoft.Azure.SignalR](https://www.nuget.org/packages/Microsoft.Azure.SignalR) NuGet package in the project hosting your SignalR hub.
170216

171217
### [.NET CLI](#tab/dotnet-cli)
172218

@@ -183,6 +229,23 @@ dotnet add package Microsoft.Azure.SignalR
183229

184230
---
185231

232+
If you're app host is using the Azure SignalR emulator, you'll also need to install the [📦 Microsoft.Azure.SignalR.Management](https://www.nuget.org/packages/Microsoft.Azure.SignalR.Management) NuGet package.
233+
234+
### [.NET CLI](#tab/dotnet-cli)
235+
236+
```dotnetcli
237+
dotnet add package Microsoft.Azure.SignalR.Management
238+
```
239+
240+
### [PackageReference](#tab/package-reference)
241+
242+
```xml
243+
<PackageReference Include="Microsoft.Azure.SignalR.Management"
244+
Version="*" />
245+
```
246+
247+
---
248+
186249
### Configure the SignalR client
187250

188251
In your SignalR hub host project, configure Azure SignalR Service using the `AddNamedAzureSignalR` extension method chained to `AddSignalR`:
Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,48 @@
1-
var builder = WebApplication.CreateBuilder(args);
1+
using Microsoft.Azure.SignalR.Management;
2+
3+
var builder = WebApplication.CreateBuilder(args);
24

35
builder.AddServiceDefaults();
46

57
builder.Services.AddProblemDetails();
68

7-
builder.Services.AddSignalR()
8-
.AddNamedAzureSignalR("signalr");
9+
builder.Services.AddSignalR();
10+
//.AddNamedAzureSignalR("signalr");
11+
12+
builder.Services.AddSingleton(sp =>
13+
{
14+
return new ServiceManagerBuilder()
15+
.WithOptions(options =>
16+
{
17+
options.ConnectionString = builder.Configuration.GetConnectionString("signalr");
18+
})
19+
.WithLoggerFactory(sp.GetRequiredService<ILoggerFactory>())
20+
.BuildServiceManager();
21+
});
922

1023
var app = builder.Build();
1124

1225
app.UseExceptionHandler();
1326

1427
app.MapHub<ChatHub>(HubEndpoints.ChatHub);
1528

29+
app.MapPost("/negotiate", async (ServiceManager serviceManager, string? userId) =>
30+
{
31+
var healthy = await serviceManager.IsServiceHealthy(CancellationToken.None);
32+
if (healthy is false)
33+
{
34+
return Results.Problem("SignalR service is not healthy.");
35+
}
36+
37+
var hubContext = await serviceManager.CreateHubContextAsync(HubEndpoints.ChatHub, default);
38+
var negotiateResponse = await hubContext.NegotiateAsync(new NegotiationOptions
39+
{
40+
UserId = userId ?? "user1"
41+
});
42+
43+
return Results.Ok(negotiateResponse);
44+
});
45+
1646
app.MapDefaultEndpoints();
1747

1848
app.Run();

docs/real-time/snippets/signalr/SignalR.ApiService/SignalR.ApiService.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.Azure.SignalR" Version="1.30.2" />
12+
<PackageReference Include="Microsoft.Azure.SignalR.Management" Version="1.30.2" />
1213
</ItemGroup>
1314

1415
<ItemGroup>

docs/real-time/snippets/signalr/SignalR.AppHost/Program.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
var builder = DistributedApplication.CreateBuilder(args);
1+
using Aspire.Hosting.Azure;
22

3-
var signalR = builder.AddAzureSignalR("signalr")
3+
var builder = DistributedApplication.CreateBuilder(args);
4+
5+
var signalR = builder.AddAzureSignalR("signalr", AzureSignalRServiceMode.Serverless)
46
.RunAsEmulator();
57

68
var apiService = builder.AddProject<Projects.SignalR_ApiService>("apiservice")

0 commit comments

Comments
 (0)