public NatsClient(ConnectionInfo connectionInfo, ISocketFactory socketFactory = null) { if (connectionInfo == null) { throw new ArgumentNullException(nameof(connectionInfo)); } Id = UniqueId.Generate(); _inboxAddress = $"IB.{Id}"; _sync = new SemaphoreSlim(1, 1); _connectionInfo = connectionInfo.Clone(); _subscriptions = new ConcurrentDictionary <string, Subscription>(StringComparer.OrdinalIgnoreCase); _eventMediator = new NatsObservableOf <IClientEvent>(); _opMediator = new NatsOpMediator(); _connectionManager = new NatsConnectionManager(socketFactory ?? new SocketFactory()); Events.SubscribeSafe(async ev => { switch (ev) { case ClientDisconnected disconnected when disconnected.Reason == DisconnectReason.DueToFailure && _connectionInfo.AutoReconnectOnFailure: await ReconnectAsync().ConfigureAwait(false); break; } }); }
public NatsClient(ConnectionInfo connectionInfo, ISocketFactory socketFactory = null) { EnsureArg.IsNotNull(connectionInfo, nameof(connectionInfo)); _sync = new Locker(); _connectionInfo = connectionInfo.Clone(); _subscriptions = new ConcurrentDictionary <string, Subscription>(); _eventMediator = new NatsObservableOf <IClientEvent>(); _opMediator = new NatsOpMediator(); _connectionManager = new NatsConnectionManager(socketFactory ?? new SocketFactory()); Events.SubscribeSafe(ev => { if (ev is ClientDisconnected disconnected) { var shouldTryToReconnect = _connectionInfo.AutoReconnectOnFailure && disconnected.Reason == DisconnectReason.DueToFailure; if (!shouldTryToReconnect) { return; } Reconnect(); } }); }