Esempio n. 1
0
        /// <summary>
        /// Completes an asynchronous operation to return a, possibly converted, <see cref="XmlMessage"/>-based response to a
        /// relayed <see cref="XmlMessage"/>-based request.
        /// </summary>
        /// <typeparam name="TResponse">
        /// The type of the <see cref="XmlMessage"/>-based response.
        /// </typeparam>
        /// <param name="asyncResult">
        /// The <see cref="IAsyncResult"/> returned by a call to the <see
        /// cref="BeginRelayRequest{TRequest}(TRequest,AsyncCallback,object)"/> method.
        /// </param>
        /// <returns>
        /// The <see cref="XmlMessage"/>-based response.
        /// </returns>
        protected TResponse EndRelayRequest <TResponse>(IAsyncResult asyncResult)
            where TResponse : XmlMessage, new()
        {
            if (asyncResult == null)
            {
                throw new ArgumentNullException(nameof(asyncResult));
            }
            if (_logger.IsInfoEnabled)
            {
                _logger.InfoFormat("Beginning relaying asynchronous response {0}.", typeof(TResponse).Name);
            }
            ClientRelay clientRelay = null;

            try
            {
                var wrappedAsyncResult = ((AsyncResultWrapper)asyncResult).WrappedAsyncResult;
                clientRelay = (ClientRelay)wrappedAsyncResult.AsyncState;
                var response = clientRelay.EndRequest <TResponse>(wrappedAsyncResult);
                clientRelay.Close();
                if (_logger.IsInfoEnabled)
                {
                    _logger.InfoFormat("Relaying asynchronous response {0} succeeded.", response);
                }
                return(response);
            }
            catch (Exception exception)
            {
                if (_logger.IsWarnEnabled)
                {
                    _logger.Warn($"Relaying asynchronous response {typeof(TResponse).Name} failed.", exception);
                }
                clientRelay?.Abort();
                throw;
            }
        }
Esempio n. 2
0
        private TResponse RelayRequest <TRequest, TResponse>(TRequest request, Func <ClientRelay, TimeSpan> getTimeout, IXmlMessageConverter converter)
            where TRequest : XmlMessage
            where TResponse : XmlMessage, new()
        {
            if (_logger.IsInfoEnabled)
            {
                _logger.InfoFormat("Relaying synchronous request {0}.", request);
            }
            ClientRelay clientRelay = null;

            try
            {
                clientRelay = _clientRelayFactory();
                var response = clientRelay.Request <TRequest, TResponse>(request, getTimeout(clientRelay), converter);
                clientRelay.Close();
                if (_logger.IsInfoEnabled)
                {
                    _logger.InfoFormat("Relaying synchronous response {0} succeeded.", response);
                }
                return(response);
            }
            catch (Exception exception)
            {
                if (_logger.IsWarnEnabled)
                {
                    _logger.Warn($"Relaying synchronous response {typeof(TResponse).Name} failed.", exception);
                }
                clientRelay?.Abort();
                throw;
            }
        }
Esempio n. 3
0
        private IAsyncResult BeginRelayRequest <TRequest>(
            TRequest request,
            Func <ClientRelay, TimeSpan> getTimeout,
            IXmlMessageConverter converter,
            AsyncCallback asyncCallback,
            object asyncState)
            where TRequest : XmlMessage
        {
            if (_logger.IsInfoEnabled)
            {
                _logger.InfoFormat("Beginning relaying asynchronous request {0}.", request);
            }
            ClientRelay clientRelay = null;

            try
            {
                clientRelay = _clientRelayFactory();
                var ar = clientRelay.BeginRequest(
                    request,
                    getTimeout(clientRelay),
                    converter,
                    asyncResult => asyncCallback(new AsyncResultWrapper(asyncResult, asyncState)),
                    clientRelay);
                if (_logger.IsInfoEnabled)
                {
                    _logger.InfoFormat("Relaying asynchronous request {0} succeeded.", typeof(TRequest).Name);
                }
                return(ar);
            }
            catch (Exception exception)
            {
                if (_logger.IsWarnEnabled)
                {
                    _logger.Warn($"Relaying asynchronous request {typeof(TRequest).Name} failed.", exception);
                }
                clientRelay?.Abort();
                throw;
            }
        }