Skip to content

Commit 9f3ec76

Browse files
Merge pull request #130 from JRWinter1/bugfix/hub-name
Adds HubUtility to resolve hub names.
2 parents db057b6 + ea8c988 commit 9f3ec76

File tree

5 files changed

+54
-12
lines changed

5 files changed

+54
-12
lines changed

Diff for: src/SignalR.Orleans/Core/GrainExtensions.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Threading.Tasks;
2+
using Microsoft.AspNetCore.SignalR;
23
using Microsoft.AspNetCore.SignalR.Protocol;
34
using Orleans.Concurrency;
45
using SignalR.Orleans.Core;
@@ -37,7 +38,7 @@ public static void SendOneWay(this IHubMessageInvoker grain, string methodName,
3738

3839
public static class GrainFactoryExtensions
3940
{
40-
public static HubContext<THub> GetHub<THub>(this IGrainFactory grainFactory)
41+
public static HubContext<THub> GetHub<THub>(this IGrainFactory grainFactory) where THub : Hub
4142
{
4243
return new HubContext<THub>(grainFactory);
4344
}

Diff for: src/SignalR.Orleans/Core/HubContext.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
using Orleans;
1+
using Microsoft.AspNetCore.SignalR;
2+
using Orleans;
23
using SignalR.Orleans.Users;
34
using SignalR.Orleans.Groups;
45
using SignalR.Orleans.Clients;
56

67
namespace SignalR.Orleans.Core
78
{
8-
public class HubContext<THub>
9+
public class HubContext<THub> where THub : Hub
910
{
1011
private readonly IGrainFactory _grainFactory;
1112
private readonly string _hubName;
1213

1314
public HubContext(IGrainFactory grainFactory)
1415
{
1516
_grainFactory = grainFactory;
16-
var hubType = typeof(THub);
17-
_hubName = hubType.IsInterface && hubType.Name.StartsWith("I")
18-
? hubType.Name[1..]
19-
: hubType.Name;
17+
_hubName = HubUtility.GetHubName<THub>();
2018
}
2119

2220
public IClientGrain Client(string connectionId) => _grainFactory.GetClientGrain(_hubName, connectionId);

Diff for: src/SignalR.Orleans/HubUtility.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Microsoft.AspNetCore.SignalR;
2+
3+
namespace SignalR.Orleans;
4+
5+
internal static class HubUtility
6+
{
7+
internal static string GetHubName<THub>() where THub : Hub
8+
{
9+
return typeof(THub).Name;
10+
}
11+
}

Diff for: src/SignalR.Orleans/OrleansHubLifetimeManager.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ public OrleansHubLifetimeManager(
3434
IClusterClientProvider clusterClientProvider
3535
)
3636
{
37-
var hubType = typeof(THub).BaseType?.GenericTypeArguments.FirstOrDefault() ?? typeof(THub);
38-
var name = hubType.Name.AsSpan();
39-
_hubName = hubType.IsInterface && name[0] == 'I'
40-
? new string(name.Slice(1))
41-
: hubType.Name;
37+
_hubName = HubUtility.GetHubName<THub>();
4238
_serverId = Guid.NewGuid();
4339
_logger = logger;
4440
_clusterClientProvider = clusterClientProvider;

Diff for: test/SignalR.Orleans.Tests/HubUtilityTests.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Microsoft.AspNetCore.SignalR;
2+
using Xunit;
3+
4+
namespace SignalR.Orleans.Tests
5+
{
6+
public class HubUtilityTests
7+
{
8+
[Fact]
9+
public void GivenAWeaklyTypedHub_WhenGetHubName_ThenReturnsHubName()
10+
{
11+
var result = HubUtility.GetHubName<MyWeaklyTypedHub>();
12+
13+
Assert.Equal("MyWeaklyTypedHub", result);
14+
}
15+
16+
[Fact]
17+
public void GivenAStronglyTypedHub_WhenGetHubName_ThenReturnsHubName()
18+
{
19+
var result = HubUtility.GetHubName<MyStronglyTypedHub>();
20+
21+
Assert.Equal("MyStronglyTypedHub", result);
22+
}
23+
24+
private class MyWeaklyTypedHub : Hub
25+
{
26+
}
27+
28+
private interface IHubClient
29+
{
30+
}
31+
32+
private class MyStronglyTypedHub : Hub<IHubClient>
33+
{
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)