public void Add(string endpointName, ReceiveEndpoint endpoint) { if (endpoint == null) { throw new ArgumentNullException(nameof(endpoint)); } if (string.IsNullOrWhiteSpace(endpointName)) { throw new ArgumentException($"The {nameof(endpointName)} must not be null or empty", nameof(endpointName)); } endpoint.HealthResult = _started ? EndpointHealthResult.Healthy(endpoint, "starting") : EndpointHealthResult.Unhealthy(endpoint, "not ready", null); var added = _endpoints.TryAdd(endpointName, key => { endpoint.ConnectReceiveEndpointObserver(new ReceiveEndpointStateMachineObserver(_machine, endpoint)); return(endpoint); }); if (!added) { throw new ConfigurationException($"A receive endpoint with the same key was already added: {endpointName}"); } }
HostReceiveEndpointHandle StartEndpoint(string endpointName, ReceiveEndpoint endpoint, CancellationToken cancellationToken) { try { var observerHandle = endpoint.ConnectReceiveEndpointObserver(_receiveEndpointObservers); void RemoveEndpoint() { observerHandle.Disconnect(); Remove(endpointName); } var handle = new Handle(endpoint, RemoveEndpoint); handle.Start(cancellationToken); TaskUtil.Await(() => _machine.RaiseEvent(endpoint, _machine.ReceiveEndpointStarted, cancellationToken), cancellationToken); return(handle); } catch { lock (_mutateLock) _endpoints.Remove(endpointName); throw; } }
HostReceiveEndpointHandle StartEndpoint(string endpointName, ReceiveEndpoint endpoint, CancellationToken cancellationToken) { try { var observerHandle = endpoint.ConnectReceiveEndpointObserver(_receiveEndpointObservers); void RemoveEndpoint() { observerHandle.Disconnect(); Remove(endpointName); } var handle = new Handle(endpoint, RemoveEndpoint); handle.Start(cancellationToken); var raiseEventTask = _machine.RaiseEvent(endpoint, _machine.ReceiveEndpointStarted, cancellationToken); if (raiseEventTask.IsCompletedSuccessfully()) { return(handle); } TaskUtil.Await(() => raiseEventTask, cancellationToken); return(handle); } catch { _endpoints.TryRemove(endpointName, out _); throw; } }
public void Add(string endpointName, ReceiveEndpoint endpoint) { if (endpoint == null) { throw new ArgumentNullException(nameof(endpoint)); } if (string.IsNullOrWhiteSpace(endpointName)) { throw new ArgumentException($"The {nameof(endpointName)} must not be null or empty", nameof(endpointName)); } lock (_mutateLock) { if (_endpoints.ContainsKey(endpointName)) { throw new ConfigurationException($"A receive endpoint with the same key was already added: {endpointName}"); } endpoint.ConnectReceiveEndpointObserver(new ReceiveEndpointStateMachineObserver(_machine, endpoint)); _endpoints.Add(endpointName, endpoint); } }