public TransportInitializationHandler(IHttpClient httpClient, IConnection connection, string connectionData, string transport, CancellationToken disconnectToken, TransportHelper transportHelper) { if (connection == null) { throw new ArgumentNullException("connection"); } _connection = connection; _httpClient = httpClient; _connectionData = connectionData; _transport = transport; _transportHelper = transportHelper; _initializationTask = new DispatchingTaskCompletionSource <object>(); _initializationInvoker = new ThreadSafeInvoker(); // Default event OnFailure = () => { }; // We want to fail if the disconnect token is tripped while we're waiting on initialization try { _tokenCleanup = disconnectToken.SafeRegister( _ => Fail(new OperationCanceledException(Resources.Error_ConnectionCancelled, disconnectToken)), state: null); } catch (ObjectDisposedException) { // We only dispose this token after cancelling it, so consider this cancellation. // The ODE is only thrown on .NET 4.5.2 and below (.NET 4.6 no longer throws ODE from CTS.Register) Fail(new OperationCanceledException(Resources.Error_ConnectionCancelled, disconnectToken)); } TaskAsyncHelper.Delay(connection.TotalTransportConnectTimeout) .Then(() => { // don't timeout once connect request has finished if (Interlocked.CompareExchange(ref _state, InitializationState.Failed, InitializationState.Initial) == InitializationState.Initial) { Fail(new TimeoutException(Resources.Error_TransportTimedOutTryingToConnect)); } }); }
public TransportInitializationHandler(TimeSpan failureTimeout, CancellationToken disconnectToken) { _initializationTask = new TaskCompletionSource <object>(); _initializationInvoker = new ThreadSafeInvoker(); // Default event OnFailure = () => { }; // We want to fail if the disconnect token is tripped while we're waiting on initialization _tokenCleanup = disconnectToken.SafeRegister(_ => { Fail(); }, state: null); TaskAsyncHelper.Delay(failureTimeout).Then(() => { Fail(new TimeoutException(Resources.Error_TransportTimedOutTryingToConnect)); }); }