Skip to content

Commit 4445455

Browse files
committed
more (azure) signal Rsetup
1 parent 715ac49 commit 4445455

File tree

8 files changed

+74
-4
lines changed

8 files changed

+74
-4
lines changed

src/web/Client/Extensions/WebAssemblyHostBuilderExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public static IServiceCollection AddRefitClient<TClient>(
1111
RefitSettings? settings = null)
1212
where TClient : class
1313
{
14-
services.AddRefitClient<TClient>(settings).ConfigureHttpClient(client =>
14+
services.AddRefitClient<TClient>(settings)
15+
.ConfigureHttpClient(client =>
1516
{
1617
client.BaseAddress = baseAddress;
1718
// This prevents the api from redirecting to Account/Login on 401
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
@page "/"
2+
@using Microsoft.AspNetCore.SignalR.Client
3+
@inject NavigationManager Navigation
4+
@implements IAsyncDisposable
5+
6+
@code {
7+
private HubConnection? _hubConnection;
8+
9+
protected override async Task OnInitializedAsync()
10+
{
11+
_hubConnection = new HubConnectionBuilder()
12+
.WithUrl(Navigation.ToAbsoluteUri("/chathub"))
13+
.WithAutomaticReconnect()
14+
.Build();
15+
16+
_hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
17+
{
18+
var encodedMsg = $"{user}: {message}";
19+
//messages.Add(encodedMsg);
20+
StateHasChanged();
21+
});
22+
23+
await _hubConnection.StartAsync();
24+
}
25+
26+
private async Task Send()
27+
{
28+
if (_hubConnection is not null)
29+
{
30+
await _hubConnection.SendAsync("SendMessage");
31+
}
32+
}
33+
34+
public bool IsConnected => _hubConnection?.State == HubConnectionState.Connected;
35+
36+
public async ValueTask DisposeAsync()
37+
{
38+
if (_hubConnection is not null)
39+
{
40+
await _hubConnection.DisposeAsync();
41+
}
42+
}
43+
}

src/web/Server/Jordnaer.Server.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
44
<TargetFramework>net7.0</TargetFramework>
@@ -9,8 +9,10 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Azure.Storage.Blobs" Version="12.17.0" />
12+
1213
<PackageReference Include="MassTransit.AspNetCore" Version="7.3.1" />
1314
<PackageReference Include="MassTransit.Azure.ServiceBus.Core" Version="8.0.16" />
15+
1416
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
1517

1618
<PackageReference Include="Microsoft.AspNetCore.Authentication.Facebook" Version="7.0.9" />
@@ -52,6 +54,8 @@
5254
<PrivateAssets>all</PrivateAssets>
5355
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
5456
</PackageReference>
57+
58+
<PackageReference Include="Microsoft.Azure.SignalR" Version="1.21.3" />
5559
</ItemGroup>
5660

5761
<ItemGroup>

src/web/Server/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363

6464
builder.AddMassTransit();
6565

66-
builder.Services.AddSignalR();
66+
builder.Services.AddSignalR().AddAzureSignalR(options =>
67+
options.ServerStickyMode = ServerStickyMode.Required);
6768
if (!builder.Environment.IsDevelopment())
6869
{
6970
builder.Services.AddResponseCompression(options =>
@@ -126,6 +127,8 @@
126127

127128
app.MapHealthChecks("/health").AllowAnonymous().RequireHealthCheckRateLimit();
128129

130+
app.MapHub<ChatHub>("/chathub");
131+
129132
app.MapFallbackToFile("index.html");
130133

131134
app.Run();

src/web/Server/Properties/launchSettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"applicationUrl": "https://localhost:7116;http://localhost:5235",
99
"environmentVariables": {
1010
"ASPNETCORE_ENVIRONMENT": "Development",
11-
"ConnectionStrings_JordnaerDbContext": "Server=localhost,1433;Database=jordnaer;User ID=sa;Password=6efe173b-3e33-4d6c-8f50-3e5f7cadd54c;Persist Security Info=False;TrustServerCertificate=true;"
11+
"ConnectionStrings_JordnaerDbContext": "Server=localhost,1433;Database=jordnaer;User ID=sa;Password=6efe173b-3e33-4d6c-8f50-3e5f7cadd54c;Persist Security Info=False;TrustServerCertificate=true;",
12+
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.Azure.SignalR"
1213
}
1314
}
1415
}

src/web/Server/Properties/serviceDependencies.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
"type": "mssql",
1313
"connectionId": "ConnectionStrings:JordnaerDbContext",
1414
"dynamicId": null
15+
},
16+
"signalr1": {
17+
"type": "signalr",
18+
"connectionId": "ConnectionStrings:AzureSignalR",
19+
"dynamicId": null
1520
}
1621
}
1722
}

src/web/Server/Properties/serviceDependencies.local.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
"type": "mssql.container",
2222
"connectionId": "ConnectionStrings:JordnaerDbContext",
2323
"dynamicId": null
24+
},
25+
"signalr1": {
26+
"serviceConnectorResourceId": "/subscriptions/[parameters('subscriptionId')]/resourceGroups/[parameters('resourceGroupName')]/providers/Microsoft.ServiceLinker/locations/swedencentral/connectors/AzureSignalRConnectionString_4B4C9F2617",
27+
"secretStore": null,
28+
"resourceId": "/subscriptions/[parameters('subscriptionId')]/resourceGroups/[parameters('resourceGroupName')]/providers/Microsoft.SignalRService/SignalR/jordnaer",
29+
"type": "signalr.azure",
30+
"connectionId": "ConnectionStrings:AzureSignalR",
31+
"dynamicId": null
2432
}
2533
}
2634
}

src/web/Server/appsettings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,10 @@
1515
},
1616
"DataForsyningen": {
1717
"BaseUrl": "https://api.dataforsyningen.dk"
18+
},
19+
"Azure": {
20+
"SignalR": {
21+
"Enabled": "true"
22+
}
1823
}
1924
}

0 commit comments

Comments
 (0)