Skip to content

Commit 7e41d6c

Browse files
committed
Modernize codebase and improve maintainability
* Updated Directory.Build.props to support multiple target frameworks (net8.0, net9.0) * Moving to centralized package management * Code Cleanups * NuGet updates
1 parent 85ac61c commit 7e41d6c

20 files changed

+136
-185
lines changed

Directory.Build.props

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<TargetFramework>net8.0</TargetFramework>
3+
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
44
<Nullable>enable</Nullable>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<DebugSymbols>true</DebugSymbols>
@@ -9,14 +9,12 @@
99
<ServerGarbageCollection>true</ServerGarbageCollection>
1010
<EmbedAllSources>true</EmbedAllSources>
1111
</PropertyGroup>
12-
1312
<ItemGroup>
14-
<PackageReference Include="DotNet.ReproducibleBuilds.Isolated" Version="1.2.4" PrivateAssets="All" />
13+
<PackageReference Include="DotNet.ReproducibleBuilds.Isolated" PrivateAssets="All" />
1514
</ItemGroup>
16-
1715
<ItemGroup>
1816
<Using Include="MessagePack" />
1917
<Using Include="System.Buffers" />
20-
<Using Include="Microsoft.AspNetCore.SignalR "/>
18+
<Using Include="Microsoft.AspNetCore.SignalR " />
2119
</ItemGroup>
2220
</Project>

Directory.Packages.props

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<Project>
2+
<PropertyGroup>
3+
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
4+
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
5+
<NoWarn>$(NoWarn);NU1507</NoWarn>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageVersion Include="coverlet.collector" Version="6.0.3" />
9+
<PackageVersion Include="DotNet.ReproducibleBuilds.Isolated" Version="1.2.25" />
10+
<PackageVersion Include="MartinCostello.Logging.XUnit" Version="0.5.1" />
11+
<PackageVersion Include="MessagePack" Version="3.1.1" />
12+
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
13+
<PackageVersion Include="NATS.Net" Version="2.5.5" />
14+
<PackageVersion Include="xunit" Version="2.9.3" />
15+
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.0" />
16+
<PackageVersion Include="Yarp.ReverseProxy" Version="2.2.0" />
17+
</ItemGroup>
18+
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
19+
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.11" />
20+
<PackageVersion Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.11" />
21+
<PackageVersion Include="Microsoft.AspNetCore.SignalR.Specification.Tests" Version="8.0.11" />
22+
<PackageVersion Include="Microsoft.AspNetCore.SignalR.StackExchangeRedis" Version="8.0.11" />
23+
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="8.0.11" />
24+
</ItemGroup>
25+
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
26+
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.0" />
27+
<PackageVersion Include="Microsoft.AspNetCore.SignalR.Client" Version="9.0.0" />
28+
<PackageVersion Include="Microsoft.AspNetCore.SignalR.Specification.Tests" Version="9.0.0" />
29+
<PackageVersion Include="Microsoft.AspNetCore.SignalR.StackExchangeRedis" Version="9.0.0" />
30+
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="9.0.0" />
31+
</ItemGroup>
32+
</Project>

