예제 #1
0
        /// <summary>
        /// Creates a communication client for the given endpoint address.
        /// </summary>
        /// <param name="endpoint">Endpoint address where the service is listening</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <returns>The communication client that was created</returns>
        protected override async Task <WcfCommunicationClient <TServiceContract> > CreateClientAsync(string endpoint, CancellationToken cancellationToken)
        {
            EndpointAddress  endpointAddress            = new EndpointAddress(endpoint);
            TServiceContract channel                    = this.factory.CreateChannel(endpointAddress);
            IClientChannel   clientChannel              = (IClientChannel)channel;
            Exception        connectionTimeoutException = null;

            try
            {
                Task openTask = Task.Factory.FromAsync(clientChannel.BeginOpen(this.factory.Endpoint.Binding.OpenTimeout, null, null), clientChannel.EndOpen);
                if (await Task.WhenAny(openTask, Task.Delay(this.factory.Endpoint.Binding.OpenTimeout, cancellationToken)) == openTask)
                {
                    if (openTask.Exception != null)
                    {
                        throw openTask.Exception;
                    }
                    openTask = (Task)null;
                }
                else
                {
                    clientChannel.Abort();
                    throw new TimeoutException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, "Failed to open communication client after period '{0}'", new object[1]
                    {
                        (object)this.factory.Endpoint.Binding.OpenTimeout
                    }));
                }
            }
            catch (AggregateException ex)
            {
                ex.Handle((Func <Exception, bool>)(x => x is TimeoutException));
                connectionTimeoutException = (Exception)ex;
            }
            catch (TimeoutException ex)
            {
                connectionTimeoutException = (Exception)ex;
            }
            if (connectionTimeoutException != null)
            {
                throw new EndpointNotFoundException(connectionTimeoutException.Message, connectionTimeoutException);
            }
            clientChannel.OperationTimeout = this.factory.Endpoint.Binding.ReceiveTimeout;
            return(this.CreateWcfCommunicationClient(channel));
        }
예제 #2
0
        public void OpenAsync()
        {
            if (_isDisposed)
                throw new ObjectDisposedException(this.GetType().Name);
            if (ChangeStateSafe(StableConnectionState.Opening) != StableConnectionState.Created)
                throw new InvalidOperationException("Can't open channel, that is in state: " + _state.ToString());

            try
            {
                _channel.BeginOpen(new AsyncCallback(OpenedAsyncHandler), null);
            }
            catch (CommunicationException cex)
            {
                Logger.Logger.Instance.Debug(cex, "Error during opening the connection to " + TargetName);
                OnClosed();
            }
            catch (TimeoutException tex)
            {
                Logger.Logger.Instance.Debug(tex, "Error during opening the connection to " + TargetName);
                OnClosed();
            }
        }