Skip to content

Commit 69e56fd

Browse files
committed
invocatorcontext property, simple
1 parent b4eb357 commit 69e56fd

File tree

7 files changed

+22
-21
lines changed

7 files changed

+22
-21
lines changed

src/NetCoreStack.WebSockets.ProxyClient/ApplicationBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static IApplicationBuilder UseProxyWebSockets(this IApplicationBuilder ap
2424
IList<IWebSocketConnector> connectors = InvocatorFactory.GetConnectors(app.ApplicationServices);
2525
foreach (var connector in connectors)
2626
{
27-
InvocatorsHelper.EnsureHostPair(connector.GetInvocatorContext());
27+
InvocatorsHelper.EnsureHostPair(connector.InvocatorContext);
2828
appLifeTime.ApplicationStopping.Register(OnShutdown, connector);
2929
Task.Factory.StartNew(async () => await connector.ConnectAsync(cancellationTokenSource), TaskCreationOptions.LongRunning);
3030
}

src/NetCoreStack.WebSockets.ProxyClient/ClientWebSocketConnector.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,27 @@ public ClientWebSocketConnector(IServiceProvider serviceProvider,
4545
_loggerFactory = loggerFactory;
4646
}
4747

48-
public abstract InvocatorContext GetInvocatorContext();
48+
public abstract InvocatorContext InvocatorContext { get; }
4949

5050
private async Task<WebSocketReceiver> TryConnectAsync(CancellationTokenSource cancellationTokenSource = null)
5151
{
52-
var invocatorContext = GetInvocatorContext();
5352
_webSocket = new ClientWebSocket();
54-
_webSocket.Options.SetRequestHeader(SocketsConstants.ConnectorName, invocatorContext.ConnectorName);
53+
_webSocket.Options.SetRequestHeader(SocketsConstants.ConnectorName, InvocatorContext.ConnectorName);
5554
try
5655
{
5756
CancellationToken token = cancellationTokenSource != null ? cancellationTokenSource.Token : CancellationToken.None;
58-
await _webSocket.ConnectAsync(invocatorContext.Uri, token);
57+
await _webSocket.ConnectAsync(InvocatorContext.Uri, token);
5958
}
6059
catch (Exception ex)
6160
{
62-
ProxyLogHelper.Log(_loggerFactory, invocatorContext, "Error", ex);
61+
ProxyLogHelper.Log(_loggerFactory, InvocatorContext, "Error", ex);
6362
return null;
6463
}
6564

6665
var receiverContext = new WebSocketReceiverContext
6766
{
6867
Compressor = _compressor,
69-
InvocatorContext = invocatorContext,
68+
InvocatorContext = InvocatorContext,
7069
LoggerFactory = _loggerFactory,
7170
WebSocket = _webSocket
7271
};

src/NetCoreStack.WebSockets.ProxyClient/ClientWebSocketConnectorOfT.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ public class ClientWebSocketConnectorOfT<TInvocator> : ClientWebSocketConnector,
99
{
1010
private readonly IClientInvocatorContextFactory<TInvocator> _invocatorContextFactory;
1111

12-
public ProxyOptions<TInvocator> Options { get; }
13-
14-
public override InvocatorContext GetInvocatorContext()
15-
{
16-
return _invocatorContextFactory.CreateInvocatorContext();
17-
}
12+
public override InvocatorContext InvocatorContext { get; }
1813

1914
public ClientWebSocketConnectorOfT(IServiceProvider serviceProvider,
2015
IClientInvocatorContextFactory<TInvocator> invocatorContextFactory,
@@ -23,6 +18,7 @@ public ClientWebSocketConnectorOfT(IServiceProvider serviceProvider,
2318
: base(serviceProvider, compressor, loggerFactory)
2419
{
2520
_invocatorContextFactory = invocatorContextFactory ?? throw new ArgumentNullException(nameof(invocatorContextFactory));
21+
InvocatorContext = _invocatorContextFactory.CreateInvocatorContext();
2622
}
2723
}
28-
}
24+
}

src/NetCoreStack.WebSockets.ProxyClient/IWebSocketConnector.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ public interface IWebSocketConnector
1111
Task ConnectAsync(CancellationTokenSource cancellationTokenSource);
1212
Task SendAsync(WebSocketMessageContext context);
1313
Task SendBinaryAsync(byte[] bytes);
14-
InvocatorContext GetInvocatorContext();
14+
InvocatorContext InvocatorContext { get; }
1515
}
1616

1717
public interface IWebSocketConnector<TInvocator> : IWebSocketConnector where TInvocator : IClientWebSocketCommandInvocator
1818
{
19-
ProxyOptions<TInvocator> Options { get; }
2019
}
21-
}
20+
}

