示例#1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceFabricHttpClient"/> class.
        /// </summary>
        /// <param name="clusterEndpoints">Uris for Service Cluster management endpoint.</param>
        /// <param name="clientSettings">Client settings for connecting to cluster. Default value is null which means connecting to unsecured cluster.</param>
        /// <param name="innerHandler">The inner handler which is responsible for processing the HTTP response messages. When null or not provided, <see cref="System.Net.Http.HttpClientHandler"/> will be used as last handler in channel.</param>
        /// <param name="delegateHandlers">An ordered list of <see cref="System.Net.Http.DelegatingHandler"/> instances to be invoked in HTTP message channel as message flows to and from the final handler in the channel.
        /// Last handler in the channel is <paramref name="innerHandler"/>.</param>
        public ServiceFabricHttpClient(
            IReadOnlyList <Uri> clusterEndpoints,
            ClientSettings clientSettings  = null,
            HttpClientHandler innerHandler = null,
            params DelegatingHandler[] delegateHandlers)
            : base(clusterEndpoints, clientSettings)
        {
            this.CreateManagementClients();
            var scheme = Uri.UriSchemeHttp;

            // setup security settings
            this.securitySettings            = this.ClientSettings?.SecuritySettings?.Invoke();
            this.refreshSecuritySettingsFunc = this.ClientSettings?.SecuritySettings;
            this.securityType = this.securitySettings?.SecurityType ?? SecurityType.None;

            // validate Uri schema.
            if (this.securityType == SecurityType.X509 ||
                this.securityType == SecurityType.Claims)
            {
                scheme = Uri.UriSchemeHttps;
            }

            var invalidClusterEndpoint = this.ClusterEndpoints.FirstOrDefault(url => !string.Equals(url.Scheme, scheme, StringComparison.OrdinalIgnoreCase));

            if (invalidClusterEndpoint != null)
            {
                throw new ArgumentException(string.Format(SR.ErrorUrlScheme, invalidClusterEndpoint.Scheme, scheme));
            }

            if (delegateHandlers.Any(handler => handler == null))
            {
                throw new ArgumentException(SR.ErrorNullDelegateHandler);
            }

            var seed = (int)DateTime.Now.Ticks;

            this.randomizedEndpoints      = new RandomizedList <Uri>(this.ClusterEndpoints, new Random(seed));
            this.ClientId                 = Guid.NewGuid().ToString();
            this.innerHandler             = innerHandler ?? new HttpClientHandler();
            this.httpClientHandlerWrapper = new HttpClientHandlerWrapper(this.innerHandler);
            this.httpClient               = this.CreateHttpClient(delegateHandlers);

            if (this.ClientSettings?.ClientTimeout != null)
            {
                this.httpClient.Timeout = (TimeSpan)this.ClientSettings.ClientTimeout;
            }
        }
示例#2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceFabricHttpClient"/> class.
        /// </summary>
        /// <param name="builder">Builder isntance to create ServiceFabricHttpClient from.</param>
        private ServiceFabricHttpClient(ServiceFabricClientBuilder builder)
            : base(builder.Endpoints, builder.SecuritySettings, builder.ClientSettings)
        {
            this.CreateManagementClients();

            // Validate when Security Settings is null, url can't be https
            if (this.SecuritySettingsFunc == null)
            {
                var scheme = Uri.UriSchemeHttp;
                var invalidClusterEndpoint = this.ClusterEndpoints.FirstOrDefault(url => !string.Equals(url.Scheme, scheme, StringComparison.OrdinalIgnoreCase));

                if (invalidClusterEndpoint != null)
                {
                    throw new InvalidOperationException(string.Format(SR.ErrorUrlScheme, invalidClusterEndpoint.Scheme, scheme));
                }
            }
            else
            {
                // Url validation for secured cluster will be done after SecuritySettings is invoked in Initialize, it can be https only for Claims and X509.
            }

            var seed = (int)DateTime.Now.Ticks;

            this.randomizedEndpoints = new RandomizedList <Uri>(this.ClusterEndpoints, new Random(seed));
            this.ClientId            = Guid.NewGuid().ToString();

            // Get information from DI container in ServiceFabricClientBuilder
            this.innerHandler = new HttpClientHandler();
            if (builder.Container.ContainsKey(typeof(HttpClientHandler)))
            {
                this.innerHandler = (HttpClientHandler)builder.Container[typeof(HttpClientHandler)];
            }

            if (builder.Container.ContainsKey(typeof(DelegatingHandler[])))
            {
                this.delegateHandlers = (DelegatingHandler[])builder.Container[typeof(DelegatingHandler[])];
            }

            this.refreshSecuritySettingsFunc = this.SecuritySettingsFunc;
            this.httpClientHandlerWrapper    = new HttpClientHandlerWrapper(this.innerHandler);
            this.ClientTypeHeaderValue       = Constants.ClientlibClientTypeHeaderValue;
        }