예제 #1
0
        /// <summary>
        /// Initializes a new instance of the RoutingServiceUrlProvider class.
        /// </summary>
        /// <param name="serviceUrl">A url to the load-balancing web service.</param>
        /// <param name="serverTitle">A title of the server to use load-balancing for.</param>
        /// <param name="certificateValidationSettings">The reference to the certificate
        /// validation settings object.</param>
        public RoutingServiceUrlProvider(
            string serviceUrl,
            string serverTitle,
            ICertificateValidationSettings certificateValidationSettings)
        {
            Debug.Assert(serviceUrl != null, "Expects non-null serviceUrl");
            Debug.Assert(serverTitle != null, "Expects non-null serverTitle");
            Debug.Assert(
                certificateValidationSettings != null,
                "Expects non-null certificateValidationSettings");

            _serviceUrl  = serviceUrl;
            _serverTitle = serverTitle;

            var uri = new Uri(serviceUrl);

            _serviceScheme = uri.Scheme;

            _certificateValidationSettings = certificateValidationSettings;

            _serviceWrapper = new RetriableInvocationWrapper(
                MAX_RETRY_COUNT,
                _PrepareForRetry,
                _TranslateExceptions);
        }
예제 #2
0
        /// <summary>
        /// Invokes the specified service method.
        /// </summary>
        /// <param name="method">The method to be invoked.</param>
        /// <exception cref="ArgumentNullException"><paramref name="method"/> is a null
        /// reference.</exception>
        private TResult _Invoke <TResult>(Func <TService, TResult> method)
        {
            CodeContract.RequiresNotNull("method", method);

            var invocationWrapper = new RetriableInvocationWrapper(
                MAX_RETRY_COUNT,
                _PrepareRetry,
                _TranslateException);

            var result = default(TResult);

            invocationWrapper.Invoke(() =>
            {
                try
                {
                    using (var connection = _connectionPool.AcquireConnection())
                    {
                        result = method(connection.Client);
                    }
                }
                catch (Exception ex)
                {
                    if (!_exceptionHandler.HandleException(ex, _serviceTitle))
                    {
                        throw;
                    }
                }
            });

            return(result);
        }
예제 #3
0
        /// <summary>
        /// Checks if the specified url points to the load balancing service.
        /// </summary>
        /// <param name="serviceUrl">Url to check for service presence.</param>
        /// <returns>true if and only if the <paramref name="serviceUrl"/>
        /// points to the load balancing service.</returns>
        public static bool HasLoadBalanceService(string serviceUrl)
        {
            var hasService = false;

            try
            {
                var metadataUri = new Uri(serviceUrl + "?wsdl");
                var mexClient   = new MetadataExchangeClient(
                    metadataUri,
                    MetadataExchangeClientMode.HttpGet);
                var metadataRetrievalWrapper = new RetriableInvocationWrapper(
                    MAX_RETRY_COUNT,
                    _PrepareForRetry);
                var metaDocs = metadataRetrievalWrapper.Invoke(
                    () => mexClient.GetMetadata());
                var importer  = new WsdlImporter(metaDocs);
                var contracts = importer.ImportAllContracts();
                foreach (var contract in contracts)
                {
                    if (string.Equals(
                            contract.Name,
                            LOAD_BALANCE_SERVICE_CONTRACT_NAME,
                            StringComparison.InvariantCulture))
                    {
                        hasService = true;
                        break;
                    }
                }
            }
            catch (InvalidOperationException e)
            {
                Logger.Warning(e);
            }
            catch (Exception e)
            {
                if (!ServiceHelper.IsCommunicationError(e))
                {
                    throw;
                }

                Logger.Warning(e);
            }

            return(hasService);
        }
예제 #4
0
        /// <summary>
        /// Sends REST request to the specified url.
        /// </summary>
        /// <typeparam name="T">The type of the request result.</typeparam>
        /// <param name="context">The reference to the rest request context object.</param>
        /// <param name="url">The url to send request to.</param>
        /// <param name="query">The query to be sent.</param>
        /// <param name="opt">The reference to the request sending options.</param>
        /// <returns>Result of the specified REST request.</returns>
        /// <exception cref="T:ESRI.ArcLogistics.Routing.RestException">error was
        /// returned by the REST API.</exception>
        /// <exception cref="T:ESRI.ArcLogistics.CommunicationException">failed
        /// to communicate with the REST service at the specified url.</exception>
        /// <exception cref="T:System.ArgumentNullException">Any of
        /// <paramref name="context"/>, <paramref name="url"/>,
        /// <paramref name="query"/> or <paramref name="opt"/> arguments is a null
        /// reference.</exception>
        public T SendRequest <T>(
            IRestRequestContext context,
            string url,
            string query,
            HttpRequestOptions opt)
        {
            Debug.Assert(context != null);
            Debug.Assert(!string.IsNullOrEmpty(url));
            Debug.Assert(!string.IsNullOrEmpty(query));
            Debug.Assert(opt != null);

            var sendRequestWrapper = new RetriableInvocationWrapper(
                MAX_RETRY_COUNT,
                (e) => _PrepareRetry(context, e),
                (e) => _TranslateException(context, e));

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            return(sendRequestWrapper.Invoke(() =>
            {
                var timeout = opt.Timeout - stopwatch.ElapsedMilliseconds;
                timeout = Math.Max(timeout, 0);
                opt.Timeout = (int)timeout;

                var response = _SendRequest <T>(
                    context,
                    url,
                    query,
                    opt);
                RestHelper.ValidateResponse(response);

                return response;
            }));
        }
        /// <summary>
        /// Checks if the specified url points to the load balancing service.
        /// </summary>
        /// <param name="serviceUrl">Url to check for service presence.</param>
        /// <returns>true if and only if the <paramref name="serviceUrl"/>
        /// points to the load balancing service.</returns>
        public static bool HasLoadBalanceService(string serviceUrl)
        {
            var hasService = false;

            try
            {
                var metadataUri = new Uri(serviceUrl + "?wsdl");
                var mexClient = new MetadataExchangeClient(
                    metadataUri,
                    MetadataExchangeClientMode.HttpGet);
                var metadataRetrievalWrapper = new RetriableInvocationWrapper(
                    MAX_RETRY_COUNT,
                    _PrepareForRetry);
                var metaDocs = metadataRetrievalWrapper.Invoke(
                    () => mexClient.GetMetadata());
                var importer = new WsdlImporter(metaDocs);
                var contracts = importer.ImportAllContracts();
                foreach (var contract in contracts)
                {
                    if (string.Equals(
                        contract.Name,
                        LOAD_BALANCE_SERVICE_CONTRACT_NAME,
                        StringComparison.InvariantCulture))
                    {
                        hasService = true;
                        break;
                    }
                }
            }
            catch (InvalidOperationException e)
            {
                Logger.Warning(e);
            }
            catch (Exception e)
            {
                if (!ServiceHelper.IsCommunicationError(e))
                {
                    throw;
                }

                Logger.Warning(e);
            }

            return hasService;
        }