Skip to content

Commit

Permalink
Update to .NET 9-rc.1, drop SignalR polymorphism workaround (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
martijn authored Sep 20, 2024
1 parent 71ac934 commit 47b086a
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 67 deletions.
11 changes: 5 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Adjust DOTNET_OS_VERSION as desired
ARG DOTNET_SDK_VERSION=9.0-preview
ARG DOTNET_VERSION=9.0

FROM mcr.microsoft.com/dotnet/sdk:${DOTNET_SDK_VERSION}-alpine AS build
FROM mcr.microsoft.com/dotnet/sdk:${DOTNET_VERSION}-alpine AS build
RUN dotnet workload install wasm-tools
WORKDIR /src

Expand Down Expand Up @@ -38,9 +37,9 @@ RUN set -ex; \
RUN dotnet publish --no-restore -c Release -o /app PointingParty

# final stage/image
FROM mcr.microsoft.com/dotnet/aspnet:${DOTNET_SDK_VERSION}-alpine
ENV ASPNETCORE_URLS http://+:8080
ENV ASPNETCORE_ENVIRONMENT Production
FROM mcr.microsoft.com/dotnet/aspnet:${DOTNET_VERSION}-alpine
ENV ASPNETCORE_URLS=http://+:8080
ENV ASPNETCORE_ENVIRONMENT=Production
EXPOSE 8080
WORKDIR /app
COPY --from=build /app .
Expand Down
10 changes: 5 additions & 5 deletions PointingParty.Client.Tests/PointingParty.Client.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="bunit" Version="2.0.24-preview"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
<PackageReference Include="bunit" Version="2.0.33-preview" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="NSubstitute" Version="5.1.0"/>
<PackageReference Include="NSubstitute.Analyzers.CSharp" Version="1.0.17">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.8.1"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0-pre.30">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion PointingParty.Client/GameContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void PublishEvents()
logger.LogDebug("Publishing {count} events", Game.EventsToPublish.Count);

foreach (var gameEvent in Game!.EventsToPublish)
_hub!.BroadcastGameEvent(gameEvent.GetType().ToString(), gameEvent);
_hub!.BroadcastGameEvent(gameEvent);

Game.EventsToPublish.Clear();
}
Expand Down
6 changes: 3 additions & 3 deletions PointingParty.Client/PointingParty.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Blazor-ApexCharts" Version="3.3.0"/>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.0-preview.4.24267.6"/>
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="9.0.0-preview.4.24267.6"/>
<PackageReference Include="Blazor-ApexCharts" Version="3.4.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.0-rc.1.24452.1" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="9.0.0-rc.1.24452.1" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client.SourceGenerator" Version="7.0.0-preview.7.22376.6"/>
</ItemGroup>

Expand Down
5 changes: 2 additions & 3 deletions PointingParty.Domain/IGameEventHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ namespace PointingParty.Domain;

public interface IGameEventHub
{
// SignalR client doesn't support polymorphic serialization yet, so we send a type + object
public Task BroadcastGameEvent(string type, object gameEvent);
}
public Task BroadcastGameEvent(object gameEvent);
}
59 changes: 12 additions & 47 deletions PointingParty/GameEventHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,33 @@ public GameEventHub(ILogger<GameEventHub> logger)
_logger = logger;
}

public Task BroadcastGameEvent(string type, object gameEvent)
public async Task BroadcastGameEvent(object gameEvent)
{
var jsonEvent = (JsonElement)gameEvent;
var targetType = typeof(IGameEvent).Assembly.GetType(type);

if (targetType is null)
if (gameEvent is not JsonElement jsonEvent)
{
_logger.LogError("Cannot find type {type}", type);
return Task.CompletedTask;
_logger.LogWarning("Invalid game event received.");
return;
}

IGameEvent? typedEvent = null;

// If SignalR .NET client gets support for polymorphic json serialization we can
// change the signature of this method to IGameEvent and get rid of this mess.
switch (targetType.Name)
var typedEvent = jsonEvent.Deserialize<IGameEvent>(JsonSerializerOptions.Web);
if (typedEvent is null)
{
case nameof(GameReset):
typedEvent = DeserializeEvent<GameReset>(jsonEvent);
break;
case nameof(PlayerJoinedGame):
typedEvent = DeserializeEvent<PlayerJoinedGame>(jsonEvent);
break;
case nameof(PlayerLeftGame):
typedEvent = DeserializeEvent<PlayerLeftGame>(jsonEvent);
break;
case nameof(Sync):
typedEvent = DeserializeEvent<Sync>(jsonEvent);
break;
case nameof(VoteCast):
typedEvent = DeserializeEvent<VoteCast>(jsonEvent);
break;
case nameof(VotesShown):
typedEvent = DeserializeEvent<VotesShown>(jsonEvent);
break;
default:
_logger.LogError("Cannot handle type {type}", targetType.Name);
break;
_logger.LogWarning("Failed to deserialize game event.");
return;
}

if (typedEvent is PlayerJoinedGame joinedGame)
{
// Save player details, so we can broadcast a leave event when client disconnects
Context.Items[PlayerNameItem] = joinedGame.PlayerName;
Context.Items[GameIdItem] = joinedGame.GameId;

// Add client to SignalR group so it receives events for the game it joined
Groups.AddToGroupAsync(Context.ConnectionId, joinedGame.GameId);
await Groups.AddToGroupAsync(Context.ConnectionId, joinedGame.GameId);
}

if (typedEvent is not null)
{
_logger.LogInformation("{gameId}: processing event {event}", typedEvent.GameId, typedEvent);
return Clients.GroupExcept(typedEvent.GameId, Context.ConnectionId).ReceiveGameEvent(typedEvent);
}

return Task.CompletedTask;
_logger.LogInformation("{gameId}: processing event {event}", typedEvent.GameId, typedEvent);
await Clients.GroupExcept(typedEvent.GameId, Context.ConnectionId).ReceiveGameEvent(typedEvent);
}

public override async Task OnConnectedAsync()
Expand All @@ -97,9 +67,4 @@ public override async Task OnDisconnectedAsync(Exception? exception)

await base.OnDisconnectedAsync(exception);
}

private static T? DeserializeEvent<T>(JsonElement jsonEvent) where T : class, IGameEvent
{
return jsonEvent.Deserialize<T>(JsonOptions);
}
}
2 changes: 1 addition & 1 deletion PointingParty/PointingParty.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<ItemGroup>
<ProjectReference Include="..\PointingParty.Client\PointingParty.Client.csproj"/>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="9.0.0-preview.4.24267.6"/>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="9.0.0-rc.1.24452.1" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion PointingParty/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@

app.MapHub<GameEventHub>("/events");

app.Run();
app.Run();

0 comments on commit 47b086a

Please sign in to comment.