test/Common.Libs/CacheHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static CacheItemDescriptor GetDescriptor(string key)
2929
return descriptor;
3030
}
3131

32-
throw new ArgumentOutOfRangeException($"Key could not be found: \"{key}\"");
32+
return null;
3333
}
3434
}
3535
}

test/NetCoreStack.WebSockets.Tests/ProxyBuilderTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void CustomInvocatorRegistryTest()
6363
var customWebSocketCommandInvocator = Services.GetService<IWebSocketConnector<CustomWebSocketCommandInvocator>>();
6464
Assert.IsType<ClientWebSocketConnectorOfT<CustomWebSocketCommandInvocator>>(customWebSocketCommandInvocator);
6565

66-
var context = customWebSocketCommandInvocator.GetInvocatorContext();
66+
var context = customWebSocketCommandInvocator.InvocatorContext;
6767
Assert.Equal($"TestWebApp-{Environment.MachineName}", context.ConnectorName);
6868
Assert.Equal("localhost:7803", context.HostAddress);
6969

@@ -76,7 +76,7 @@ public void AnotherEndpointWebSocketCommandInvocator()
7676
var customWebSocketCommandInvocator = Services.GetService<IWebSocketConnector<AnotherEndpointWebSocketCommandInvocator>>();
7777
Assert.IsType<ClientWebSocketConnectorOfT<AnotherEndpointWebSocketCommandInvocator>>(customWebSocketCommandInvocator);
7878

79-
var context = customWebSocketCommandInvocator.GetInvocatorContext();
79+
var context = customWebSocketCommandInvocator.InvocatorContext;
8080
Assert.Equal("TestMachineName", context.ConnectorName);
8181
Assert.Equal("localhost:5003", context.HostAddress);
8282

test/WebClientTestApp/CustomWebSocketCommandInvocator.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ namespace WebClientTestApp
1313
public class CustomWebSocketCommandInvocator : IClientWebSocketCommandInvocator
1414
{
1515
private readonly IConnectionManager _connectionManager;
16+
private readonly IWebSocketConnector<CustomWebSocketCommandInvocator> _webSocketConnector;
1617
private readonly ILogger _logger;
1718
private readonly InMemoryCacheProvider _cacheProvider;
1819

19-
public CustomWebSocketCommandInvocator(IConnectionManager connectionManager,
20+
public CustomWebSocketCommandInvocator(IConnectionManager connectionManager,
21+
IWebSocketConnector<CustomWebSocketCommandInvocator> webSocketConnector,
2022
InMemoryCacheProvider cacheProvider,
2123
ILogger<CustomWebSocketCommandInvocator> logger)
2224
{
2325
_connectionManager = connectionManager;
26+
_webSocketConnector = webSocketConnector;
2427
_cacheProvider = cacheProvider;
2528
_logger = logger;
2629
}
@@ -50,6 +53,10 @@ public async Task InvokeAsync(WebSocketMessageContext context)
5053
{
5154
var keyStr = key.ToString();
5255
var descriptor = CacheHelper.GetDescriptor(key.ToString());
56+
if (descriptor == null)
57+
{
58+
return;
59+
}
5360
try
5461
{
5562
var genericList = descriptor.Type.CreateElementTypeAsGenericList();

0 commit comments

Comments
 (0)