Stebet.SignalR.NATS.sln

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
22
# Visual Studio Version 17
3-
VisualStudioVersion = 17.5.002.0
3+
VisualStudioVersion = 17.5.2.0
44
MinimumVisualStudioVersion = 10.0.40219.1
55
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "perf", "perf", "{2BFA9A1E-AADB-4877-A424-CCCADA82994A}"
66
EndProject
@@ -16,9 +16,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stebet.SignalR.NATS.Tests",
1616
EndProject
1717
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "shared", "shared", "{C0569A8B-1F11-4CE5-B581-CA3F61CF0A00}"
1818
ProjectSection(SolutionItems) = preProject
19+
.editorconfig = .editorconfig
1920
Default.runsettings = Default.runsettings
2021
Directory.Build.props = Directory.Build.props
21-
.editorconfig = .editorconfig
22+
Directory.Packages.props = Directory.Packages.props
2223
README.md = README.md
2324
EndProjectSection
2425
EndProject
@@ -32,10 +33,6 @@ Global
3233
Release|Any CPU = Release|Any CPU
3334
EndGlobalSection
3435
GlobalSection(ProjectConfigurationPlatforms) = postSolution
35-
{709E9B6C-D8DF-4E7C-AB41-EA53D174B75C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
36-
{709E9B6C-D8DF-4E7C-AB41-EA53D174B75C}.Debug|Any CPU.Build.0 = Debug|Any CPU
37-
{709E9B6C-D8DF-4E7C-AB41-EA53D174B75C}.Release|Any CPU.ActiveCfg = Release|Any CPU
38-
{709E9B6C-D8DF-4E7C-AB41-EA53D174B75C}.Release|Any CPU.Build.0 = Release|Any CPU
3936
{C1A9A782-DD39-4801-A37A-52F82BA486F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
4037
{C1A9A782-DD39-4801-A37A-52F82BA486F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
4138
{C1A9A782-DD39-4801-A37A-52F82BA486F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -48,6 +45,10 @@ Global
4845
{AFC39AD4-8C22-476B-BEFA-DADDA922C40C}.Debug|Any CPU.Build.0 = Debug|Any CPU
4946
{AFC39AD4-8C22-476B-BEFA-DADDA922C40C}.Release|Any CPU.ActiveCfg = Release|Any CPU
5047
{AFC39AD4-8C22-476B-BEFA-DADDA922C40C}.Release|Any CPU.Build.0 = Release|Any CPU
48+
{709E9B6C-D8DF-4E7C-AB41-EA53D174B75C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
49+
{709E9B6C-D8DF-4E7C-AB41-EA53D174B75C}.Debug|Any CPU.Build.0 = Debug|Any CPU
50+
{709E9B6C-D8DF-4E7C-AB41-EA53D174B75C}.Release|Any CPU.ActiveCfg = Release|Any CPU
51+
{709E9B6C-D8DF-4E7C-AB41-EA53D174B75C}.Release|Any CPU.Build.0 = Release|Any CPU
5152
{F66735A7-5D9E-4D52-9A00-2CDF10C9A654}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
5253
{F66735A7-5D9E-4D52-9A00-2CDF10C9A654}.Debug|Any CPU.Build.0 = Debug|Any CPU
5354
{F66735A7-5D9E-4D52-9A00-2CDF10C9A654}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -57,10 +58,10 @@ Global
5758
HideSolutionNode = FALSE
5859
EndGlobalSection
5960
GlobalSection(NestedProjects) = preSolution
60-
{709E9B6C-D8DF-4E7C-AB41-EA53D174B75C} = {2BFA9A1E-AADB-4877-A424-CCCADA82994A}
6161
{C1A9A782-DD39-4801-A37A-52F82BA486F1} = {2BFA9A1E-AADB-4877-A424-CCCADA82994A}
6262
{82C6A4A5-AFCE-48DB-882B-ECA9D8C62106} = {35AFA92E-9EC3-4459-9BF1-4572288435B8}
6363
{AFC39AD4-8C22-476B-BEFA-DADDA922C40C} = {9C5BECE8-B964-4B60-864E-F71CA436005E}
64+
{709E9B6C-D8DF-4E7C-AB41-EA53D174B75C} = {2BFA9A1E-AADB-4877-A424-CCCADA82994A}
6465
{F66735A7-5D9E-4D52-9A00-2CDF10C9A654} = {2BFA9A1E-AADB-4877-A424-CCCADA82994A}
6566
EndGlobalSection
6667
GlobalSection(ExtensibilityGlobals) = postSolution

perf/Stebet.SignalR.NATS.LoadTest/Program.cs

+12-12
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@
33
using Microsoft.AspNetCore.SignalR.Client;
44

55

6-
List<HubConnection> _connections = new();
6+
List<HubConnection> _connections = [];
77
string message = $"Hello World";
88
await InitializeAsync();
99
Console.WriteLine("Sending some messages as a warm-up");
10-
await Parallel.ForAsync(0L, 100000, new ParallelOptions { TaskScheduler = TaskScheduler.Default, MaxDegreeOfParallelism = 1024 }, async (_, _) =>
10+
await Parallel.ForAsync(0L, 100000, new ParallelOptions { TaskScheduler = TaskScheduler.Default, MaxDegreeOfParallelism = 1024 }, async (_, cancellationToken) =>
1111
{
1212
HubConnection conn1 = _connections[Random.Shared.Next(0, _connections.Count)];
1313
HubConnection conn2 = _connections[Random.Shared.Next(0, _connections.Count)];
14-
string response = await conn2.InvokeAsync<string>("SendToClient", message, conn1.ConnectionId);
14+
string response = await conn2.InvokeAsync<string>("SendToClient", message, conn1.ConnectionId, cancellationToken);
1515
});
1616

1717
var timer = Stopwatch.StartNew();
18-
using CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
18+
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
1919
int responsesReceived = 0;
2020
Console.WriteLine("Starting invoke test");
2121
try
2222
{
23-
await Parallel.ForAsync(0L, Int64.MaxValue, new ParallelOptions { TaskScheduler = TaskScheduler.Default, MaxDegreeOfParallelism = 1024, CancellationToken = cts.Token }, async (_, _) =>
23+
await Parallel.ForAsync(0L, long.MaxValue, new ParallelOptions { TaskScheduler = TaskScheduler.Default, MaxDegreeOfParallelism = 1024, CancellationToken = cts.Token }, async (_, cancellationToken) =>
2424
{
2525
HubConnection conn1 = _connections[Random.Shared.Next(0, _connections.Count)];
2626
HubConnection conn2 = _connections[Random.Shared.Next(0, _connections.Count)];
27-
string response = await conn2.InvokeAsync<string>("SendToClient", message, conn1.ConnectionId);
27+
string response = await conn2.InvokeAsync<string>("SendToClient", message, conn1.ConnectionId, cancellationToken);
2828
responsesReceived++;
2929
});
3030
}
@@ -34,15 +34,15 @@
3434
}
3535

3636
timer = Stopwatch.StartNew();
37-
using CancellationTokenSource cts2 = new CancellationTokenSource(TimeSpan.FromSeconds(10));
37+
using var cts2 = new CancellationTokenSource(TimeSpan.FromSeconds(10));
3838
responsesReceived = 0;
3939
Console.WriteLine("Starting send all test");
4040
try
4141
{
42-
await Parallel.ForAsync(0L, Int64.MaxValue, new ParallelOptions { TaskScheduler = TaskScheduler.Default, MaxDegreeOfParallelism = 1024, CancellationToken = cts2.Token }, async (_, _) =>
42+
await Parallel.ForAsync(0L, long.MaxValue, new ParallelOptions { TaskScheduler = TaskScheduler.Default, MaxDegreeOfParallelism = 1024, CancellationToken = cts2.Token }, async (_, cancellationToken) =>
4343
{
4444
HubConnection conn1 = _connections[Random.Shared.Next(0, _connections.Count)];
45-
await conn1.InvokeAsync("SendToAllClients", message);
45+
await conn1.InvokeAsync("SendToAllClients", message, cancellationToken);
4646
responsesReceived++;
4747
});
4848
}
@@ -52,15 +52,15 @@
5252
}
5353

5454
timer = Stopwatch.StartNew();
55-
using CancellationTokenSource cts3 = new CancellationTokenSource(TimeSpan.FromSeconds(10));
55+
using var cts3 = new CancellationTokenSource(TimeSpan.FromSeconds(10));
5656
responsesReceived = 0;
5757
Console.WriteLine("Starting send others test");
5858
try
5959
{
60-
await Parallel.ForAsync(0L, Int64.MaxValue, new ParallelOptions { TaskScheduler = TaskScheduler.Default, MaxDegreeOfParallelism = 1024, CancellationToken = cts3.Token }, async (_, _) =>
60+
await Parallel.ForAsync(0L, long.MaxValue, new ParallelOptions { TaskScheduler = TaskScheduler.Default, MaxDegreeOfParallelism = 1024, CancellationToken = cts3.Token }, async (_, cancellationToken) =>
6161
{
6262
HubConnection conn1 = _connections[Random.Shared.Next(0, _connections.Count)];
63-
await conn1.InvokeAsync("SendToOthers", message);
63+
await conn1.InvokeAsync("SendToOthers", message, cancellationToken);
6464
responsesReceived++;
6565
});
6666
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
32
<PropertyGroup>
43
<OutputType>Exe</OutputType>
54
<IsPublishable>false</IsPublishable>
65
</PropertyGroup>
7-
86
<ItemGroup>
9-
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.6" />
7+
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" />
108
</ItemGroup>
11-
129
<ItemGroup>
1310
<ProjectReference Include="..\Stebet.SignalR.NATS.TestServer\Stebet.SignalR.NATS.TestServer.csproj" />
1411
</ItemGroup>
15-
1612
</Project>
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,10 @@
11
namespace Stebet.SignalR.NATS.Tests
22
{
3-
public class EchoHub : Hub
3+
public class EchoHub() : Hub
44
{
5-
private readonly ILogger<EchoHub> _logger;
6-
7-
public EchoHub(ILogger<EchoHub> logger)
8-
{
9-
_logger = logger;
10-
}
11-
public Task<string> Send(string message)
12-
{
13-
return Task.FromResult(message);
14-
}
15-
16-
public Task<string> SendToClient(string message, string connectionId)
17-
{
18-
return Clients.Client(connectionId).InvokeAsync<string>("Hello", message, Context.ConnectionId, CancellationToken.None);
19-
}
20-
21-
public Task SendToAllClients(string message)
22-
{
23-
return Clients.All.SendAsync("Send", message);
24-
}
25-
26-
public Task SendToOthers(string message)
27-
{
28-
return Clients.Others.SendAsync("Send", message);
29-
}
5+
public Task<string> Send(string message) => Task.FromResult(message);
6+
public Task<string> SendToClient(string message, string connectionId) => Clients.Client(connectionId).InvokeAsync<string>("Hello", message, Context.ConnectionId, CancellationToken.None);
7+
public Task SendToAllClients(string message) => Clients.All.SendAsync("Send", message);
8+
public Task SendToOthers(string message) => Clients.Others.SendAsync("Send", message);
309
}
3110
}

perf/Stebet.SignalR.NATS.TestServer/Stebet.SignalR.NATS.TestServer.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
<InternalsVisibleTo Include="Stebet.SignalR.NATS.Tests" />
99
</ItemGroup>
1010
<ItemGroup>
11-
<PackageReference Include="Microsoft.AspNetCore.SignalR.StackExchangeRedis" Version="8.0.6" />
11+
<PackageReference Include="Microsoft.AspNetCore.SignalR.StackExchangeRedis" />
1212
</ItemGroup>
1313
<ItemGroup>
1414
<ProjectReference Include="..\..\src\Stebet.SignalR.NATS\Stebet.SignalR.NATS.csproj" />
1515
</ItemGroup>
16-
</Project>
16+
</Project>

perf/Stebet.SignalR.NATS.Yarp/Stebet.SignalR.NATS.Yarp.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
<ContainerBaseImage>mcr.microsoft.com/dotnet/aspnet:8.0-jammy-chiseled</ContainerBaseImage>
66
</PropertyGroup>
77
<ItemGroup>
8-
<PackageReference Include="Yarp.ReverseProxy" Version="2.1.0" />
8+
<PackageReference Include="Yarp.ReverseProxy" />
99
</ItemGroup>
10-
</Project>
10+
</Project>

src/Stebet.SignalR.NATS/ClientResultsManager.cs

+4-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public ClientResultsManager(IHubProtocolResolver hubProtocolResolver, IOptions<H
2121
var hubProtocols = new List<IHubProtocol>(supportedProtocols.Count);
2222
foreach (string protocolName in supportedProtocols)
2323
{
24-
IHubProtocol? protocol = hubProtocolResolver.GetProtocol(protocolName, (supportedProtocols as IReadOnlyList<string>) ?? supportedProtocols.ToList());
24+
IHubProtocol? protocol = hubProtocolResolver.GetProtocol(protocolName, (supportedProtocols as IReadOnlyList<string>) ?? [.. supportedProtocols]);
2525
if (protocol != null)
2626
{
2727
hubProtocols.Add(protocol);
@@ -129,16 +129,10 @@ public Type GetReturnType(string invocationId)
129129
}
130130

131131
// Unused, here to honor the IInvocationBinder interface but should never be called
132-
public IReadOnlyList<Type> GetParameterTypes(string methodName)
133-
{
134-
throw new NotImplementedException();
135-
}
132+
public IReadOnlyList<Type> GetParameterTypes(string methodName) => throw new NotImplementedException();
136133

137134
// Unused, here to honor the IInvocationBinder interface but should never be called
138-
public Type GetStreamItemType(string streamId)
139-
{
140-
throw new NotImplementedException();
141-
}
135+
public Type GetStreamItemType(string streamId) => throw new NotImplementedException();
142136

143137
// Custom TCS type to avoid the extra allocation that would be introduced if we managed the cancellation separately
144138
// Also makes it easier to keep track of the CancellationTokenRegistration for disposal
@@ -161,12 +155,10 @@ public void RegisterCancellation()
161155
}
162156
}
163157

164-
public new void SetCanceled()
165-
{
158+
public new void SetCanceled() =>
166159
// TODO: RedisHubLifetimeManager will want to notify the other server (if there is one) about the cancellation
167160
// so it can clean up state and potentially forward that info to the connection
168161
clientResultsManager.TryCompleteResult(connectionId, CompletionMessage.WithError(invocationId, "Invocation canceled by the server."));
169-
}
170162

171163
public new void SetResult(T result)
172164
{

src/Stebet.SignalR.NATS/Helpers.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
internal static class Helpers
44
{
5-
internal static readonly ParallelOptions DefaultParallelOptions = new() { TaskScheduler = TaskScheduler.Default };
5+
internal static readonly ParallelOptions s_defaultParallelOptions = new() { TaskScheduler = TaskScheduler.Default };
66
}

src/Stebet.SignalR.NATS/HubConnectionContextExtensions.cs

+2-8
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,10 @@ public static async Task RemoveFromGroupAsync<THub>(this HubConnectionContext co
2929
}
3030
}
3131

32-
public static void SetConnectionHandler<THub>(this HubConnectionContext connection, NatsHubConnectionHandler<THub> handler) where THub : Hub
33-
{
34-
connection.Items["ConnectionHandler"] = handler;
35-
}
32+
public static void SetConnectionHandler<THub>(this HubConnectionContext connection, NatsHubConnectionHandler<THub> handler) where THub : Hub => connection.Items["ConnectionHandler"] = handler;
3633

37-
public static NatsHubConnectionHandler<THub> GetConnectionHandler<THub>(this HubConnectionContext connection) where THub : Hub
38-
{
39-
return (connection.Items.TryGetValue("ConnectionHandler", out object? handler) &&
34+
public static NatsHubConnectionHandler<THub> GetConnectionHandler<THub>(this HubConnectionContext connection) where THub : Hub => (connection.Items.TryGetValue("ConnectionHandler", out object? handler) &&
4035
handler is NatsHubConnectionHandler<THub> connectionHandler)
4136
? connectionHandler
4237
: throw new InvalidOperationException("No ConnectionHandler set on the connection.");
43-
}
4438
}

src/Stebet.SignalR.NATS/IBufferWriterExtensions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ internal static void WriteMessage(this IBufferWriter<byte> bufferWriter, HubMess
4848
{
4949
MessagePackWriter writer = new(bufferWriter);
5050
writer.WriteArrayHeader(protocols.Count);
51-
foreach (var protocol in protocols)
51+
foreach (IHubProtocol protocol in protocols)
5252
{
53-
using NatsBufferWriter<byte> tempBuffer = new NatsBufferWriter<byte>();
53+
using var tempBuffer = new NatsBufferWriter<byte>();
5454
protocol.WriteMessage(message, tempBuffer);
5555
writer.Write(protocol.Name);
5656
writer.WriteBinHeader(tempBuffer.WrittenCount);

src/Stebet.SignalR.NATS/MessagePackReaderExtensions.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,5 @@ internal static SortedSet<string> ReadStringArray(this ref MessagePackReader rea
3939
return strings;
4040
}
4141

42-
internal static SerializedHubMessage ReadSerializedHubMessage(this ref MessagePackReader reader)
43-
{
44-
return new SerializedHubMessage(reader.ReadSerializedMessages());
45-
}
42+
internal static SerializedHubMessage ReadSerializedHubMessage(this ref MessagePackReader reader) => new(reader.ReadSerializedMessages());
4643
}

0 commit comments

Comments
 (0)